<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Stephen Diehl</title>
    <description>Personal Blog</description>
    <link>https://www.stephendiehl.com</link>
    <atom:link href="https://www.stephendiehl.com/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Thu, 02 Apr 2026 01:00:00 +0000</pubDate>
    <lastBuildDate>Thu, 02 Apr 2026 01:00:00 +0000</lastBuildDate>
    
    <item>
      <title>Book Review: What We Can Know</title>
      <description>&lt;h1 id=&quot;book-review%3A-what-we-can-know&quot; tabindex=&quot;-1&quot;&gt;Book Review: What We Can Know&lt;/h1&gt;
&lt;p&gt;Ian McEwan has described &lt;em&gt;What We Can Know&lt;/em&gt; as &amp;quot;science fiction without the science,&amp;quot; which is both a fair warning and a precise advertisement. The novel is set in 2119. Rising seas and a cascade of nuclear wars (including a misfired Russian warhead in the Atlantic and something called the Third Sino-American War) have halved the world&#39;s population and turned Britain into a sleepy archipelago of mountain peaks connected by ferries and funiculars. The Bodleian Library has been relocated to Snowdonia. The global currency is the Nigerian naira. The United States is a lawless territory of feuding warlords. People navigate between islands in electric canoes. Oxford&#39;s Sheldonian Theatre is underwater. And the people of this era look back at the early twenty-first century with the specific mixture of envy and contempt that we reserve for civilizations that had everything and squandered it. They call our time &amp;quot;the Derangement.&amp;quot; They study us the way we study the late Romans: as a case study in spectacular, voluntary decline.&lt;/p&gt;
&lt;p&gt;McEwan builds this world with the offhand confidence of someone who has thought about it for a very long time and decided that understatement is more frightening than spectacle. He never dwells on the catastrophe. He simply places you in its aftermath and lets you feel the weight of what is missing. The future is rendered in small, devastating details: a literature professor teaching to near-empty rooms, &amp;quot;a relic of the humanities in a world that no longer values them, a poor cousin to the water scientists.&amp;quot; The only growth industries are data recovery and atmospheric management. Meanwhile, future scholars study the period 1990-2030 under the rubric &amp;quot;90.30 literature,&amp;quot; and their verdict on us is brutal: &amp;quot;They were big and brave, superb scholars and scientists, musicians, actors and athletes, and they were idiots who were throwing it all away.&amp;quot;&lt;/p&gt;
&lt;p&gt;Into this world McEwan places Tom Metcalfe, a literature professor who has devoted his career to reconstructing a lost poem. The poem, &amp;quot;A Corona for Vivien,&amp;quot; was written by the celebrated (and, as we will learn, monstrous) poet Francis Blundy. A corona is a chain of linked sonnets where each begins with the final line of the previous one and the sequence closes when the first line returns, forming a circle. Blundy read it aloud at his wife Vivien&#39;s birthday dinner in 2014. The few who heard it called it the capstone of his career, comparable to &lt;em&gt;The Waste Land&lt;/em&gt;. He then destroyed all drafts, making the single vellum copy a unique gift. The poem was never seen again.&lt;/p&gt;
&lt;p&gt;Tom has access to the complete digital archive of the early twenty-first century. Quantum computing broke all the cryptography of our era, cracking open every encrypted data silo, every private message, every corporate vault. The result is total transparency: every email, every text message, every shopping list, every social media post, all of it preserved in servers in New Lagos, all of it legible to anyone with a library card. He can reconstruct the dinner party guest by guest, message by message. And here is the novel&#39;s central, quietly devastating paradox: having all the data changes nothing. The fundamental truth about the poem, about the people who made and destroyed it, remains hidden. As Tom observes with the wry frustration of a man drowning in information: &amp;quot;If you want your secrets kept, whisper them into the ear of your dearest, most trusted friend. Do not trust the keyboard and screen. If you do, we&#39;ll know everything.&amp;quot; Everything, that is, except what matters.&lt;/p&gt;
&lt;p&gt;This is McEwan&#39;s epistemological argument, and he pursues it with the rigor of a philosopher and the patience of a novelist who has been thinking about narrative and knowledge for fifty years. The past, he argues, is always a reconstruction. &amp;quot;Memory is a sponge. It soaks up material from other times, other places and leaks it all over the moment in question.&amp;quot; A journal &amp;quot;fixes events like beads on a string,&amp;quot; but lived experience refuses to be sequential. The imagined, he writes, &amp;quot;lords it over the actual, no paradox or mystery there.&amp;quot; What we can know, it turns out, is bounded on all sides by what we want to believe, what we can remember, and what we are willing to see.&lt;/p&gt;
&lt;p&gt;The novel&#39;s second half detonates everything the first half has built. Vivien&#39;s own memoir surfaces, and in it we learn that Francis Blundy, revered poet, climate change denier, man of letters compared to Heaney and Larkin, murdered Vivien&#39;s first husband. Percy Greene, a luthier with early-onset Alzheimer&#39;s, was pushed down the stairs and finished with a mallet. Blundy framed it as mercy. He then absorbed the dead man&#39;s suffering into his poetry, transmuting private horror into public art. The famous &amp;quot;Corona for Vivien&amp;quot; was, in coded form, a confession of this crime and an aestheticization of the grief it produced. Vivien, recognizing the poem as a beautiful counterfeit that stole her real memories and repackaged them as literary accomplishment, fed the vellum into a dairy stove. The poem burned. She then buried her prose account, ensuring that the truth, rather than the artful lie, would reach the future.&lt;/p&gt;
&lt;p&gt;McEwan has always been drawn to people who believe their own brilliance exempts them from ordinary moral constraints, and Blundy is the fullest realization of this obsession. The novel asks whether great art can launder terrible acts and has the courage to leave the question genuinely open. When Vivien destroys the poem, you feel the loss and the justice simultaneously, and that simultaneity is the point. The beauty of the work and the cruelty of the man who made it occupy the same space, and McEwan will not let you collapse one into the other.&lt;/p&gt;
&lt;p&gt;If you have read &lt;em&gt;Atonement&lt;/em&gt;, you will recognize the engine: nested narratives, the question of who controls the story, the devastation that arrives when the reader&#39;s reconstruction collides with the truth. But where &lt;em&gt;Atonement&lt;/em&gt; asked whether fiction could redeem guilt, &lt;em&gt;What We Can Know&lt;/em&gt; goes further. It suggests that the possibility of redemption was always a comfortable fiction, and that what remains is persistence. A colleague&#39;s pregnancy near the end is described as &amp;quot;the next link in the chain of futility and care,&amp;quot; with the emphasis falling equally on both nouns. You keep going because the work of reconstruction (of poems, of knowledge, of a livable world) has its own dignity regardless of outcome.&lt;/p&gt;
&lt;p&gt;The novel is dense, and it is about more things than any single review can cover. It is about the humanities and whether they survive catastrophe (they do, barely, and in diminished form, and the novel argues this diminished survival is still worth something). It is about climate change as a moral solvent that dissolves familiar categories into entropy. It is about the specific longing of looking backward at a world you never knew, for which McEwan observes we need a word &amp;quot;beyond nostalgia, which pines for what was once known.&amp;quot; It is about the relationship between scholarship and love, between archival precision and emotional truth, between the living and the dead.&lt;/p&gt;
&lt;p&gt;McEwan&#39;s prose has loosened with age. The earlier novels were watchmaker-precise, sometimes to the point of airlessness. Here the sentences breathe. There is humor (dry, English, arriving when you least expect it) and a warmth toward the characters that his earlier work sometimes withheld. The &lt;em&gt;New York Times&lt;/em&gt; called it &amp;quot;the best thing McEwan has written in ages.&amp;quot; &lt;em&gt;The Scotsman&lt;/em&gt; called it his masterpiece. I think they are both right. It is the rare novel that will make you feel as deeply as it makes you think, and the rarer one still that earns the right to that ambition.&lt;/p&gt;
&lt;p&gt;McEwan has said that his goal was &amp;quot;to let the past, present and future address each other across the barriers of time.&amp;quot; He has succeeded. If you are reading this in 2026, the novel&#39;s future scholars have already rendered their verdict on you. The fact that you are reading a book review rather than doomscrolling may, by the standards of 2119, constitute a minor act of civilization. Make the most of it.&lt;/p&gt;
</description>
      <pubDate>Thu, 02 Apr 2026 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/what_we_can_know/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/what_we_can_know/</guid>
    </item>
    
    <item>
      <title>Book Review: There Is No Antimemetics Division</title>
      <description>&lt;h1 id=&quot;book-review%3A-there-is-no-antimemetics-division&quot; tabindex=&quot;-1&quot;&gt;Book Review: There Is No Antimemetics Division&lt;/h1&gt;
&lt;p&gt;There is a particular flavor of horror that only people who work with formal systems for a living can fully appreciate. It is the horror of data loss, of silent corruption, of the thing that fails without logging an error. It is the backup that was never tested. The monitoring system that monitors everything except its own health. The silent failure that propagates through a distributed system for weeks before anyone notices, and by the time you do notice, the state of the world has drifted so far from what you believed it to be that the gap itself has become invisible. If Kafka wrote incident reports, they would read like this novel. If this specific dread resonates with you on a spiritual level, then Sam Hughes (writing as &lt;a href=&quot;https://qntm.org/&quot;&gt;qntm&lt;/a&gt;) has written your nightmare scenario, scaled up from a database to the entire ontological fabric of reality, and turned it into one of the most inventive science fiction novels of the past decade.&lt;/p&gt;
&lt;p&gt;The premise is this: a meme is an idea that wants to be known. It spreads, replicates, and lodges itself in your mind. An antimeme is the opposite. It is an idea, or an entity, or a phenomenon that actively resists being perceived or remembered. The moment you stop looking at it, it vanishes from your mind, and so does the fact that you were ever looking. You cannot fight it because you cannot remember it exists. You cannot organize a defense because the knowledge that a defense is needed is the first thing it destroys. The monster hides in the structure of cognition itself. The darkness is a feature.&lt;/p&gt;
&lt;p&gt;The book originated as a series of entries on the SCP Foundation wiki, a collaborative fiction project where contributors write clinical, bureaucratic documentation for fictional anomalous objects and entities. If you have never encountered SCP, imagine the filing cabinets of a secret agency that catalogs impossible things with the same dry procedural language you would use to document a network outage. The SCP Foundation is, in essence, what would happen if the IETF wrote horror fiction, and the result is exactly as wonderful as that sounds. It is one of the genuinely great creative experiments of the internet age, and Hughes&#39;s Antimemetics Division entries are widely regarded as the best thing to come out of it. The novel collects and revises these into a coherent narrative, and it works beautifully as a standalone book.&lt;/p&gt;
&lt;p&gt;The protagonist, Marion Wheeler, runs the Antimemetics Division. Her job is to remember things that the universe is trying to make her forget. She and her staff take mnestic drugs (the opposite of amnestics: chemicals that force you to retain memories your brain is actively discarding) to perceive and fight threats that are, by their nature, invisible to everyone else. The drugs have brutal side effects. The work has worse ones. You are fighting a war that nobody knows is happening, that nobody will remember you fought, and that erases its own history as it proceeds. Every victory is immediately forgotten. Every sacrifice is invisible. It is, in other words, open source maintainership as cosmic horror. This is heroism that is structurally incapable of being recognized, which is either the noblest possible form of service or the most absurd possible form of futility, and Hughes has the good sense not to tell you which.&lt;/p&gt;
&lt;p&gt;The novel&#39;s cosmology is built on a hierarchy where information is more real than matter. Beneath ordinary three-dimensional spacetime lies the noosphere: the space of all human-conceivable ideas, memes, and concepts, a vast ecology that transcends the physical world and can retroactively edit memory, identity, and even the historical record. The noosphere is not a metaphor. It is, within the novel&#39;s logic, the true substrate of reality, and the physical world is a shadow cast by it. This idea-space has its own fauna, including antimemetic spiders and various lesser predators that rip through the membrane between concept and matter. And at the top of the food chain sits SCP-3125 (renamed in the published edition, but the designation is so perfect I am using it anyway): a five-dimensional informational apex predator shaped, in the rare moments anyone perceives it, like a starfish. It is so vast and so hostile that anyone who successfully conceptualizes it is detected and killed. Knowing about it is lethal. Understanding it is lethal. The act of comprehension is the attack surface. This is Lovecraftian horror translated into information theory with perfect fidelity. Lovecraft&#39;s cosmos was terrifying because it was indifferent and vast. Hughes&#39;s cosmos is terrifying because it is hostile and memetic. The monster lurks in the act of thinking about it.&lt;/p&gt;
&lt;p&gt;What elevates the book from clever premise to genuine art is what it does with memory and identity. Marion&#39;s strategy for fighting SCP-3125 involves systematically erasing her own memories to deny the entity information it could use as a vector. She forgets her husband. She forgets her colleagues. She works from notes and recordings because her own mind has become unreliable. The protagonist of the novel is a woman who is voluntarily dismantling her own identity in order to save a world that will never know she existed. Every convention of the heroic arc is inverted. The hero loses knowledge and power over the course of the story. Deliberately. The sacrifice goes beyond death (though that comes too). The real sacrifice is self-erasure: the voluntary destruction of everything that makes you you.&lt;/p&gt;
&lt;p&gt;And then, in the second half, something extraordinary happens. Marion&#39;s husband, Adam, senses her absence. He cannot remember her. He has no evidence she existed. But there is a hole in his life that has her shape, and the shape will not go away. Love, the novel argues, leaves traces that even antimemetic erasure cannot fully remove. This is the most emotionally devastating science fiction idea I have encountered in years, and Hughes handles it with the restraint and precision it deserves. In a lesser book it would be sentimental. Here it is earned, because the entire preceding narrative has been a rigorous demonstration of exactly how much can be lost, which makes the thing that persists feel miraculous.&lt;/p&gt;
&lt;p&gt;The novel&#39;s form mirrors its content in a way that is both structurally elegant and deeply unsettling. Chapters begin mid-scene because the context has been erased. Characters appear without introduction because the reader, like the characters, has been dropped into a situation whose history is missing. You read the way the Antimemetics Division works: assembling fragments, inferring what is missing from the outline of what remains, never certain your reconstruction is correct. It is the only honest way to tell a story about forgetting.&lt;/p&gt;
&lt;p&gt;If you work with systems, if you think about information, if you have ever been kept up at night by the fragility of the things we build and the ease with which they can fail silently, this book will get under your skin and stay there. It is Lovecraft for people who think in terms of information theory. It is a horror novel where the monster is an idea and the weapon is your own capacity to understand it. It is brilliant, it is original, and it is the best argument I have seen for why the SCP Foundation is one of the most important literary projects of the twenty-first century. That a novel this good started life as collaborative wiki fiction is itself an antimemetic phenomenon: a masterpiece hiding in plain sight in a format that literary culture is constitutionally incapable of taking seriously. Read it, and then try to remember that you did.&lt;/p&gt;
</description>
      <pubDate>Sun, 22 Mar 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/no_antimimetics/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/no_antimimetics/</guid>
    </item>
    
    <item>
      <title>Book Review: Piranesi</title>
      <description>&lt;h1 id=&quot;book-review%3A-piranesi&quot; tabindex=&quot;-1&quot;&gt;Book Review: Piranesi&lt;/h1&gt;
&lt;p&gt;Susanna Clarke&#39;s &lt;em&gt;Piranesi&lt;/em&gt; is a book about a man who lives in a House. The House has an infinite number of marble halls. Ocean tides flood the lower floors. Clouds drift through the upper ones. Thousands of statues stand in the middle halls, each one unique, depicting figures from mythology and daily life and everything in between. The narrator maps the halls, tracks the tides, catches fish to eat, and keeps meticulous journals. He calls himself Piranesi. He believes he is one of two living people in the entire world. He is happy.&lt;/p&gt;
&lt;p&gt;If this sounds like a fairy tale, it reads like one too. The prose is plain, earnest, and completely without irony, delivered in the cadence of a naturalist&#39;s field journal. Piranesi describes extraordinary things (tidal waves crashing through marble vestibules, clouds forming indoors, skeletons of people who died in halls nobody else has visited) with the calm precision of someone recording the migratory patterns of local birds. The effect is hypnotic. You settle into the rhythm of his observations and begin to see the House the way he sees it: as a complete and beautiful world, sufficient unto itself.&lt;/p&gt;
&lt;p&gt;And then, very gradually, the floor drops out.&lt;/p&gt;
&lt;p&gt;Clarke structures the novel so that you, the reader, understand what is happening to Piranesi before he does. You notice the gaps in his journals. You catch the references to things that should not exist in an infinite marble labyrinth (shoes, a wristwatch, the word &amp;quot;Manchester&amp;quot;). You begin to assemble the real story from fragments, exactly the way an archaeologist assembles a pot from shards. The dramatic irony is sustained for the entire book, and it is devastating, because Piranesi&#39;s innocence is so total and so genuine that watching it dissolve feels like a small crime.&lt;/p&gt;
&lt;p&gt;The philosophical engine of the novel is a question about identity and memory. Piranesi&#39;s real name, real history, and real knowledge have been taken from him. What remains is a person who is good, curious, and attentive to the world he inhabits. Clarke asks whether this person, built from the ruins of another person, is less real than the original. Her answer is generous and quietly radical: the self that pays attention, that cares for the dead, that feels gratitude for the beauty of statues, has its own integrity. Memory loss produced this person, but the person is real.&lt;/p&gt;
&lt;p&gt;The name, of course, comes from Giovanni Battista Piranesi, the eighteenth-century Italian artist whose &amp;quot;Carceri d&#39;Invenzione&amp;quot; (Imaginary Prisons) depict vast, impossible architectural spaces filled with staircases and arches that lead nowhere. The etchings are claustrophobic despite their scale. Infinite space rendered as confinement. Clarke takes the Carceri and makes them habitable, even lovely. Her narrator finds the House beautiful because he has no memory of anything else, no basis for comparison, and therefore no basis for complaint. A prison the prisoner loves. It is the novel&#39;s most quietly terrifying idea, and Clarke handles it with the lightest possible touch, trusting you to feel the weight yourself.&lt;/p&gt;
&lt;p&gt;What makes &lt;em&gt;Piranesi&lt;/em&gt; exceptional, and the reason I think it will last, is the quality of attention it models. Piranesi&#39;s journals are an extended practice of noticing. He names every statue. He tracks every tide. He remembers every skeleton. In a literary landscape saturated with cleverness, irony, and narrative pyrotechnics, Clarke wrote a book whose central argument is that paying attention to the world you are in, really paying attention, with care and without cynicism, is itself a moral act. It is a book about the radical sufficiency of presence. If you have ever caught yourself looking at something ordinary (a building, a riverbank, a piece of code) and felt the brief, embarrassing conviction that it was beautiful and that the beauty mattered, Clarke has written the novel about that feeling.&lt;/p&gt;
&lt;p&gt;It is 250 pages. You will read it in a day. You will think about the statues for weeks. Clarke published it sixteen years after &lt;em&gt;Jonathan Strange &amp;amp; Mr Norrell&lt;/em&gt;, which was 800 pages of baroque alternate history. &lt;em&gt;Piranesi&lt;/em&gt; is the opposite: everything stripped to the bone, every sentence load-bearing. If &lt;em&gt;Jonathan Strange&lt;/em&gt; was a cathedral, &lt;em&gt;Piranesi&lt;/em&gt; is a single perfectly proportioned room. Personally, I prefer the room.&lt;/p&gt;
</description>
      <pubDate>Mon, 09 Mar 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/piranesi/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/piranesi/</guid>
    </item>
    
    <item>
      <title>Optimal Caverna Gameplay via Formal Methods</title>
      <description>&lt;h1 id=&quot;optimal-caverna-gameplay-via-formal-methods&quot; tabindex=&quot;-1&quot;&gt;Optimal Caverna Gameplay via Formal Methods&lt;/h1&gt;
&lt;p&gt;I always win at Caverna (Uwe Rosenberg&#39;s &lt;a href=&quot;https://boardgamegeek.com/boardgame/102794/caverna-the-cave-farmers&quot;&gt;classic European worker placement&lt;/a&gt; tabletop board game). Always. But &amp;quot;always&amp;quot; just means &amp;quot;every time so far,&amp;quot; and I needed something with more mathematical permanence. So I formalized the entire game in Lean 4 and proved that my strategy is the unique weakly dominant pure strategy across every possible game configuration. My friends think this is excessive. My friends also lose at Caverna. Unrelated, I don&#39;t get invited to board game night much anymore.&lt;/p&gt;
&lt;p&gt;Caverna: The Cave Farmers is the 2013 sequel to &lt;a href=&quot;https://boardgamegeek.com/boardgame/31260/agricola&quot;&gt;Agricola&lt;/a&gt;, a game about feeding dwarfs who live in caves and do a suspicious amount of farming. You place workers, gather resources, breed animals, excavate caverns, furnish rooms, and at the end of 12 rounds the scoring formula totals up everything you&#39;ve accomplished and everything you failed to accomplish. It&#39;s a good game. It&#39;s also, in the 2-player variant, a finite deterministic perfect-information system with discrete phases, which means it&#39;s a labeled transition system, which means it&#39;s amenable to formal verification. So I did that.&lt;/p&gt;
&lt;p&gt;There is a very small mathematical model here. We just define two things: an &lt;code&gt;init&lt;/code&gt; predicate describing the starting state, and a &lt;code&gt;trans&lt;/code&gt; relation describing every legal state-to-state move. Then instead of running the game, you prove properties about it by structural induction: show the property holds at the start, show every legal transition preserves it, and you&#39;ve covered every possible game without enumerating a single one. It&#39;s the same technique used in hardware verification and protocol proofs, applied to a board game.&lt;/p&gt;
&lt;p&gt;The project is about 3,000 lines of Lean 4 spread across 19 modules: 11 definition files modeling the complete game (all 24 action spaces, all 48 unique furnishing tiles, the expedition loot system, board geometry, harvest schedule, scoring formula) and 8 theorem files containing 176 machine-checked proofs. The model covers all 2,880 possible 2-player game setups (144 card orderings times 20 harvest marker placements).&lt;/p&gt;
&lt;p&gt;The main result is that &lt;strong&gt;furnishing rush is the weakly dominant strategy&lt;/strong&gt;. It is the optimal response to every opponent, in every setup, regardless of which cards come out when or where the harvest markers land.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://sdiehl.github.io/caverna-formal/blueprint/&quot;&gt;interactive proof blueprint&lt;/a&gt; has the full derivation with a dependency graph showing how every theorem connects. The &lt;a href=&quot;https://github.com/sdiehl/caverna-formal&quot;&gt;source code&lt;/a&gt; compiles.&lt;/p&gt;
&lt;h2 id=&quot;labeled-transition-systems&quot; tabindex=&quot;-1&quot;&gt;Labeled Transition Systems&lt;/h2&gt;
&lt;p&gt;A labeled transition system (LTS) is a triple &#92;((S, A, T)&#92;) where &#92;(S&#92;) is a set of states, &#92;(A&#92;) is a set of actions, and &#92;(T &#92;subseteq S &#92;times A &#92;times S&#92;) is a transition relation specifying which state changes are legal. You start in some initial state satisfying an &lt;code&gt;init&lt;/code&gt; predicate. The system evolves by taking actions: if &#92;((s, a, s&#39;) &#92;in T&#92;), you can move from state &#92;(s&#92;) to state &#92;(s&#39;&#92;) by performing action &#92;(a&#92;). A state is reachable if there&#39;s a finite chain of transitions from an initial state to it. A property is an invariant if it holds on every reachable state.&lt;/p&gt;
&lt;p&gt;In Lean 4, the LTS is three fields:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure LTS (State : Type) (Action : Type) where
  init : State -&amp;gt; Prop
  trans : State -&amp;gt; Action -&amp;gt; State -&amp;gt; Prop
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reachability is an inductive type with two constructors: initial states are reachable, and if you can reach &#92;(s&#92;) and take action &#92;(a&#92;) to get to &#92;(s&#39;&#92;), then &#92;(s&#39;&#92;) is reachable:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;inductive Reachable (sys : LTS State Action) : State -&amp;gt; Prop where
  | init : forall s, sys.init s -&amp;gt; Reachable sys s
  | step : forall s a s&#39;, Reachable sys s -&amp;gt; sys.trans s a s&#39; -&amp;gt; Reachable sys s&#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In plain terms: the LTS is the rulebook encoded as a state transition relation. &lt;code&gt;init&lt;/code&gt; says what the starting board looks like, and &lt;code&gt;trans&lt;/code&gt; encodes every legal move as a state-to-state constraint. The entire Caverna rulebook fits in about 250 lines of Lean: a 160-line &lt;code&gt;applyPlacement&lt;/code&gt; function matching on all 24 action spaces with their effects, and a 90-line &lt;code&gt;cavernaLTS&lt;/code&gt; definition wiring up phase transitions, harvest logic, and round progression. It&#39;s a relation rather than a function because Caverna has sub-choices within actions (sow grain vs. sow vegetable, which furnishing tile to install, which loot to take), so a single action space placement can lead to multiple successor states. The relation captures all of them.&lt;/p&gt;
&lt;p&gt;Once you have &lt;code&gt;init&lt;/code&gt; and &lt;code&gt;trans&lt;/code&gt;, you prove a property holds on all reachable states without enumerating them. You prove the base case (holds on initial states) and the inductive step (every valid transition preserves it). This is strictly stronger than testing. Testing checks specific play sequences. An invariant proof covers every sequence of legal moves across every setup, including sequences no human would ever play.&lt;/p&gt;
&lt;p&gt;Here&#39;s a small fragment of the Caverna LTS to give the flavor of how a round of play works as a state machine:&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/lts.svg&quot; alt=&quot;Fragment of the game state machine for one round of play&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Each state records the player&#39;s resources and remaining dwarfs to place. Transitions correspond to action space selections. With 13 initial action spaces and 2 dwarfs, even a single round produces &#92;(13 &#92;times 12 = 156&#92;) placement sequences. Over 12 rounds with family growth, the branching factor is astronomical, but the LTS doesn&#39;t care. The structure is finite, and every path through it is covered by the invariant proofs.&lt;/p&gt;
&lt;h2 id=&quot;the-game-as-a-transition-relation&quot; tabindex=&quot;-1&quot;&gt;The Game as a Transition Relation&lt;/h2&gt;
&lt;p&gt;Board games are natural LTS candidates. In Caverna 2-player, the states are the complete game configurations (round number, phase, both players&#39; inventories, board layouts, available action spaces), the actions are dwarf placements and harvest events, and the transition relation encodes the rulebook.&lt;/p&gt;
&lt;p&gt;The game has five phases that cycle within each round:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;inductive Phase where
  | placeP1      -- player 1 places a dwarf
  | placeP2      -- player 2 places a dwarf
  | harvest      -- harvest phase (feeding, breeding, fields)
  | roundEnd     -- round cleanup, advance to next round
  | gameOver     -- game has ended
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The full game state tracks everything both players could possibly have, plus the global state of the board:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure GState where
  round : Nat
  phase : Phase
  p1 : FullPlayer
  p2 : FullPlayer
  p1IsFirst : Bool
  placementsLeft : Nat
  acc : AccState
  occupiedSpaces : List ActionSpaceId := []
  harvestSchedule : Nat -&amp;gt; HarvestEvent
  wishIsUrgent : Bool := false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The transition relation is a single function with a &lt;code&gt;match&lt;/code&gt; on &lt;code&gt;(gs.phase, act)&lt;/code&gt;. Each case is a game rule. Here&#39;s the core of the placement logic for Player 1:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def cavernaLTS (schedule : Nat -&amp;gt; HarvestEvent) :
    TransitionSystem.LTS GState GameAction where
  init := fun gs =&amp;gt; gs = initFullGState schedule
  trans := fun gs act gs&#39; =&amp;gt;
    match gs.phase, act with
    | .placeP1, .place space choice =&amp;gt;
      spaceAvailable gs.round space = true /&#92;
      spaceUnoccupied gs space = true /&#92;
      gs.placementsLeft &amp;gt; 0 /&#92;
      (let (p1&#39;, acc&#39;) := applyPlacement gs.p1 gs space choice
       let newPlacements := gs.placementsLeft - 1
       let newOccupied := space :: gs.occupiedSpaces
       if newPlacements == 0 then
         gs&#39; = { gs with p1 := p1&#39;, acc := acc&#39;,
                         phase := .harvest, placementsLeft := 0,
                         occupiedSpaces := newOccupied }
       else
         gs&#39; = { gs with p1 := p1&#39;, acc := acc&#39;,
                         phase := .placeP2,
                         placementsLeft := newPlacements,
                         occupiedSpaces := newOccupied })
    -- ... Player 2, harvest, round end, game over ...
    | _, _ =&amp;gt; False
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That last line is beautiful. &lt;code&gt;| _, _ =&amp;gt; False&lt;/code&gt; says: any action not explicitly listed is illegal. The transition relation is closed. No undefined behavior, no edge cases, no &amp;quot;the rules don&#39;t say I can&#39;t.&amp;quot; If it&#39;s not in the match, it doesn&#39;t happen.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;applyPlacement&lt;/code&gt; function is a 250-line match on all 24 action spaces, each encoding the exact effect from the rulebook. Blacksmithing forges a weapon from ore and runs an expedition. Wish for Children can only grow your family if you have a dwelling with capacity. Excavation gives you stone and lets you carve out cavern/tunnel pairs. Every sub-choice (sow grain vs. sow vegetable, build small pasture vs. large pasture, which furnishing tile to install) is a branch in the &lt;code&gt;ActionChoice&lt;/code&gt; type.&lt;/p&gt;
&lt;h2 id=&quot;the-game-timeline&quot; tabindex=&quot;-1&quot;&gt;The Game Timeline&lt;/h2&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/timeline.svg&quot; alt=&quot;Game timeline for 2-player Caverna&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Twelve rounds. Green nodes are harvest rounds where dwarfs must be fed. Two critical milestones: &amp;quot;Wish for Children&amp;quot; at round 4 enables the first family growth (from 2 dwarfs to 3), and &amp;quot;Family Life&amp;quot; at round 8 enables the second (3 to 5, eventually). One new action space reveals each round, growing from 13 to 24 available choices. The interaction between when cards flip and when harvests hit is the clock that drives the entire strategic analysis.&lt;/p&gt;
&lt;p&gt;The timeline matters because of this: without growth you get 44 total dwarf placements across all 12 rounds. With one growth at round 4, you get 47. With both growths, you get 56. That&#39;s a 27% increase in total actions from growing your family as fast as possible, and since actions are the binding constraint on everything else (scoring, food, resources), the 12-placement gap between &amp;quot;no growth&amp;quot; and &amp;quot;both growths&amp;quot; is the single most important strategic lever in the game.&lt;/p&gt;
&lt;h2 id=&quot;refinement-types%3A-making-illegal-states-unrepresentable&quot; tabindex=&quot;-1&quot;&gt;Refinement Types: Making Illegal States Unrepresentable&lt;/h2&gt;
&lt;p&gt;One of the most satisfying patterns in the formalization is using dependent types to make illegal game states impossible to construct. Weapons are the clearest example. In the physical game, weapon strength ranges from 1 to 14. You could model this as a bare &lt;code&gt;Nat&lt;/code&gt; and hope nobody passes in 0 or 15. Or you could make the type system enforce the constraint:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure Weapon where
  strength : Nat
  h__min : strength &amp;gt;= 1
  h__max : strength &amp;lt;= 14
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every &lt;code&gt;Weapon&lt;/code&gt; value carries proof that its strength is in range. This means &lt;code&gt;forgeWeapon&lt;/code&gt; must produce evidence that the forged strength is valid, and &lt;code&gt;upgradeWeapon&lt;/code&gt; must show that incrementing stays within bounds:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def forgeWeapon (oreSpent : Nat) (h__pos : oreSpent &amp;gt;= 1) : Option Weapon :=
  if h : oreSpent &amp;lt;= maxInitialWeaponStrength then
    some { strength := oreSpent
         , h__min := h__pos
         , h__max := by simp [maxInitialWeaponStrength] at h; omega }
  else
    none

def upgradeWeapon (w : Weapon) : Weapon :=
  if h : w.strength &amp;lt; maxWeaponStrength then
    { strength := w.strength + 1
    , h__min := by omega
    , h__max := by simp [maxWeaponStrength] at h; omega }
  else
    w
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;by omega&lt;/code&gt; calls are Lean&#39;s linear arithmetic tactic closing the proof obligations automatically. If &lt;code&gt;w.strength &amp;lt; 14&lt;/code&gt;, then &lt;code&gt;w.strength + 1 &amp;lt;= 14&lt;/code&gt;. If &lt;code&gt;oreSpent &amp;gt;= 1&lt;/code&gt;, then &lt;code&gt;strength &amp;gt;= 1&lt;/code&gt;. The type checker verifies this at compile time. No weapon in the entire formalization can ever have strength 0 or 15.&lt;/p&gt;
&lt;p&gt;The same pattern shows up in &lt;code&gt;RoundPlacements&lt;/code&gt;, which carries proof that all four dwarf placements within a round go to distinct action spaces:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure RoundPlacements where
  firstPlayer1  : ActionSpaceId
  secondPlayer1 : ActionSpaceId
  firstPlayer2  : ActionSpaceId
  secondPlayer2 : ActionSpaceId
  h__distinct12 : firstPlayer1 != secondPlayer1
  h__distinct13 : firstPlayer1 != firstPlayer2
  h__distinct14 : firstPlayer1 != secondPlayer2
  h__distinct23 : secondPlayer1 != firstPlayer2
  h__distinct24 : secondPlayer1 != secondPlayer2
  h__distinct34 : firstPlayer2 != secondPlayer2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Six distinctness proofs, one for each pair. You literally cannot construct a &lt;code&gt;RoundPlacements&lt;/code&gt; where two dwarfs share an action space. The game rule is baked into the type.&lt;/p&gt;
&lt;h2 id=&quot;the-weapon-system&quot; tabindex=&quot;-1&quot;&gt;The Weapon System&lt;/h2&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/weapon_path.svg&quot; alt=&quot;Weapon strength growth path&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Weapons are forged at strength 1 through 8 (costing that many ore), then grow by +1 per expedition, capping at 14. The blue range is forgeable; the peach range requires expedition grinding. Cattle loot unlocks at strength 9, which means even if you forge at max (8 ore), you still need at least one expedition before you can get cattle.&lt;/p&gt;
&lt;p&gt;The loot table is an inductive type with minimum strength requirements for each item:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def LootItem.minStrength : LootItem -&amp;gt; Nat
  | .allWeaponsPlus1    =&amp;gt; 1
  | .dog                =&amp;gt; 1
  | .wood               =&amp;gt; 1
  | .grain              =&amp;gt; 2
  | .sheep              =&amp;gt; 2
  | .stone              =&amp;gt; 3
  | .donkey             =&amp;gt; 3
  | .ore                =&amp;gt; 4
  | .wildBoar           =&amp;gt; 4
  | .stableFree         =&amp;gt; 5
  | .gold2              =&amp;gt; 6
  | .furnishCavern      =&amp;gt; 7
  | .buildFencesCheap   =&amp;gt; 8
  | .cattle             =&amp;gt; 9
  | .dwelling           =&amp;gt; 10
  | .sow                =&amp;gt; 11
  | .breedTwoTypes      =&amp;gt; 12
  | .furnishCavernAgain =&amp;gt; 14
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At strength 1 you can loot a dog or a stick. At strength 14 you can furnish a second cavern for free. The loot count at key strengths: 3 at strength 1, 13 at strength 8, 18 at strength 14. That&#39;s 5 premium items (cattle, dwelling, sow, breed, second furnish) locked behind the expedition grind. Whether the ore investment is worth it is the core question of the weapon rush archetype, and the answer turns out to be: no, not quite.&lt;/p&gt;
&lt;h2 id=&quot;the-universal-food-crisis&quot; tabindex=&quot;-1&quot;&gt;The Universal Food Crisis&lt;/h2&gt;
&lt;p&gt;Every strategy in Caverna must solve the same problem before anything else. Both players face a food deficit at the first harvest:&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/food_flow.svg&quot; alt=&quot;Food conversion network&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Player 1 starts with 1 food and needs 4 (2 dwarfs times 2 food each). Player 2 starts with 2 food and needs 4. The gaps are 3 and 2 respectively. This is proven as &lt;code&gt;universal__food__crisis&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem universal_food_crisis :
    feedingCost 2 0 - startingFoodP1 = 3 /&#92;
    feedingCost 2 0 - startingFoodP2 = 2 := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implication (&lt;code&gt;food__crisis__shapes__all__strategies&lt;/code&gt;) is that every viable archetype must spend its first few actions on food acquisition. There&#39;s no &amp;quot;skip feeding and go straight to scoring&amp;quot; option. The begging marker penalty is &#92;(-3&#92;) points each, and the theorem &lt;code&gt;absolute__floor__is__neg55&lt;/code&gt; shows that a player who takes zero actions across all rounds scores &#92;(-55&#92;). The food crisis isn&#39;t optional; it&#39;s structural.&lt;/p&gt;
&lt;p&gt;The food conversion network itself is a delightful mess of exchange rates. Cattle gives 4 food per animal, wild boar gives 3, sheep gives 2, grain gives 1, vegetables give 2, gold converts lossily at &#92;(n-1&#92;) (1 gold is wasted as overhead), and rubies are emergency food at 2+ each. And then there are donkeys, which have a superlinear pairing bonus that I spent an embarrassing amount of time formalizing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def donkeyFoodValue (n : Nat) : Nat :=
  let pairs := n / 2
  let remainder := n % 2
  pairs * 3 + remainder * 1

theorem donkey__superlinear :
    donkeyFoodValue 2 &amp;gt; donkeyFoodValue 1 + donkeyFoodValue 1 := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two donkeys together yield 3 food, but individually they&#39;d give 1 + 1 = 2. The whole is greater than the sum of its parts. Uwe Rosenberg almost certainly didn&#39;t think about this as a super-additivity property of a set function, but that&#39;s what it is, and Lean can prove it.&lt;/p&gt;
&lt;h2 id=&quot;the-feeding-cascade&quot; tabindex=&quot;-1&quot;&gt;The Feeding Cascade&lt;/h2&gt;
&lt;p&gt;The feeding function is the core survival mechanic. When harvest hits, each dwarf eats 2 food (offspring eat 1). If you don&#39;t have enough food, the deficit cascades through your resources: try food first, then convert grain (1 food each), then convert vegetables (2 food each), then take begging markers for anything remaining.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def FullPlayer.feed (p : FullPlayer) : FullPlayer :=
  let cost := p.dwarfs * 2 + p.offspring * 1
  if p.food &amp;gt;= cost then
    { p with food := p.food - cost }
  else
    let deficit := cost - p.food
    let p&#39; := { p with food := 0 }
    let grainUsed := min p&#39;.grain deficit
    let p&#39;&#39; := { p&#39; with grain := p&#39;.grain - grainUsed }
    let deficit&#39; := deficit - grainUsed
    let vegUsed := min p&#39;&#39;.vegetables (deficit&#39; / 2 + deficit&#39; % 2)
    let vegFood := min (vegUsed * 2) deficit&#39;
    let p&#39;&#39;&#39; := { p&#39;&#39; with vegetables := p&#39;&#39;.vegetables - vegUsed }
    let deficit&#39;&#39; := deficit&#39; - vegFood
    { p&#39;&#39;&#39; with beggingMarkers := p&#39;&#39;&#39;.beggingMarkers + deficit&#39;&#39; }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And a normal harvest is the composition of three phases in one elegant pipeline:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def FullPlayer.normalHarvest (p : FullPlayer) : FullPlayer :=
  p.fieldPhase.feed.breedingPhase
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Field phase harvests your sown crops. Feed pays the food cost (or generates begging markers). Breeding adds one animal per type that has at least two. The order matters: fields produce food before feeding, and breeding happens after, so newborn animals don&#39;t need to be fed in the same round they appear. This ordering rule from page 6 of the Caverna rulebook is encoded in the function composition.&lt;/p&gt;
&lt;h2 id=&quot;furnishing-tiles-and-the-bonuscontext&quot; tabindex=&quot;-1&quot;&gt;Furnishing Tiles and the BonusContext&lt;/h2&gt;
&lt;p&gt;There are 48 furnishing tiles in the game, each with resource costs, base victory points, and (for some) end-game bonus scoring formulas that depend on your final board state. The &lt;code&gt;BonusContext&lt;/code&gt; struct captures everything a furnishing tile might look at when computing its bonus:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure BonusContext where
  stoneInSupply        : Nat := 0
  oreInSupply          : Nat := 0
  sheepCount           : Nat := 0
  cattleCount          : Nat := 0
  numAdjacentDwellings : Nat := 0
  numArmedDwarfs       : Nat := 0
  numDwarfs            : Nat := 0
  rubyCount            : Nat := 0
  grainCount           : Nat := 0
  vegCount             : Nat := 0
  farmAnimalCount      : Nat := 0
  hasAnyWeapon         : Bool := false
  allDwarfsArmed       : Bool := false
  numYellowTagTiles    : Nat := 0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bonus point function is a match on tile ID that implements every scoring formula from the Caverna appendix:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def furnishingBonusPoints (fid : FurnishingId) (ctx : BonusContext) : Nat :=
  match fid with
  | .stoneStorage   =&amp;gt; ctx.stoneInSupply
  | .oreStorage     =&amp;gt; ctx.oreInSupply / 2
  | .weavingParlor  =&amp;gt; ctx.sheepCount / 2
  | .milkingParlor  =&amp;gt; ctx.cattleCount
  | .stateParlor    =&amp;gt; ctx.numAdjacentDwellings * 4
  | .mainStorage    =&amp;gt; ctx.numYellowTagTiles * 2
  | .weaponStorage  =&amp;gt; ctx.numArmedDwarfs * 3
  | .suppliesStorage =&amp;gt; if ctx.allDwarfsArmed then 8 else 0
  | .broomChamber   =&amp;gt; if ctx.numDwarfs &amp;gt;= 6 then 10
                        else if ctx.numDwarfs &amp;gt;= 5 then 5
                        else 0
  | .prayerChamber  =&amp;gt; if ctx.hasAnyWeapon then 0 else 8
  | _ =&amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Prayer Chamber is particularly nasty. It gives you 8 free points, but only if none of your dwarfs have weapons. The moment you forge a single weapon, it drops to zero. This creates a genuine strategic dilemma, formalized and proved:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem prayer__chamber__vs__weapons :
    furnishingBonusPoints .prayerChamber {} = 8 /&#92;
    furnishingBonusPoints .prayerChamber { hasAnyWeapon := true } = 0 :=
  by constructor &amp;lt;;&amp;gt; native_decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The furnishing rush archetype gets its power from stacking compatible bonus tiles: Office Room overhangs, State Parlor for +4 per adjacent dwelling (up to +16), Broom Chamber for +5 or +10 based on dwarf count, Prayer Chamber for +8 (no weapons). These bonuses compound. A player with 5 dwarfs, 4 adjacent dwellings, and no weapons can pull +39 bonus points from three tiles. That&#39;s why furnishing rush has the highest ceiling.&lt;/p&gt;
&lt;h2 id=&quot;the-strategy-space&quot; tabindex=&quot;-1&quot;&gt;The Strategy Space&lt;/h2&gt;
&lt;p&gt;With the LTS, scoring function, and food economy in place, the next question is: what are the high-level plans a player can actually follow? The transition relation defines roughly &#92;(10^{30}&#92;) possible play sequences, but the overwhelming majority of them are nonsense. Consider a player who forges a weapon in round 1 (spending precious ore), then never goes on an expedition, sows a field in round 3 but never harvests grain from it, builds a pasture in round 5 but never acquires an animal, and spends round 8 taking Starting Player for no reason. This player ends the game with a weapon they never used, an empty pasture, a field of rotting grain, and a final score somewhere around 20 points after begging penalties. The LTS covers this path. The invariant proofs hold over it. It is a legal play sequence, and it is also exactly the kind of play sequence that a five-year-old would produce by picking action spaces at random.&lt;/p&gt;
&lt;p&gt;The vast majority of the state space looks like this: diffuse, uncommitted paths where the player dabbles in everything and commits to nothing, hemorrhaging tempo and food while accumulating resources that never convert to points. Only a handful of coherent &amp;quot;channels&amp;quot; through the action space graph lead to competitive scores against a rational opponent. A strategy archetype is a consistent pattern of action space usage across the full 12-round game. I identified eight by examining which action spaces and furnishing tiles naturally cluster together: if two actions compete for the same resources or unlock the same scoring categories, they belong to the same archetype. For example, Blacksmithing, Expedition, and weapon-dependent loot all form the weapon rush cluster, while Excavation, Housework, and bonus-scoring furnishings form the furnishing rush cluster. The classification was manual, but the Lean proofs validate it: every archetype&#39;s score estimates are derived from the formalized game rules, and the dominance relations are machine-checked over the resulting payoff matrix.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;inductive StrategyArchetype where
  | furnishingRush
  | weaponRush
  | animalHusbandry
  | miningHeavy
  | balanced
  | peacefulFarming
  | rubyEconomy
  | peacefulCaveEngine
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But why eight? The answer comes from the structure of the game&#39;s scoring channels. A scoring channel is a resource-to-points pathway through the action spaces: the furnishing channel runs through Excavation and Housework into furnished caverns, the weapon channel runs through Blacksmithing into expeditions and loot, and so on. There are exactly six:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;inductive ScoringChannel where
  | furnishing        -- Excavation + Housework -&amp;gt; caverns -&amp;gt; furnishing tiles
  | weaponExpedition  -- Blacksmithing + Adventure -&amp;gt; weapons -&amp;gt; loot
  | agriculture       -- Clearing + Sustenance -&amp;gt; fields -&amp;gt; crops
  | animalBreeding    -- Sheep Farming + Donkey Farming -&amp;gt; pastures -&amp;gt; animals
  | mining            -- Ore/Ruby Mine Construction -&amp;gt; mines
  | economy           -- Starting Player + Ore Trading + Ruby Mining -&amp;gt; gold/rubies
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each archetype commits to a primary channel, and the eight archetypes cover all six channels (&lt;code&gt;all__channels__covered&lt;/code&gt;). Some channels conflict over shared resources: weapon/expedition and mining both need ore, furnishing and mining both need stone. The &lt;code&gt;channelsConflict&lt;/code&gt; function encodes these tensions. With 47 productive actions per game and each channel needing at least 6 dedicated actions to reach competitive scoring, a player can invest in at most 2 channels seriously (&lt;code&gt;budget__bounds__channels&lt;/code&gt;). The archetypes are the maximal compatible channel combinations: 6 single-primary archetypes plus 2 hybrids (balanced spreads across 3 channels, peaceful cave engine uses weapons for food rather than scoring).&lt;/p&gt;
&lt;p&gt;The exhaustivity proof (&lt;code&gt;archetype__channel__surjection&lt;/code&gt;) shows every channel has at least one archetype as its primary. If you&#39;re going to score points, you have to go through a channel, and every channel is represented. No viable strategy falls outside the classification.&lt;/p&gt;
&lt;p&gt;Each archetype has an estimated scoring ceiling (best case across all setups) and floor (worst case):&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/score_intervals.svg&quot; alt=&quot;Score estimate ranges for the eight strategy archetypes&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Furnishing rush tops the ceiling at 140 and ties for the highest floor at 60. Weapon rush matches the floor but caps at 120. The others trail off. The interval chart makes the dominance relationships immediately visible: furnishing rush&#39;s bar extends further right (higher ceiling) than any other archetype, and its left endpoint (floor) is as good as anyone&#39;s.&lt;/p&gt;
&lt;h2 id=&quot;the-dominance-hierarchy&quot; tabindex=&quot;-1&quot;&gt;The Dominance Hierarchy&lt;/h2&gt;
&lt;p&gt;Before building the payoff matrix, we can establish partial dominance from the score estimates alone. If strategy &#92;(s&#92;) has both a higher ceiling and a higher floor than strategy &#92;(t&#92;) (with at least one strict inequality), then &#92;(s&#92;) dominates &#92;(t&#92;) in score estimates.&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/hasse.svg&quot; alt=&quot;Dominance partial order over strategy archetypes&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Solid green arrows show dominance via score estimates (higher ceiling and floor). Dashed green arrows indicate dominance established only through the full payoff matrix. Animal Husbandry and Mining Heavy are incomparable in score estimates (one has a higher ceiling, the other a higher floor), but both are weakly dominated by Furnishing Rush in the payoff matrix.&lt;/p&gt;
&lt;p&gt;The incomparability is proven constructively:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem incomparable__strategies__exist :
    not (dominates .miningHeavy .animalHusbandry) /&#92;
    not (dominates .animalHusbandry .miningHeavy) := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;the-payoff-matrix&quot; tabindex=&quot;-1&quot;&gt;The Payoff Matrix&lt;/h2&gt;
&lt;p&gt;The next question is what happens when two players with potentially different archetypes collide over the shared action spaces. You model this as an &#92;(8 &#92;times 8&#92;) payoff matrix &#92;(M&#92;) where &#92;(M_{ij}&#92;) is the estimated score for the row player when row plays archetype &#92;(i&#92;) and column plays archetype &#92;(j&#92;).&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/payoff_matrix.svg&quot; alt=&quot;The 8x8 payoff matrix&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;The green border marks the Furnishing Rush row (weakly dominant). Blue borders highlight the diagonal (mirror matchups, always depressed). Cell color intensity reflects payoff magnitude: light green (55) through coral (135).&lt;/p&gt;
&lt;p&gt;In Lean, the matrix is a function from &lt;code&gt;Fin 8 -&amp;gt; Fin 8 -&amp;gt; Int&lt;/code&gt; with all 64 entries hardcoded from the strategy analysis:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def payoffMatrix : Fin 8 -&amp;gt; Fin 8 -&amp;gt; Int
  | 0, 0 =&amp;gt;  85 | 0, 1 =&amp;gt; 130 | 0, 2 =&amp;gt; 135 | 0, 3 =&amp;gt; 130
  | 0, 4 =&amp;gt; 125 | 0, 5 =&amp;gt; 135 | 0, 6 =&amp;gt; 135 | 0, 7 =&amp;gt; 130
  | 1, 0 =&amp;gt;  80 | 1, 1 =&amp;gt;  75 | 1, 2 =&amp;gt; 100 | 1, 3 =&amp;gt;  85
  | 1, 4 =&amp;gt;  95 | 1, 5 =&amp;gt; 105 | 1, 6 =&amp;gt; 100 | 1, 7 =&amp;gt;  85
  -- ... 48 more entries ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A word on what &amp;quot;estimated&amp;quot; means here. The matrix entries are derived from the formalized game rules (scoring function, food costs, action budgets), not from Monte Carlo simulation or guesswork. But they are estimates in the sense that I haven&#39;t exhaustively solved every possible 47-move sequence within each archetype. How sensitive is the result? The &lt;code&gt;min__dominance__margin__is__5&lt;/code&gt; theorem proves that the minimum gap between the furnishing rush row and the next-best entry in each column is exactly 5 points (occurring in the mirror matchup column). Perturbing any single entry by less than 5 points cannot flip the dominance relation. Even a 10-point swing in a single cell would only affect one column, not the global result. The Nash equilibrium is more sensitive (it depends on the diagonal), but the mirror matchup at 85 would need to drop below the next-best response value of 80 before the equilibrium shifts. The downstream proofs are machine-checked over the matrix as stated, so the formal guarantees are exact conditional on these entries.&lt;/p&gt;
&lt;p&gt;We can do better than conditional. Replace each scalar entry with a closed interval bounding the true payoff. Mirror matchups get tight bounds (the point estimate &#92;(&#92;pm 2&#92;), since both players execute the same plan and contention is symmetric). Cross-archetype matchups get wider bounds (&#92;(&#92;pm 5&#92;), reflecting uncertainty in cross-strategy interaction). The interval payoff matrix looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure PayoffInterval where
  lo : Int
  hi : Int
  valid : lo &amp;lt;= hi

def intervalPayoff : Fin 8 -&amp;gt; Fin 8 -&amp;gt; PayoffInterval
  | 0, 0 =&amp;gt; { lo := 83, hi := 87, valid := by omega }   -- mirror: eps=2
  | 0, 1 =&amp;gt; { lo := 125, hi := 135, valid := by omega }  -- vs weapon: eps=5
  -- ... 62 more entries ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Robust weak dominance asks: for every column and every non-furnishing alternative, does the furnishing rush &lt;em&gt;lower bound&lt;/em&gt; meet or exceed the alternative&#39;s &lt;em&gt;upper bound&lt;/em&gt;? If yes, then furnishing rush dominates for ALL true payoff matrices within these intervals, not just the point estimates.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem robust_weak_dominance :
    forall (col : Fin 8) (alt : Fin 8), alt != 0 -&amp;gt;
      (intervalPayoff 0 col).lo &amp;gt;= (intervalPayoff alt col).hi := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It passes. The tightest cell is column 0 (mirror matchup): furnishing rush &#92;([83, 87]&#92;) vs. weapon rush &#92;([77, 83]&#92;). The margin is &#92;(83 - 83 = 0&#92;), a weak tie at the boundary. All other columns have margins of 20 or more (&lt;code&gt;non__mirror__columns__robust&lt;/code&gt;). If the error bounds were widened by just 1 point, column 0 would fail (&lt;code&gt;fragility__column__0&lt;/code&gt;), which tells us exactly where the result is most fragile and what the tolerance is.&lt;/p&gt;
&lt;p&gt;The interval analysis also gives us robust welfare bounds. Nash welfare lies in &#92;([166, 174]&#92;) and the social optimum in &#92;([200, 220]&#92;). Even in the best case for selfish play and worst case for cooperation, the social optimum still exceeds Nash welfare (&lt;code&gt;robust__price__of__anarchy&lt;/code&gt;). The prisoner&#39;s dilemma structure is not an artifact of the point estimates.&lt;/p&gt;
&lt;p&gt;The first row is at least as large as every other row in every column. That&#39;s it. That&#39;s the whole result. In game theory this property is called weak dominance: a strategy &#92;(&#92;sigma^*&#92;) is weakly dominant if for all opponent strategies &#92;(o&#92;) and all alternative strategies &#92;(&#92;sigma&#92;),&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
M(&#92;sigma^*, o) &#92;geq M(&#92;sigma, o)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The Lean proof is &lt;code&gt;furnishing__rush__weakly__dominant&lt;/code&gt;, and it says exactly this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem furnishing_rush_weakly_dominant :
    forall (row : Fin 8) (col : Fin 8),
      payoffMatrix 0 col &amp;gt;= payoffMatrix row col := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For all opponents, for all alternatives, the furnishing rush payoff is greater than or equal. The proof is &lt;code&gt;decide&lt;/code&gt;: Lean&#39;s kernel checks all 64 cells. Nobody has to trust my arithmetic.&lt;/p&gt;
&lt;h2 id=&quot;the-contention-effect&quot; tabindex=&quot;-1&quot;&gt;The Contention Effect&lt;/h2&gt;
&lt;p&gt;The diagonal of the payoff matrix is always depressed relative to the off-diagonal entries:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem diagonal__always__depressed :
    forall (i : Fin 8), exists (j : Fin 8), j /= i /&#92;
      payoffMatrix i j &amp;gt; payoffMatrix i i := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every mirror matchup scores below at least one non-mirror matchup in the same row. This is the contention effect: when both players pursue the same archetype, they fight over the same action spaces. Furnishing rush mirrors compete for excavation and housework. Weapon rush mirrors fight over blacksmithing. Mining mirrors clash on ore mine construction.&lt;/p&gt;
&lt;p&gt;The game punishes sameness. It&#39;s just that the punishment for sameness (85) is still less painful than the punishment for picking something worse (60 to 80 against an opponent who picked furnishing rush).&lt;/p&gt;
&lt;h2 id=&quot;nash-equilibrium-and-the-prisoner&#39;s-dilemma&quot; tabindex=&quot;-1&quot;&gt;Nash Equilibrium and the Prisoner&#39;s Dilemma&lt;/h2&gt;
&lt;p&gt;From weak dominance, the game theory falls out like dominoes. The best response function &#92;(&#92;text{BR} : &#92;text{Strategies} &#92;to &#92;text{Strategies}&#92;) maps each opponent strategy to the row-maximizer in the corresponding column. Since furnishing rush achieves the column maximum everywhere, BR is the constant function:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{BR}(x) = &#92;text{FurnishingRush} &#92;quad &#92;forall x&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;A Nash equilibrium is a fixed point of the joint best-response correspondence: a pair &#92;((a, b)&#92;) where &#92;(a = &#92;text{BR}(b)&#92;) and &#92;(b = &#92;text{BR}(a)&#92;). Since BR is constant, the only fixed point is (FurnishingRush, FurnishingRush). Existence and uniqueness in three lines:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem exactly__one__nash__equilibrium :
    (exists a b, isNashEquilibrium a b) /&#92;
    (forall a b, isNashEquilibrium a b -&amp;gt;
      a = .furnishingRush /&#92; b = .furnishingRush) :=
  &amp;lt;&amp;lt;.furnishingRush, .furnishingRush, furnishing_mirror_is_nash&amp;gt;,
   furnishing_mirror_unique_nash&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The uniqueness proof works by case-splitting on all 64 strategy pairs and observing that only one satisfies the Nash condition:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem furnishing__mirror__unique__nash :
    forall a b : StrategyArchetype,
      isNashEquilibrium a b -&amp;gt;
        a = .furnishingRush /&#92; b = .furnishingRush := by
  intro a b h
  have h1 := h.1; have h2 := h.2
  cases a &amp;lt;;&amp;gt; cases b &amp;lt;;&amp;gt; simp_all [bestResponse, isNashEquilibrium]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is exactly one pure Nash equilibrium and it is the one where both players do the same thing. Which is a little tragic.&lt;/p&gt;
&lt;h2 id=&quot;the-price-of-anarchy&quot; tabindex=&quot;-1&quot;&gt;The Price of Anarchy&lt;/h2&gt;
&lt;p&gt;Both players score 85 in the Nash equilibrium. If they could somehow coordinate on different strategies (say furnishing rush versus animal husbandry), the combined welfare would be &#92;(135 + 75 = 210&#92;) instead of &#92;(85 + 85 = 170&#92;).&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/welfare.svg&quot; alt=&quot;Nash welfare vs. social optimum&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;The price of anarchy is the ratio of the social optimum to the Nash welfare:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{PoA} = &#92;frac{210}{170} = &#92;frac{21}{17} &#92;approx 1.24&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Selfish play costs about 19% of the social optimum. The Lean proof is &lt;code&gt;decide&lt;/code&gt;, because it&#39;s just arithmetic:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem price__of__anarchy__ratio :
    socialOptimumValue = 210 /&#92; nashWelfare = 170 /&#92;
    210 * 17 = 170 * 21 := by decide
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The depressing implication is that both players would prefer the other person to play something different, but neither can unilaterally deviate without making themselves worse off. You&#39;re stuck at 85 each, staring across the table at someone who also read this blog post.&lt;/p&gt;
&lt;h2 id=&quot;the-scoring-function&quot; tabindex=&quot;-1&quot;&gt;The Scoring Function&lt;/h2&gt;
&lt;p&gt;A natural question is where the scores can actually land. The end-game scoring function totals up every positive and negative contribution:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def FullPlayer.score (p : FullPlayer) : Int :=
  let animals := (p.dogs + p.sheep + p.donkeys +
                  p.wildBoars + p.cattle : Int)
  let grainPts := (((p.grain + p.fieldsWithGrain * 3) + 1) / 2 : Int)
  let vegPts := ((p.vegetables + p.fieldsWithVeg * 2) : Int)
  let rubyPts := (p.rubies : Int)
  let dwarfPts := ((p.dwarfs + p.offspring) : Int)
  let pasturePts := (p.smallPastures * 2 + p.largePastures * 4 : Int)
  let minePts := (p.oreMines * 3 + p.rubyMines * 4 : Int)
  let goldPts := (p.gold : Int)
  let furnPts := p.furnishings.foldl (fun acc fid =&amp;gt;
    acc + ((furnishingSpec fid).basePoints : Int)) (0 : Int)
  let missingTypes :=
    (if p.sheep == 0 then 1 else 0) + (if p.donkeys == 0 then 1 else 0) +
    (if p.wildBoars == 0 then 1 else 0) + (if p.cattle == 0 then 1 else 0)
  let unusedPenalty := (p.unusedMountain + p.unusedForest : Int)
  let beggingPenalty := (p.beggingMarkers * 3 : Int)
  let missingPenalty := (missingTypes * 2 : Int)
  animals + grainPts + vegPts + rubyPts + dwarfPts + pasturePts +
  minePts + goldPts + furnPts - unusedPenalty - beggingPenalty - missingPenalty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The theoretical floor is &#92;(-55&#92;) points: a player who takes zero actions across all rounds gets +2 for their two starting dwarfs, &#92;(-8&#92;) for missing all four farm animal types, &#92;(-22&#92;) for 22 unused board spaces, and &#92;(-27&#92;) from 9 begging markers at &#92;(-3&#92;) points each. The theoretical ceiling is 202, computed by summing the independent maxima of every scoring category. But you can&#39;t reach 202, because every scoring category competes for the same 56 dwarf placements over 12 rounds. You cannot simultaneously furnish 12 caverns, build 5 mines, breed 20 animals, and sow 10 fields. The action budget is the binding constraint. The practical ceiling is around 140, achievable by furnishing rush when uncontested.&lt;/p&gt;
&lt;p&gt;The theoretical range is 195 points (from &#92;(-55&#92;) to 140), but the &#92;(-55&#92;) floor requires literally doing nothing for 12 rounds, so it&#39;s not strategically meaningful. The practical floor across all archetypes is around 45 (peaceful farming in the worst setup), giving a practical range of about 95 points. The dominant strategy&#39;s own range is 80 points (60 to 140), proven as &lt;code&gt;dominant__strategy__variance&lt;/code&gt;. The safety margin (&lt;code&gt;dominant__strategy__safety__margin&lt;/code&gt;) is 115 points above the theoretical floor, but the more relevant number is the gap between furnishing rush&#39;s worst case (60) and the worst viable archetype&#39;s worst case (45): even in the most adversarial setup, choosing correctly gains you at least 15 points.&lt;/p&gt;
&lt;h2 id=&quot;degenerate-combos&quot; tabindex=&quot;-1&quot;&gt;Degenerate Combos&lt;/h2&gt;
&lt;p&gt;The formalization surfaced several combos that look broken in isolation. Four stand out.&lt;/p&gt;
&lt;p&gt;The Beer Parlor converts grain to gold at a 3:2 ratio before scoring: every 2 grain becomes 3 gold. Without it, 20 grain scores 10 points (the grain formula is &#92;(&#92;lceil n/2 &#92;rceil&#92;)). With it, the same 20 grain becomes 30 gold. That&#39;s a 3x multiplier on the base grain value, and the gold stacks with every other scoring category. The &lt;code&gt;beer__parlor__max__gold&lt;/code&gt; theorem proves &lt;code&gt;beerParlorGold 20 = 30&lt;/code&gt;. The catch is accumulating 20 grain: you need aggressive sowing across multiple rounds, which means burning actions on Slash-and-Burn and Sustenance instead of excavating caverns.&lt;/p&gt;
&lt;p&gt;Dogs have no cap on sheep-watching: &#92;(n&#92;) dogs on one meadow guard &#92;(n+1&#92;) sheep. Stack 10 dogs and a Weaving Parlor and you&#39;re looking at 51 points from a single tile combo. The catch is that acquiring 10 dogs requires roughly 6 expedition actions (dogs are strength-1 loot, so they&#39;re easy to get but each expedition burns an action), and those 6 actions aren&#39;t building caverns or growing your family.&lt;/p&gt;
&lt;p&gt;The Writing Chamber prevents up to 7 points of losses for 2 stone. This sounds like a license to ignore entire scoring categories, and it is, except the categories you&#39;d skip (unused spaces, missing animal types) are exactly the ones that furnishing rush covers naturally. The players who benefit most from Writing Chamber are the ones playing badly enough to accumulate large penalties, and at that point the cap at 7 means it barely dents a truly neglected board.&lt;/p&gt;
&lt;p&gt;The Prayer Chamber is the most elegant. It gives 8 points for zero resource investment, but the bonus evaporates the moment any dwarf forges a weapon. The &lt;code&gt;prayer__chamber__vs__weapons&lt;/code&gt; theorem proves this is a hard binary: 8 or 0, nothing in between. It creates a genuine strategic fork (peaceful play vs. expedition play) that the rest of the game&#39;s design carefully preserves.&lt;/p&gt;
&lt;p&gt;None of these breaks the dominant strategy. Every combo faces the same binding constraint: 56 total actions with full family growth. You can produce flashy numbers in one scoring category, but you can&#39;t cover all of them. The vanilla furnishing rush, with its balanced approach to board coverage, family growth, and furnishing synergies, remains the ceiling. The combos are features, not bugs: they&#39;re what make the game worth replaying even after you know the dominant strategy, because they give you something to pivot into when the standard lines are blocked.&lt;/p&gt;
&lt;h2 id=&quot;is-the-game-solved%3F&quot; tabindex=&quot;-1&quot;&gt;Is the Game Solved?&lt;/h2&gt;
&lt;p&gt;A serious mathematician would call this &amp;quot;a formal analysis of a model, with the caveat that the model&#39;s parameters are estimated rather than computed.&amp;quot; That is a legitimate and common pattern in applied math, operations research, and mechanism design. The proofs are machine-checked and airtight, but they are conditional on the payoff matrix. The theorems say: IF the payoff matrix is this, THEN furnishing rush is weakly dominant, the Nash equilibrium is unique, and the price of anarchy is 21/17. They do not say: the true expected score of an optimally-played furnishing rush against an optimally-played animal husbandry opponent is exactly 135. That number is an estimate derived from the formalized action economy, not from exhaustive search of the game tree.&lt;/p&gt;
&lt;p&gt;How confident should you be in the estimate? The &lt;code&gt;min__dominance__margin__is__5&lt;/code&gt; theorem proves the narrowest gap between furnishing rush and its closest competitor is 5 points. If the estimation methodology has a systematic bias of less than 5 points (plausible, given that the estimates are derived from the same scoring functions and action budgets used in actual play), the qualitative result holds. A correlated shift of 5+ points across an entire row of the matrix could break weak dominance in the mirror column, but would require the methodology to consistently overestimate furnishing rush&#39;s performance in self-contention scenarios. Given that the mirror penalty (85 vs. 130+ off-diagonal) already models severe contention, this seems unlikely.&lt;/p&gt;
&lt;p&gt;There is a stronger structural argument that sidesteps the estimation question entirely. Each scoring channel has a maximum yield per action: furnishing converts actions to points at 6 points per action (excavation + furnishing tile with bonus multipliers), weapon/expedition at 4, and everything else at 3 or below. An &lt;code&gt;ActionAllocation&lt;/code&gt; distributes the 37 productive actions (47 total placements minus ~10 food obligations) across the 6 channels. The &lt;code&gt;no__better__allocation__exists&lt;/code&gt; theorem proves that for any valid allocation, the total score cannot exceed the furnishing-maximizing allocation&#39;s ceiling of 222 raw points (37 * 6). Every action diverted from furnishing to the next-best channel (weapons) loses exactly 2 points; diversion to agriculture or mining loses 3. A plausible hybrid allocation of 20 furnishing + 17 weapons achieves at most 188 points, a 34-point deficit. No reallocation of the action budget can close this gap. The action economy structurally forbids a better strategy from existing.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem no_better_allocation_exists :
    forall a : ActionAllocation, a.valid -&amp;gt;
      a.rawScore &amp;lt;= furnishingMaxRawScore := by
  intro a h__valid
  simp only [ActionAllocation.rawScore, furnishingMaxRawScore,
             furnishingMaxAllocation, channelYieldPerAction,
             productiveActionsEstimate, totalPlacementsOneGrowthRound4]
  simp only [ActionAllocation.valid, ActionAllocation.totalActions,
             productiveActionsEstimate, totalPlacementsOneGrowthRound4] at h__valid
  omega
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The proof is four lines. &lt;code&gt;omega&lt;/code&gt; closes it because every channel yield is at most 6, so any allocation over 37 actions scores at most 37 * 6 = 222. This result is independent of the payoff matrix. It depends only on the scoring rules and the action budget, both of which are formalized from the rulebook. Even if every payoff estimate were wrong by 50 points, this theorem would still hold: the furnishing channel has the highest marginal return per action, and the finite action budget makes that advantage insurmountable.&lt;/p&gt;
&lt;p&gt;At the archetype level, yes, the game is solved. For any 2-player setup, regardless of which of the 2,880 configurations you draw, the model says the optimal pure strategy is furnishing rush. The proof does not depend on any specific card ordering or harvest marker placement. What I haven&#39;t done is compute the exact sequence of dwarf placements for each specific setup. That would require solving a game tree with roughly &#92;(10^{30}&#92;) nodes. But the archetype-level result is the one that matters for actual play. You know the plan. The within-archetype decisions (which cavern to excavate first, which furnishing tile to prioritize) are tactical, not strategic. They don&#39;t change the answer.&lt;/p&gt;
&lt;p&gt;The 3+ player variants remain open. The payoff matrix becomes a rank-3 tensor with 512 entries, action space contention shifts from binary to combinatorial, and the pure dominance result almost certainly fails, since furnishing tile scarcity under three-way competition makes mixed equilibria the likely outcome. If someone wants to formalize that, I would love to read the proof.&lt;/p&gt;
&lt;h2 id=&quot;play-the-dominant-strategy&quot; tabindex=&quot;-1&quot;&gt;Play the Dominant Strategy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Play furnishing rush.&lt;/strong&gt; Excavate aggressively in rounds 1 through 4. Get Office Room early for the overhang bonuses. Get State Parlor for +4 per dwelling. Grow to 5 dwarfs and pick up Broom Chamber for +10 bonus. If your opponent does something different, you score 125 to 135 and they score 60 to 105. If your opponent also plays furnishing rush, you both land at 85, which is worse than the cooperative optimum of 210 but better than any unilateral deviation. You&#39;ll sit there at the table, both of you excavating caverns as fast as you can, both knowing that one of you could sacrifice 10 points to give the other 50, but neither willing to be the one who blinks.&lt;/p&gt;
&lt;p&gt;Prisoner&#39;s dilemma in a cave. Uwe Rosenberg probably didn&#39;t intend this, but the formal analysis says it&#39;s there.&lt;/p&gt;
&lt;p&gt;The code is built on Lean 4 v4.28.0 with Mathlib, modeling all 24 action spaces, all 48 furnishing tiles, the complete expedition loot table, the board grids, and the full 12-round 2-player harvest schedule. I think Caverna is a beautifully designed game, which is exactly why it was satisfying to find that even under formal analysis the strategic structure holds together so well. A badly designed game would have a trivially dominant strategy that makes the game boring. Caverna&#39;s dominant strategy comes with a 19% welfare tax on the mirror matchup, which means the game is always more interesting when your opponent does something unexpected (the Nash equilibrium is rarely where the fun is, in games or in life), which means in practice people don&#39;t always play the dominant strategy, which means the game stays fun.&lt;/p&gt;
&lt;p&gt;Good game design, it turns out, is robust to being solved. Now you know the optimal play. Whether you follow it is, thankfully, not a theorem.&lt;/p&gt;
</description>
      <pubDate>Sat, 21 Feb 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/caverna/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/caverna/</guid>
    </item>
    
    <item>
      <title>Can Opus 4.6 do Category Theory in Lean?</title>
      <description>&lt;h1 id=&quot;can-opus-4.6-do-category-theory-in-lean%3F&quot; tabindex=&quot;-1&quot;&gt;Can Opus 4.6 do Category Theory in Lean?&lt;/h1&gt;
&lt;p&gt;I have a little category theory library I&#39;ve been dragging around for about a decade now. It started life in Haskell, got ported to Agda, briefly lived in Idris, spent some time in Coq, and has now landed in Lean 4. I call it &lt;a href=&quot;https://github.com/sdiehl/kitty-cats&quot;&gt;Kitty Cats&lt;/a&gt;. The idea is simple: take the definitions and theorems from Awodey&#39;s &lt;em&gt;Category Theory&lt;/em&gt; and the relevant chapters of Mac Lane&#39;s &lt;em&gt;Categories for the Working Mathematician&lt;/em&gt;, and translate them directly into type-checkable code. Categories, functors, natural transformations, products, limits, adjunctions, monads, the Yoneda lemma. No libraries, no imports beyond what you define yourself. About 900 lines of Lean when all is said and done. It is the kind of project that fits entirely in your head, which is precisely the point. Every time a new dependently typed language or proof assistant comes along, I reach for this scaffold and see how it feels. It&#39;s my litmus test, my canonical exercise, my desert island formalization. And it&#39;s increasingly a good benchmark for how good language models have become abstract formal &amp;quot;reasoning&amp;quot; (or maybe just approximating it, but I&#39;ll leave that question to the philosophers!).&lt;/p&gt;
&lt;p&gt;So when Anthropic dropped the new Opus 4.6 model, I wanted to know: can it actually write proper Lean 4? Not toy examples. Not &lt;code&gt;theorem two_plus_two : 2 + 2 = 4 := rfl&lt;/code&gt;. Real proofs with real proof obligations, the kind where you need to wrangle naturality squares and coherence conditions and the elaborator is fighting you every step of the way. The beautiful thing about a proof assistant is that the answer is ground-verifiable. You don&#39;t have to squint at the output and wonder if it&#39;s subtly wrong. Either &lt;code&gt;lake build&lt;/code&gt; passes or it doesn&#39;t. The kernel doesn&#39;t care about your feelings.&lt;/p&gt;
&lt;p&gt;The short answer is: yes, kinda mostly. But the longer answer is more interesting.&lt;/p&gt;
&lt;p&gt;We (the royal &amp;quot;we&amp;quot; for me + model) built the whole library together over the course of two 30-minute sessions. The basic hierarchy went up fast. Categories, functors, natural transformations, the opposite category, morphism classes. These are mostly structure declarations with straightforward proof obligations: the category axioms &#92;(&#92;mathrm{id} &#92;circ f = f&#92;), &#92;(f &#92;circ &#92;mathrm{id} = f&#92;), and &#92;((f &#92;circ g) &#92;circ h = f &#92;circ (g &#92;circ h)&#92;); the functor laws &#92;(F(&#92;mathrm{id}) = &#92;mathrm{id}&#92;) and &#92;(F(f &#92;circ g) = F(f) &#92;circ F(g)&#92;). Opus handled these without breaking a sweat, and honestly, so would most competent language models at this point. The proofs are one or two tactics, often just &lt;code&gt;rfl&lt;/code&gt; or &lt;code&gt;by simp&lt;/code&gt;. The real question was always going to be what happens when the proofs get hard.&lt;/p&gt;
&lt;p&gt;Things got interesting around monoidal categories and the endofunctor construction. To prove that the category of endofunctors is monoidal under composition, you need to establish the pentagon and triangle coherence conditions. The pentagon axiom states that the two ways of reassociating a four-fold tensor product agree:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(&#92;alpha_{F,G,H} &#92;otimes &#92;mathrm{id}_K) &#92;circ &#92;alpha_{F, G &#92;circ H, K} &#92;circ (&#92;mathrm{id}_F &#92;otimes &#92;alpha_{G,H,K}) = &#92;alpha_{F &#92;circ G, H, K} &#92;circ &#92;alpha_{F, G, H &#92;circ K}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where &#92;(&#92;alpha&#92;) is the associator natural isomorphism. The triangle axiom relates the associator to the left and right unitors &#92;(&#92;lambda&#92;) and &#92;(&#92;rho&#92;):&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;alpha_{F, &#92;mathrm{Id}, G} &#92;circ (&#92;mathrm{id}_F &#92;otimes &#92;lambda_G) = &#92;rho_F &#92;otimes &#92;mathrm{id}_G&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;These are precisely the kind of thing that&#39;s annoying to formalize because the goals involve deeply nested natural transformations composed horizontally and vertically, and the elaborator&#39;s notion of what things are &amp;quot;definitionally equal&amp;quot; doesn&#39;t always match your intuition. Opus could get the structure right, it could set up the &lt;code&gt;show&lt;/code&gt; blocks that unfold the monoidal category projections into their concrete NatTrans representations, and it could figure out that &lt;code&gt;ext a; simp [...]&lt;/code&gt; with the right unfolding lemmas would close the goals. But the proofs it produced were verbose. Walls of nested parentheses. The kind of thing that&#39;s technically correct but makes your eyes glaze over.&lt;/p&gt;
&lt;p&gt;The crown jewel of the library is &lt;code&gt;monadIsMonoidObj&lt;/code&gt;: the proof that a monad is a monoid in the category of endofunctors. It&#39;s a joke that every functional programmer has heard (it&#39;s even &lt;a href=&quot;https://people.willamette.edu/~fruehr/haskell/evolution.html&quot;&gt;a meme&lt;/a&gt; at this point), but actually formalizing it requires you to bridge two levels of abstraction. A monad &#92;((T, &#92;eta, &#92;mu)&#92;) on a category &#92;(&#92;mathcal{C}&#92;) is an endofunctor &#92;(T&#92;) equipped with natural transformations &#92;(&#92;eta : &#92;mathrm{Id} &#92;Rightarrow T&#92;) (unit) and &#92;(&#92;mu : T^2 &#92;Rightarrow T&#92;) (multiplication) satisfying associativity and unit laws:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
T(&#92;mu_a) &#92;circ &#92;mu_a = &#92;mu_{T(a)} &#92;circ &#92;mu_a&lt;br /&gt;
&#92;qquad&lt;br /&gt;
T(&#92;eta_a) &#92;circ &#92;mu_a = &#92;mathrm{id}&lt;br /&gt;
&#92;qquad&lt;br /&gt;
&#92;eta_{T(a)} &#92;circ &#92;mu_a = &#92;mathrm{id}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;These are component-wise equations, one for each object &#92;(a&#92;) of &#92;(&#92;mathcal{C}&#92;). To show that &#92;((T, &#92;mu, &#92;eta)&#92;) forms a monoid object in the monoidal category &#92;((&#92;mathrm{End}(&#92;mathcal{C}), &#92;circ, &#92;mathrm{Id})&#92;), you need to lift them to equalities of natural transformations in the endofunctor category, which is itself equipped with the monoidal structure you just built. Each field of the MonoidObj structure requires a &lt;code&gt;show&lt;/code&gt; block that manually unfolds the monoidal category projections (because &lt;code&gt;ext&lt;/code&gt; can&#39;t see through the &lt;code&gt;Hom&lt;/code&gt; type alias to find the &lt;code&gt;NatTrans&lt;/code&gt; it needs), followed by &lt;code&gt;ext; endo_simp&lt;/code&gt; to reduce to components, followed by &lt;code&gt;exact M.mul_assoc a&lt;/code&gt; or whichever monad axiom applies. Opus got the shape of this right but needed significant guidance on factoring. The raw output had the &lt;code&gt;show&lt;/code&gt; blocks inlined directly in the instance declaration, three or four lines of deeply nested &lt;code&gt;NatTrans.vcomp (endoTensorHom M.mu (NatTrans.ident M.T)) M.mu = ...&lt;/code&gt; that, while correct, would terrify anyone reading the code for the first time.&lt;/p&gt;
&lt;p&gt;This is where the model hit a few bumps and needed some help. We refactored the proofs. We introduced &lt;code&gt;endo_ext_eq&lt;/code&gt;, a small helper that lifts component-wise equalities to NatTrans equalities, so you could write &lt;code&gt;endo_ext_eq fun a =&amp;gt; by endo_simp; exact M.mul_assoc a&lt;/code&gt; instead of the &lt;code&gt;show; ext a; endo_simp; exact&lt;/code&gt; dance. We extracted the monoidal coherence obligations into standalone lemmas (&lt;code&gt;endoPentagon&lt;/code&gt;, &lt;code&gt;endoTriangle&lt;/code&gt;, &lt;code&gt;endoTensor_id&lt;/code&gt;, &lt;code&gt;endoTensor_comp&lt;/code&gt;) so the instance declaration became pure assignment. We used &lt;code&gt;let&lt;/code&gt; bindings in theorem signatures to name the five different associator instances in the pentagon axiom, turning a wall of &lt;code&gt;(endoAssociator F (CFunctor.compose G H) K).hom&lt;/code&gt; into a legible &lt;code&gt;alpha_F_GH_K&lt;/code&gt;. The &lt;code&gt;monadIsMonoidObj&lt;/code&gt; definition went from 22 lines of inline proof to 7 lines of named lemma applications. This kind of refactoring is, I think, the thing that humans are still decisively better at: knowing what a reader needs to see, knowing which subexpressions deserve names, knowing when a proof is correct but not yet good.&lt;/p&gt;
&lt;p&gt;The Yoneda lemma was a pleasant surprise. The statement is that for any functor &#92;(F : &#92;mathcal{C}^{&#92;mathrm{op}} &#92;to &#92;mathbf{Set}&#92;) and any object &#92;(a&#92;) in &#92;(&#92;mathcal{C}&#92;), there is a natural bijection:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;mathrm{Nat}(&#92;mathrm{Hom}(-, a),; F) ;&#92;cong; F(a)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The forward direction (evaluating a natural transformation at &#92;(&#92;mathrm{id}_a&#92;) recovers the determining element) is almost trivial once you have &lt;code&gt;map_id&lt;/code&gt;. The backward direction (rebuilding the transformation from its value at &#92;(&#92;mathrm{id}_a&#92;)) requires extracting a naturality equation, simplifying it, and applying it symmetrically. Opus navigated this correctly, including the slightly tricky &lt;code&gt;congrFun&lt;/code&gt; applications needed when your hom-sets are function types in the Type category. The fully faithful embedding (&lt;code&gt;yoneda_map_eval&lt;/code&gt; and &lt;code&gt;yoneda_eval_map&lt;/code&gt;) fell out naturally. I was honestly a little charmed.&lt;/p&gt;
&lt;p&gt;What made this work as well as it did was the tooling. Two open source projects deserve credit here. The &lt;a href=&quot;https://github.com/oOo0oOo/lean-lsp-mcp&quot;&gt;lean-lsp-mcp&lt;/a&gt; server by Oliver Dressler exposes the Lean 4 language server as an MCP interface, giving Claude direct access to goal states, diagnostics, completions, and hover information. The &lt;a href=&quot;https://github.com/cameronfreer/lean4-skills&quot;&gt;lean4-skills&lt;/a&gt; plugin by Cameron Freer builds on top of this with higher-level proving commands, cycle engines, and premise search integrations. Together they give the model a genuine feedback loop. It can call &lt;code&gt;lean_goal&lt;/code&gt; to inspect the proof state at any line, call &lt;code&gt;lean_diagnostic_messages&lt;/code&gt; to see if the file has errors, call &lt;code&gt;lean_multi_attempt&lt;/code&gt; to try several tactic sequences and see which ones make progress, and call &lt;code&gt;lean_run_code&lt;/code&gt; to test a complete snippet against the kernel without modifying the working file. This changes the dynamics fundamentally. Instead of generating Lean code and hoping it compiles, the model operates in a tight loop: write a proof, check the goal state, see the error, adjust. It&#39;s the difference between painting blindfolded and painting with your eyes open. The model still makes mistakes, it still tries tactics that don&#39;t apply and writes &lt;code&gt;rw&lt;/code&gt; chains that don&#39;t match, but it can detect these failures immediately and recover. Six months ago this kind of integration didn&#39;t exist. The model would have been generating Lean into a void, with no way to know if the elaborator was happy or screaming.&lt;/p&gt;
&lt;p&gt;The search tools matter too. &lt;code&gt;lean_leansearch&lt;/code&gt;, &lt;code&gt;lean_loogle&lt;/code&gt;, and &lt;code&gt;lean_leanfinder&lt;/code&gt; let the model query Mathlib&#39;s lemma database by natural language, by type signature, and by semantic similarity. We didn&#39;t use Mathlib in this project (the whole point is to build from scratch), but the ability to verify that a lemma name exists before trying to use it, or to find the right name for a theorem you know should exist, is invaluable. &lt;code&gt;lean_state_search&lt;/code&gt; and &lt;code&gt;lean_hammer_premise&lt;/code&gt; go even further: given a proof state, they suggest lemmas that might close the goal. This is premise selection, the same technique that powers the strongest automated theorem provers, exposed as an API call. We&#39;re not quite at the point where you can say &amp;quot;prove the pentagon axiom&amp;quot; and walk away. But we&#39;re remarkably close to something that can handle a lot of nontrivial theorem proving with appropriate guidance.&lt;/p&gt;
&lt;p&gt;If I&#39;m being honest about the limitations: Opus struggles with the kind of reasoning that requires holding a complex proof state in working memory and planning several steps ahead. It can close goals that yield to a single tactic or a short chain, but multi-step rewrites where you need to introduce a naturality equation, simplify it in a particular way, then apply it at a specific position in a long composition chain, that still requires hand-holding. It sometimes tries &lt;code&gt;simp&lt;/code&gt; when it needs &lt;code&gt;rw&lt;/code&gt;, or applies a lemma at the wrong position, or forgets that a &lt;code&gt;show&lt;/code&gt; block is necessary to make &lt;code&gt;ext&lt;/code&gt; fire. These are not deep failures of understanding; they&#39;re more like the mistakes a graduate student makes in their first semester with a proof assistant. The model knows what it wants to prove, it roughly knows the strategy, but the bureaucratic details of getting the elaborator to agree take iteration.&lt;/p&gt;
&lt;p&gt;One thing worth noting: in informal experiments, turning the reasoning budget up did increase convergence time (more tokens spent deliberating before committing to a tactic) but didn&#39;t obviously improve the quality of the final output. The model would think longer and still reach for the same &lt;code&gt;simp&lt;/code&gt; call. This is a hard thing to measure empirically because the process is nondeterministic and highly sensitive to prompting, so take it with a grain of salt. But my working hypothesis is that for tactic-level theorem proving, the bottleneck is less about &amp;quot;thinking harder&amp;quot; and more about having the right tool calls in the loop. The language server feedback matters more than the internal chain of thought.&lt;/p&gt;
&lt;p&gt;The thing is, it&#39;s gotten exponentially better in the last six months. I&#39;ve been running this same exercise periodically, and the trajectory is striking. A year ago, language models could barely write syntactically valid Lean. Six months ago, they could handle simple tactic proofs but fell apart on anything involving universe polymorphism or typeclass resolution. Today, with MCP integration and language server feedback, we built 900 lines of formalized category theory in two sessions, including proofs that many math graduate students would find challenging. It&#39;s not hard to project where this is going.&lt;/p&gt;
&lt;p&gt;And maybe that&#39;s the exciting part. Not that AI can prove theorems today (it can, sort of, with help), but that the boilerplate is evaporating. The tedious parts of formalization, the coherence conditions, the naturality lemmas, the &lt;code&gt;simp&lt;/code&gt; configurations, these are exactly the kind of structured, verifiable, mechanically checkable work that language models are getting good at. When this layer becomes trivial, we get to spend our time on the parts that actually matter: choosing the right abstractions, seeing the connections between structures, deciding what&#39;s worth formalizing in the first place. The proof assistant becomes less of a bureaucratic obstacle and more of a genuine thinking tool. We get to build higher.&lt;/p&gt;
&lt;p&gt;I&#39;ve been carrying this little category theory library around for ten years, porting it from language to language, and every time, the experience tells me something about the state of the art. This time what it told me is that we&#39;re living in the future, and it&#39;s weirder and more interesting than I expected.&lt;/p&gt;
</description>
      <pubDate>Mon, 16 Feb 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/lean-opus-blog/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/lean-opus-blog/</guid>
    </item>
    
    <item>
      <title>Book Review: When We Cease to Understand the World</title>
      <description>&lt;h1 id=&quot;book-review%3A-when-we-cease-to-understand-the-world&quot; tabindex=&quot;-1&quot;&gt;Book Review: When We Cease to Understand the World&lt;/h1&gt;
&lt;p&gt;Benjamin Labatut&#39;s &lt;em&gt;When We Cease to Understand the World&lt;/em&gt; is the best book I have read in years, and it is the kind of book that makes you feel slightly unwell afterward in the way that only the best books can. It is nominally about the history of science. It is actually about what happens to the human mind when it touches something it was never designed to comprehend. If you have ever stared at an equation long enough that the symbols started to feel hostile, or found yourself at three in the morning with the unsettling conviction that the abstraction you are building understands something you do not, this book was written for you.&lt;/p&gt;
&lt;p&gt;The structure is deceptively simple. Labatut takes a series of real scientists (Fritz Haber, Karl Schwarzschild, Werner Heisenberg, Erwin Schrodinger, Alexander Grothendieck) and tells the story of their discoveries as a series of escalating psychological horror vignettes. Haber synthesizes ammonia and feeds a billion people, then synthesizes chlorine gas and poisons thousands of them. Schwarzschild solves Einstein&#39;s field equations in a trench during World War I while his own immune system eats him alive, and the solution he finds describes a hole in spacetime where the laws of physics go to die. Heisenberg, on the island of Helgoland, suffering from hay fever so severe he can barely see, discovers that the universe will not let you know both where a particle is and where it is going. All of this, more or less, actually happened. The universe has a flair for narrative that most novelists would find implausible. If Schwarzschild&#39;s story were submitted as fiction, an editor would send it back with a note about heavy-handed symbolism. The man solving the equations of gravitational collapse while his own body collapses around him? Too on the nose. Unfortunately, nobody copyedits reality.&lt;/p&gt;
&lt;p&gt;What Labatut does with these facts is something genuinely new. He tells you at the outset that the quantity of fiction increases as the book progresses (the technique is called &lt;a href=&quot;https://www.goodreads.com/shelf/show/historiographic-metafiction&quot;&gt;historiographic metafiction&lt;/a&gt;). The early chapters hew closely to the historical record. By the final chapter, you are reading something closer to a dream. You cannot tell where the real ends and the invented begins, and this is the entire point. The form enacts the thesis: as knowledge approaches the frontier, certainty dissolves. The book about the limits of knowing is itself a document whose factual boundaries you cannot locate. It is the literary equivalent of the uncertainty principle applied to the act of reading.&lt;/p&gt;
&lt;p&gt;The genius of the book, and the reason I think it deserves a much wider readership among people who work with formal systems for a living, is that Labatut takes the romantic cliche of the mad genius and replaces it with something far more disturbing. His argument is stronger and stranger: the things these people found are genuinely alien to human cognition, and the psychological cost was structural, baked into the discovery itself. Heisenberg went slightly mad because he discovered that the universe, at its most fundamental level, refuses to behave the way human minds expect it to behave, and his nervous system registered this as an emergency. Schrodinger&#39;s wave equation emerged during a feverish affair in an alpine hotel, as if the only way to access that particular region of reality was to destabilize every other region of his life first.&lt;/p&gt;
&lt;p&gt;The Grothendieck chapter is the one that will haunt anyone who has spent time in mathematics or theoretical computer science. Grothendieck is arguably the greatest mathematician of the twentieth century. He rebuilt algebraic geometry from scratch. He saw structures so deep and so general that the mathematical community is still unpacking them decades later. And then he stopped. He walked away from mathematics entirely, convinced that the same cognitive acts that built beautiful theorems also built nuclear weapons. He spent his final decades living in a village in the Pyrenees, eating dandelion soup, writing thousand-page mystical treatises that nobody read. For anyone who has ever rage-quit a codebase, Grothendieck is the patron saint: the one person in history who rage-quit all of mathematics and arguably had the standing to do so. Labatut presents this as a coherent response to what Grothendieck saw. The man looked into the deepest structures of mathematical reality and concluded that the act of looking was itself dangerous. This is Nietzsche&#39;s abyss, staring back with a Fields Medal.&lt;/p&gt;
&lt;p&gt;The book&#39;s final and perhaps most unsettling move is its insistence that beauty and horror are the same thing. Schwarzschild&#39;s solution is beautiful. It also describes the annihilation of everything that falls past a certain radius. The nitrogen fixation reaction that feeds billions is chemically identical to the process that produces cyanide. Prussian blue, a pigment of extraordinary beauty, is a precursor to Zyklon B. Labatut is making a metaphysical claim: at the level of fundamental reality, creation and destruction are the same operation viewed from different angles. The universe is gorgeous and indifferent to you, and these two facts are related.&lt;/p&gt;
&lt;p&gt;But the Lovecraftian reading, the one where contact with deep reality simply destroys the knower, is only half of what Labatut is doing. The night gardener in the final chapter has looked into the same abyss as Grothendieck and Heisenberg. He has come back, and he tends a garden. The garden is full of rot and parasites and dying trees, and he tends it anyway. There is something quietly radical in Labatut&#39;s refusal to end the book on annihilation. The living does not require certainty. The absence of certainty is not emptiness but space. Space is where everything that matters happens. The scientists in this book were damaged by what they found at the bottom of the equations, but the damage was the cost of looking, and the looking produced real knowledge about a real universe that we actually inhabit. Labatut&#39;s point, ultimately, is that the risk is worth taking. The garden persists. You tend it in the dark, without guarantees, and that is enough.&lt;/p&gt;
&lt;p&gt;I recommend this book to anyone who reads this blog. If you have ever been seduced by the elegance of a proof, or felt the particular vertigo that comes from watching an abstraction click into place with a precision that seems to exceed what the physical universe should allow, Labatut has written the horror novel about your vocation. It is short (under 200 pages), which means it has a higher insight-per-page density than most textbooks and approximately all of Twitter. It is exquisitely written (Adrian Nathan West&#39;s translation deserves its own paragraph of praise, but I will restrain myself), and it will leave a residue in your thinking for months. Read it on a plane, in a single sitting, and then stare out the window for a while. You will need to.&lt;/p&gt;
</description>
      <pubDate>Sun, 08 Feb 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/cease_understand_world/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/cease_understand_world/</guid>
    </item>
    
    <item>
      <title>Hypothetical Divine Signatures</title>
      <description>&lt;h1 id=&quot;hypothetical-divine-signatures&quot; tabindex=&quot;-1&quot;&gt;Hypothetical Divine Signatures&lt;/h1&gt;
&lt;p&gt;The author of the Epistle to the Hebrews famously claimed that &amp;quot;faith is the substance of things hoped for, the evidence of things not seen,&amp;quot; which was a perfectly serviceable theological patch for an era where the average person’s greatest computational challenge was counting their own fingers. However, in an age where we can simulate galaxies and sequence genomes, &amp;quot;I’m God, trust me bro&amp;quot; is not a particularly compelling argument to the modern mind, unlike our goat-herding ancestors who were easily impressed by a well-timed solar eclipse or a particularly loud bush. If a truly omniscient entity wanted to establish a &amp;quot;scientific secular covenant&amp;quot; with a technological species, it would not rely on subjective feelings or ambiguous dreams. It would instead provide a Divine Signature through the cold, hard lens of computational complexity: a set of claims that are succinct enough to be carved into a stone tablet but so mathematically dense that finding them would require more energy than exists in the observable universe.&lt;/p&gt;
&lt;p&gt;To move beyond mere storytelling and into the realm of objective proof, a text must demonstrate that it has bypassed &lt;a href=&quot;https://en.wikipedia.org/wiki/Bremermann%27s_limit&quot;&gt;Bremermann&#39;s Limit&lt;/a&gt;, the physical threshold for the maximum computational speed of any self-contained system in the material universe. This limit, derived from Einstein&#39;s mass-energy equivalence and the Heisenberg uncertainty principle, is approximately &#92;( c^2/h &#92;approx 1.36 &#92;times 10^{50} &#92;) bits per second per kilogram. The &amp;quot;soft&amp;quot; version of this limit considers a computer the mass of the Earth running for billions of years; the &amp;quot;strong&amp;quot; version considers harnessing every atom and cubic meter of space in the observable universe until heat death. By providing answers to problems that exceed even the strong limit, a &amp;quot;divine&amp;quot; author proves they are operating from a platform that exists outside our localized entropy and processing constraints, effectively signing their work with a flourish that no amount of human ingenuity or effort could forge.&lt;/p&gt;
&lt;p&gt;A Platonist might object that a divine entity would not compute these answers but simply know them, the way one knows that 2+2=4, by virtue of being the ground of mathematical reality itself. This is a fair metaphysical point, but it is irrelevant to the epistemic argument. However the knowledge is obtained, whether by super-computation, by direct mathematical intuition, or by being the ontological source of mathematical truth, the demonstration of that knowledge in a text remains evidence of something beyond bounded physical intelligence. The signature&#39;s power lies not in proving that God has big computers, but in proving that the author possesses knowledge that no physically-bounded process could have generated. The mechanism is beside the point; the information-theoretic fact is not.&lt;/p&gt;
&lt;p&gt;Personally, I hold no supernatural beliefs, and I am serenely indifferent to those who do. But a good hypothesis must be falsifiable, and intellectual honesty requires stating what evidence would change my mind. A verified divine signature of this kind would do exactly that. I would naturally default to material explanations until they were exhausted, but the exercise demonstrates that materialism is not unfalsifiable dogma; one can articulate the precise circumstances under which it would fall.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Divine Factorization.&lt;/strong&gt; A prophetic verse might read: &lt;em&gt;&amp;quot;Behold the great number &#92;( N &#92;), whose length is as twenty thousand digits; it is born of the union of &#92;( P &#92;) and &#92;( Q &#92;), and none other shall divide it.&amp;quot;&lt;/em&gt; This obviously refers to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Integer_factorization&quot;&gt;Integer Factorization Problem&lt;/a&gt;. Twenty thousand digits is not even that much text; it is considerably shorter than the &amp;quot;begots&amp;quot; documenting forty generations of Levantine shepherds. While multiplying two primes to get a 20,000-digit semiprime is a simple operation, reversing the process is effectively intractable. For example, RSA-129 was published in 1977 with a one hundred million dollar prize and remained unfactored for 17 years:&lt;br /&gt;
$$114381625757888867669235779976146612010218296721242362562561842935706935245733897830597123563958705058989075147599290026879543541$$&lt;br /&gt;
$$= 3490529510847650949147849619903898133417764638493387843990820577$$&lt;br /&gt;
$$&#92;times &#92; 32769132993266709549961988190834461413177642967992942539798288533$$&lt;br /&gt;
At only 129 digits, this took a massive distributed computing effort in 1994. A 20,000-digit semiprime is in another universe entirely. Under the General Number Field Sieve, the clock cycles required to factor a 20,000-digit number would exceed the total available energy in the observable universe. It is a thermodynamic wall that cannot be breached by any bounded intelligence without infinite time. For us, verification is trivial; we simply multiply the two provided numbers together to see if they match &#92;( N &#92;), a task a standard smartphone can complete in a fraction of a second.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Ramsey Revelation.&lt;/strong&gt; The scripture would proclaim: &lt;em&gt;&amp;quot;If ten brethren and ten strangers be gathered in a hall of souls, there shall surely be a clique of ten or a desert of ten; behold the map of their connections.&amp;quot;&lt;/em&gt; This addresses the &lt;a href=&quot;https://en.wikipedia.org/wiki/Ramsey%27s_theorem&quot;&gt;Constructive Lower Bound of Ramsey Numbers&lt;/a&gt;, denoted as &#92;( R(r, s) &#92;). The known diagonal Ramsey numbers are:&lt;br /&gt;
$$R(3,3) = 6, &#92;quad R(4,4) = 18, &#92;quad R(5,5) = 43&#92;text{--}48, &#92;quad R(6,6) = 102&#92;text{--}165, &#92;quad &#92;ldots$$&lt;br /&gt;
We cannot even pin down &#92;( R(5, 5) &#92;) to an exact value; it is famously &amp;quot;impossible&amp;quot; to compute because the combinations grow too fast. Providing the exact value for &#92;( R(10, 10) &#92;) along with a specific graph coloring that avoids a clique of size 10 is succinct to state but requires navigating a search space that is thermodynamically inaccessible to our universe. To put this in perspective: the number of ways to two-color the edges of a graph with 1,000 nodes is &#92;( 2^{499,500} &#92;), while the number of atoms in the observable universe is roughly &#92;( 2^{266} &#92;). The search space is mathematically larger than any physical computer could ever process. We verify one graph; the author had to search all of them. Finding a specific &amp;quot;needle&amp;quot; graph in this haystack is a rational proof of super-universal processing power. Verification is straightforward for us because we can simply run a script to scan the provided adjacency matrix to confirm no group of 10 nodes exists where every edge is the same color, which is a polynomial time operation &#92;( O(n^{10}) &#92;) that is easily handled by modern hardware.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Circle&#39;s Secret Checksum.&lt;/strong&gt; The text would command: &lt;em&gt;&amp;quot;Search the circle&#39;s measure at the position of &#92;( 10^{20} &#92;) and there find a thousand zeros followed by the message of the stars.&amp;quot;&lt;/em&gt; This utilizes the &lt;a href=&quot;https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula&quot;&gt;Bailey–Borwein–Plouffe algorithm&lt;/a&gt;, which allows us to calculate the &#92;( n^{th} &#92;) digit of &#92;( &#92;pi &#92;) in base-16 without calculating the preceding digits:&lt;br /&gt;
$$&#92;pi = &#92;sum_{k=0}^{&#92;infty} &#92;frac{1}{16^k} &#92;left( &#92;frac{4}{8k+1} - &#92;frac{2}{8k+4} - &#92;frac{1}{8k+5} - &#92;frac{1}{8k+6} &#92;right)$$&lt;br /&gt;
We cannot currently scan &#92;( &#92;pi &#92;) out to &#92;( 10^{20} &#92;) to find interesting patterns, and the information entropy of &#92;( &#92;pi &#92;) suggests that such a low-entropy anomaly at such a distant coordinate is statistically impossible to occur by chance. If the text successfully predicts a thousand zeros at a specific, distant coordinate, it implies the author did not &amp;quot;scan&amp;quot; for it but rather created the fundamental constants of mathematics itself, or possesses infinite foreknowledge of the structure of irrational numbers. This was, incidentally, a plot point in Carl Sagan&#39;s novel &lt;a href=&quot;https://en.wikipedia.org/wiki/Contact_(novel)&quot;&gt;Contact&lt;/a&gt;, where a message hidden deep in the digits of &#92;( &#92;pi &#92;) serves as a signature from the architects of the universe. Verification is feasible with a dedicated planetary effort; the BBP algorithm&#39;s time complexity of &#92;( O(n &#92;log^2 n &#92;log &#92;log n) &#92;) makes checking a specific coordinate far cheaper than scanning for one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The End of Euler’s Dream.&lt;/strong&gt; The prophet would say: &lt;em&gt;&amp;quot;Though twelve powers of twelve seem to need their kind, search for the eleven that equal the one; find them at these integers.&amp;quot;&lt;/em&gt; This targets the &lt;a href=&quot;https://en.wikipedia.org/wiki/Euler%27s_sum_of_powers_conjecture&quot;&gt;Smallest Counter-example to Euler’s Sum of Powers Conjecture&lt;/a&gt;, which posits that you need &#92;( n &#92;) &#92;( n^{th} &#92;) powers to sum to another &#92;( n^{th} &#92;) power:&lt;br /&gt;
$$&#92;sum_{i=1}^{k} a_i^n = b^n &#92;implies k &#92;geq n$$&lt;br /&gt;
While humans found counter-examples for &#92;( n=4 &#92;) and &#92;( n=5 &#92;) after centuries of searching, providing a solution for &#92;( n=12 &#92;) would be a needle-in-a-haystack problem of cosmic proportions. This is essentially a search through a Diophantine space that is effectively infinite. We verify the claim easily by plugging the provided integers into a high-precision calculator and confirming the left side of the equation perfectly equals the right, transforming a massive search problem into a simple arithmetic check.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Busy Beaver&#39;s Rest.&lt;/strong&gt; A prophetic verse might say: &lt;em&gt;&amp;quot;Consider the machine of twenty states, simple in its ways; it shall toil for exactly &#92;( X &#92;) steps and then find its rest, and no man shall count the days of its labor.&amp;quot;&lt;/em&gt; This is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Busy_beaver&quot;&gt;Busy Beaver Function&lt;/a&gt;, &#92;( BB(n) &#92;), the final boss of computer science. Because it effectively solves the Halting Problem, no general algorithm exists to calculate these values; they are mathematically uncomputable. Humanity has, with heroic effort, managed to prove values only for the smallest machines:&lt;br /&gt;
$$BB(1) = 1, &#92;quad BB(2) = 6, &#92;quad BB(3) = 21, &#92;quad BB(4) = 107, &#92;quad BB(5) = 47176870, &#92;quad &#92;ldots$$&lt;br /&gt;
Beyond five states, the function explodes beyond comprehension; &#92;( BB(6) &#92;) is known to exceed &#92;( 10 &#92;uparrow&#92;uparrow 15 &#92;), a tower of 15 tens. Stating the exact halting time for a 20-state Turing machine is a God-level flex because it implies the author bypassed the logical impossibility of the Halting Problem itself. Verification is as simple as simulating the specific machine described and counting its steps until it halts, which requires zero creative mathematics or algorithmic breakthroughs on our part.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Kissing of Spheres.&lt;/strong&gt; The scripture would read: &lt;em&gt;&amp;quot;In a realm of an hundred depths, where spheres are gathered like grapes, exactly &#92;( X &#92;) shall press against the heart of the center.&amp;quot;&lt;/em&gt; This refers to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Kissing_number&quot;&gt;Kissing Number&lt;/a&gt; problem: how many non-overlapping spheres can touch a central sphere of the same size? In three dimensions, the answer is 12, which is easy enough to visualize over a drink. In 8 dimensions, the answer is 240. In 24 dimensions, it is 196,560, thanks to the elegant structure of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Leech_lattice&quot;&gt;Leech Lattice&lt;/a&gt;. But beyond 24 dimensions, we lack these lattice optimizations that make the math manageable. In 100 dimensions, the symmetry is so complex that our best supercomputers can only give us a vague range. The number of possible configurations for non-overlapping spheres explodes into a combinatorial nightmare, and determining the exact maximum requires navigating a high-dimensional search space so vast that even an advanced intelligence utilizing quantum annealing would likely settle on a local maximum rather than the true global optimum. Providing the exact integer would reveal a perfect mastery of high-dimensional space, the kind of insight that suggests the author is comfortable navigating 100-dimensional manifolds like they were a simple game of marbles. We verify the result by checking the provided coordinates of the spheres to ensure the distance between any two sphere centers satisfies:&lt;br /&gt;
$$&#92;text{dist}(c_i, c_j) &#92;geq 2$$&lt;br /&gt;
while each satisfies &#92;( &#92;text{dist}(c_i, &#92;text{origin}) = 2 &#92;), which is basic linear algebra.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Titan of Primes.&lt;/strong&gt; The text might state: &lt;em&gt;&amp;quot;The millionth prime of the form &#92;( 2^p - 1 &#92;) shall be found when &#92;( p &#92;) is this specific titan of a number.&amp;quot;&lt;/em&gt; This identifies a &amp;quot;deep&amp;quot; &lt;a href=&quot;https://en.wikipedia.org/wiki/Mersenne_prime&quot;&gt;Mersenne Prime&lt;/a&gt;, &#92;( M_p = 2^p - 1 &#92;). While we have only found about 50 of these primes using global distributed computing networks like GIMPS:&lt;br /&gt;
$$p_5 = 13, &#92;quad p_{10} = 89, &#92;quad p_{20} = 4423, &#92;quad p_{30} = 132049, &#92;quad p_{40} = 20996011, &#92;quad p_{51} = 82589933, &#92;quad &#92;ldots$$&lt;br /&gt;
providing a &amp;quot;Distant&amp;quot; Mersenne Prime Exponent for the millionth one would be providing a password to the secret architecture of the number line. Finding it requires a &amp;quot;God’s-eye view&amp;quot; of prime distribution that likely requires a proof of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Riemann_hypothesis&quot;&gt;Riemann Hypothesis&lt;/a&gt;. Verification is quite efficient for us; we simply run the &lt;a href=&quot;https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test&quot;&gt;Lucas-Lehmer&lt;/a&gt; test on the provided exponent, which is a deterministic and well-understood primality test.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Great Prime Desert.&lt;/strong&gt; The text would proclaim: &lt;em&gt;&amp;quot;Seek the first desert of ten thousand steps between adjacent numbers which no other divides; it begins at this number of one hundred digits.&amp;quot;&lt;/em&gt; A &lt;a href=&quot;https://en.wikipedia.org/wiki/Prime_gap&quot;&gt;prime gap&lt;/a&gt; is the difference between consecutive primes. While gaps tend to grow logarithmically, finding the first gap exceeding a specific large threshold requires searching through an enormous range of candidates. The largest known prime gaps have been found through exhaustive computation, and predicting where a gap of unprecedented size first occurs would demonstrate knowledge of prime distribution far beyond our current reach. Verification is straightforward: we simply confirm both endpoints are prime using the &lt;a href=&quot;https://en.wikipedia.org/wiki/AKS_primality_test&quot;&gt;AKS primality test&lt;/a&gt; (a deterministic polynomial-time algorithm) and verify that no integer between them is prime, which is feasible for a gap of &amp;quot;merely&amp;quot; ten thousand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Diophantine Key.&lt;/strong&gt; The text would say: &lt;em&gt;&amp;quot;Three cubes shall be gathered, and their sum shall be forty-two; seek them among the numbers of seventeen digits, and there find the truth.&amp;quot;&lt;/em&gt; While humans found the solution for &lt;a href=&quot;https://en.wikipedia.org/wiki/Sums_of_three_cubes&quot;&gt;sums of three cubes&lt;/a&gt; for &#92;( k=42 &#92;) in 2019 using a planetary-scale computer network:&lt;br /&gt;
$$42 = (-80538738812075974)^3 + 80435758145817515^3 + 12602123297335631^3$$&lt;br /&gt;
providing a solution for a much more complex equation moves the problem into the realm of Hilbert&#39;s Tenth Problem. Because this problem is generally undecidable, there is no &amp;quot;method&amp;quot; to solve it; you cannot write a program to solve all Diophantine equations. Finding a solution to a sufficiently complex one implies the ability to look through the infinite set of integers and &amp;quot;pick out&amp;quot; the needle. A divine solution transcends mere calculation; it is an insight into an undecidable space, suggesting the author is unbound by the Halting Problem. Verification is the definition of trivial; we cube the three provided integers, add them together, and see if the sum equals the target constant.&lt;/p&gt;
&lt;p&gt;A fair objection: would I actually update my beliefs, or would I simply expand the space of naturalistic hypotheses to include time travelers, simulation operators, or aliens? This is a legitimate concern about Bayesian reasoning under extreme priors. My honest answer is that a single signature would indeed trigger such explanatory gymnastics. But the argument becomes qualitatively different if an ancient text contained multiple independent signatures across different complexity classes: a Ramsey construction, a Mersenne exponent, and a Busy Beaver value. The probability of a Bronze Age civilization stumbling upon all three by any natural process, including alien intervention, collapses to effective zero. At some point, &amp;quot;super-universal intelligence authored this&amp;quot; becomes the simplest explanation, not the most extravagant one.&lt;/p&gt;
&lt;p&gt;Consider the &amp;quot;simulation operator&amp;quot; hypothesis more carefully. If our universe is a simulation, and the operators embedded verifiable signatures in ancient texts, what exactly distinguishes this from theism? We would have an intelligence that created our reality, possesses knowledge beyond anything accessible within our physics, chose to communicate through authored texts, and exists &amp;quot;outside&amp;quot; our universe. This is not a materialist escape hatch; it is theism wearing a lab coat. The same applies to sufficiently advanced aliens: if they can bypass Bremermann&#39;s limit, they are operating from outside our universe&#39;s physical constraints entirely. At that point, the distinction between &amp;quot;alien&amp;quot; and &amp;quot;god&amp;quot; is just vibes. A verified divine signature would not necessarily prove the God of Abraham, but it would prove that our universe was authored or governed by an intelligence beyond physical bounds, which is the core metaphysical claim of theism, shorn of its cultural dress. The retreat to &amp;quot;simulator&amp;quot; is not a retreat; it is a concession that looks like a deflection.&lt;/p&gt;
&lt;p&gt;The theologian might counter that God deliberately withholds such proof to preserve free will or test faith. This defense is perfectly general and therefore perfectly empty. It transforms &amp;quot;my God exists but chooses not to prove it&amp;quot; from a substantive claim into an unfalsifiable tautology. A God who could provide proof but will not is operationally indistinguishable from a God who does not exist, at least for the purposes of rational belief formation.&lt;/p&gt;
&lt;p&gt;One might also object that computational complexity is a parochial concern of 21st-century computer scientists, meaningless to ancient audiences. This is half-correct. A 20,000-digit number would indeed be noise to a Bronze Age shepherd. But the message need not be understood by every generation to be present for every generation. A prime factorization sits inert in the text until a civilization develops the mathematics to recognize it, at which point it becomes a time-delayed proof of authorship. This is a feature: a signature that reveals itself only to sufficiently advanced civilizations is more elegant than one that announces itself crudely.&lt;/p&gt;
&lt;p&gt;Of course, no existing religious text actually does this. Various &amp;quot;Bible code&amp;quot; phenomena have generated considerable excitement before being debunked as statistical pareidolia. One could argue we have not checked every possible encoding, but this is the logic of &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Library_of_Babel&quot;&gt;Borges&#39; Library of Babel&lt;/a&gt;: given enough combinatorial freedom, you can find anything in anything. The burden of proof runs the other way. &amp;quot;Maybe there is a hidden 20,000-digit prime factorization if you read every seventh letter in Aramaic&amp;quot; is not a research program; it is cope. Instead, these texts mostly focus on the really important issues of existence like who you can bang or what type of cheese you can put on meat, which were very human concerns that do not require much more than a Bronze Age imagination. One would think that a divine entity with infinite knowledge and infinite (computational) power would want to leave a signature that actually scales with the intelligence of the species it created. Providing a succinct, verifiable, but computationally impossible result would be the only way to satisfy a scientific secular framework. The fact that we have found plenty of rules about shellfish but zero 20,000-digit prime factors suggests that if there is a Great Programmer in the sky, they are either very shy or they simply forgot to &lt;code&gt;git add README.md&lt;/code&gt; in the final build.&lt;/p&gt;
</description>
      <pubDate>Wed, 14 Jan 2026 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/divinity/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/divinity/</guid>
    </item>
    
    <item>
      <title>Typechecker Zoo</title>
      <description>&lt;h1 id=&quot;typechecker-zoo&quot; tabindex=&quot;-1&quot;&gt;Typechecker Zoo&lt;/h1&gt;
&lt;p&gt;As a fun side project, I&#39;ve built four literate Rust implementations spanning 50 years of type theory research, from Milner&#39;s Algorithm W to modern dependent type systems. I&#39;ve tried to make each implementation as readable and understandable as possible, with detailed explanations and comments throughout the code and to make it as succinct and short as possible so you can pull it into your own projects as a place to start when building any type of system somewhere on the lambda cube.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/typechecker-zoo&quot;&gt;Github Repo&lt;/a&gt; | &lt;a href=&quot;https://sdiehl.github.io/typechecker-zoo/&quot;&gt;Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;-Since it&#39;s a zoo, I&#39;ve also given each one a cute animal mascot to represent them:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quot;https://sdiehl.github.io/typechecker-zoo/algorithm-w/lambda-calculus.html&quot;&gt;&lt;img src=&quot;https://www.stephendiehl.com/images/lambda.png&quot; alt=&quot;Algorithm W&quot; style=&quot;max-width:256px; height:auto;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Algorithm W (Hindley-Milner Type System)&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Implements Milner&#39;s 1978 unification-based type inference using structural recursion. The algorithm generates constraints between type variables during expression traversal, then solves these constraints through Robinson&#39;s unification algorithm. Features let-polymorphism enabling rank-1 polymorphic type inference without explicit type annotations. The implementation demonstrates classical occurs check handling and substitution propagation through type environments.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Reference:&lt;/strong&gt; &lt;br /&gt;&lt;em&gt;A Theory of Type Polymorphism in Programming&lt;/em&gt; by Robin Milner.&lt;br /&gt;&lt;a href=&quot;https://www.research.ed.ac.uk/en/publications/a-theory-of-type-polymorphism-in-programming&quot;&gt;PDF&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quot;https://sdiehl.github.io/typechecker-zoo/system-f/system-f.html&quot;&gt;&lt;img src=&quot;https://www.stephendiehl.com/images/ocaml.png&quot; alt=&quot;OCaml&quot; style=&quot;max-width:256px; height:auto;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;System F (Second-Order Lambda Calculus)&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Second-order lambda calculus with parametric polymorphism using bidirectional type checking. Bidirectional type checker implementing Dunfield-Krishnaswami algorithm for System F with universal quantification. Splits type checking into synthesis (⇒) and checking (⇐) modes, enabling polymorphic type inference with existential variable solving. Handles parametric polymorphism through universal types (∀α. τ), with careful context management for type variable scoping. Includes comprehensive constraint solving for existential variables and subtyping relations between polymorphic types.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Reference:&lt;/strong&gt; &lt;br /&gt;&lt;em&gt;Complete and Easy Bidirectional Typechecking for Higher-rank Polymorphism&lt;/em&gt; by Dunfield and Krishnaswami.&lt;br /&gt;&lt;a href=&quot;https://arxiv.org/abs/1306.6032&quot;&gt;PDF&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quot;https://sdiehl.github.io/typechecker-zoo/system-f-omega/system-f-omega.html&quot;&gt;&lt;img src=&quot;https://www.stephendiehl.com/images/haskell.png&quot; alt=&quot;Haskell&quot; style=&quot;max-width:256px; height:auto;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;System F-ω (Higher-Kinded Polymorphism)&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;A miniature Haskell-lite. Extends System F with type constructors and kind checking, implementing higher-rank polymorphism with algebraic data types. Features kind inference alongside type inference, managing both type-level and kind-level environments. Supports type constructor application and polymorphic kind assignments. Implements constraint generation for higher-kinded unification problems, demonstrating how kind systems prevent malformed type applications.&lt;br /&gt;&lt;br /&gt;Complete implementation of System F-ω with higher-kinded types, DK bidirectional type checking, existential type variables, polymorphic constructor applications, pattern matching, and lambda expressions with type inference.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Reference:&lt;/strong&gt; &lt;br /&gt;&lt;em&gt;A Mechanical Formalization of Higher-Ranked Polymorphic Type Inference&lt;/em&gt; by Joshua Dunfield.&lt;br /&gt;&lt;a href=&quot;https://i.cs.hku.hk/~bruno/papers/icfp2019.pdf&quot;&gt;PDF&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href=&quot;https://sdiehl.github.io/typechecker-zoo/coc/calculus-of-constructions.html&quot;&gt;&lt;img src=&quot;https://www.stephendiehl.com/images/lean.png&quot; alt=&quot;Lean&quot; style=&quot;max-width:256px; height:auto;&quot; /&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;Calculus of Constructions (Dependent Types)&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The Calculus of Constructions with a hierarchy of non-cumulative universes and inductive types. Full dependent type system unifying types and terms, implementing universe polymorphism with constraint-based inference. Has inductive type definitions, dependent function types (Pi-types, Sigma-types), and universe polymorphism. Uses meta-variable solving for partial solving of higher-order unification problems using Miller pattern technique.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Reference:&lt;/strong&gt; &lt;br /&gt;&lt;em&gt;A universe polymorphic type system&lt;/em&gt; by Vladimir Voevodsky.&lt;br /&gt;&lt;a href=&quot;https://ncatlab.org/ufias2012/files/Universe+polymorphic+type+sytem.pdf&quot;&gt;PDF&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;hr /&gt;
&lt;p&gt;The examples are implemented in fairly idiomatic Rust with a full parse and test suite (though no evaluator), using the usual compiler libraries such as larlpop, logos, ariande, etc. They are obviously simplified and code golfed versions of the full implementations so that they can be easily understood and modified. But they should be a LOT easier to understand that trying to read the Haskell or Coq typecheckers which are easily 100k+ LOC each. Hopefully useful to any young, budding language enthusiasts out there. I wish I had had this when I was learning these topics a decade ago so I figured I&#39;d build the resources I would want so that others don&#39;t have to struggle so much!&lt;/p&gt;
</description>
      <pubDate>Fri, 15 Aug 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/typechecker_zoo/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/typechecker_zoo/</guid>
    </item>
    
    <item>
      <title>The Stochastic Code Monkey Theorem</title>
      <description>&lt;h1 id=&quot;the-stochastic-code-monkey-theorem&quot; tabindex=&quot;-1&quot;&gt;The Stochastic Code Monkey Theorem&lt;/h1&gt;
&lt;p&gt;The relentless hype machine surrounding large language models would have you believe we are on the cusp of a software development revolution, a new epoch where programmers are obsolete, their keyboards gathering dust while throwing enough GPUs and parallel stochastic code monkeys at any problem will conjure the coding prowess of Fabrice Bellard and Jeff Dean given enough time and compute. This narrative is (not surprisingly) mostly spun by podcast hucksters and influencers rather than actual engineers and is, to put it mildly, a fanciful exaggeration. For the seasoned software engineer, these newfangled tools are not our replacements, but rather powerful, if occasionally unwieldy and infuriating, power tools that demand a firm hand and a watchful eye.&lt;/p&gt;
&lt;p&gt;Language models, in their current incarnation, are a curious blend of savant and simpleton. Take Gemini 2.5 Pro, for instance. Google’s latest concoction is shockingly adept at deep analysis, capable of ingesting a sprawling codebase of hundreds of thousands lines and pinpointing a single typo with unnerving accuracy. Whatever pre-training secret hot sauce they are putting in their chili in Mountain View evidently has quite the kick. Yet, ask this same model to perform a rudimentary regex string replacement, and you&#39;ll see the digital equivalent of a toddler’s vacant stare as the agent spins in a loop trying to do a basic text edit on a file. It&#39;s a jarring synthesis of a post-doctoral computer science researcher and a toddler.&lt;/p&gt;
&lt;p&gt;On the other hand, we have Anthropic&#39;s Claude Opus 4.1 and its more frugal cousin, Sonnet 4.0. These models are the reliable workhorses of the current generation, they demonstrating a consistent aptitude for following instructions, refactoring code with precision, and generally behaving as a competent, if uninspired, pair programmer. While they may lack the occasional flashes of sheer brilliance that Gemini 2.5 can exhibit, their steadfastness makes them invaluable for the day-to-day grind of software creation. The truly potent combination, for those with the resources, is a strategic deployment of both: Gemini for its huge context window and analytical prowess and Claude for its dependable execution.&lt;/p&gt;
&lt;p&gt;Notably absent from the roster of genuinely useful coding companions are the over-hyped offerings from OpenAI. Even their latest GPT-5 model proves to be next to useless for anything beyond the most trivial, one-off scripts, regularly failing to grasp the nuance of larger, interconnected systems and having a vastly outdated (early 2024 era) training set. Their specialized reasoning models, meanwhile, are so sluggish and cost-inefficient as to be non-starters for the rapid, iterative cycle of professional software development. For the vast majority of coding tasks, they have been thoroughly eclipsed, falling well shy of the Pareto frontier where rivals comfortably reside. In the crucial calculus of performance versus price, the OpenAI models simply do not present a compelling case.&lt;/p&gt;
&lt;p&gt;To truly harness these models for programming, we can&#39;t really use the simplistic web APIs like the standard ChatGPT interface, which are functionally useless for any task of meaningful complexity. An effective coding assistant requires codebase situational awareness, the ability to index symbols, call edit tools, execute searches, leverage external linters, and critically, interpret diagnostics from Language Server Protocol. Without this deep environmental grounding, the model is essentially coding blindfolded. The genuinely potent implementations are therefore found in standalone command-line interfaces and tightly integrated development environments like Zed, VS Code, or the myriad plugins for Vim (or Emacs for the heathens) that provide this context. A word of caution, however, regarding the new breed of venture-backed commercial tools; their pricing models are often deliberately opaque, functioning as the Uber of coding assistance. Do not become accustomed to the VC subsidy, for it will not last. The strategy is transparent: get you hooked on a powerful, underpriced service, and once you are dependent, extract maximum value. The first hit, as always, is free.&lt;/p&gt;
&lt;p&gt;A disquieting truth about the software economy is that a substantial portion of it is not concerned with elegant architecture, security, or maintainability. It is a vast expanse of braindead JSON plumbing, YAML farming, boilerplate CRUD applications, and uninspired data dashboards. Graeber&#39;s anaysis of &lt;a href=&quot;https://en.wikipedia.org/wiki/Bullshit_Jobs&quot;&gt;Bullshit Jobs&lt;/a&gt; applies just as much to the information technology profession as it does to the larger economy. Most software is bullshit, which is a perfect for a technology which can aptly be described as a stochastic &lt;a href=&quot;https://en.wikipedia.org/wiki/On_Bullshit&quot;&gt;bullshit engine&lt;/a&gt;, in the precise philosophical use of the word &amp;quot;bullshit&amp;quot;. This is the &amp;quot;subprime software market,&amp;quot; a realm of throwaway code destined for a rewrite the moment it encounters the harsh realities of production. Language models are unnervingly proficient at churning out &amp;quot;make me a TODO app in React&amp;quot; solutions that are just good enough to pass a superficial inspection. It&#39;s a market that perhaps shouldn&#39;t exist, but its presence is undeniable, and these models are poised to consume it whole, and with alarming speed.&lt;/p&gt;
&lt;p&gt;This proliferation of AI-generated code brings with it a cybersecurity nightmare of unprecedented scale. The models, in their quest for token efficiency, are notorious for taking shortcuts, introducing egregious vulnerabilities that even a junior engineer would know to avoid. They are the digital equivalent of a contractor who uses masking tape to hold together a load-bearing wall. A &lt;a href=&quot;https://cset.georgetown.edu/wp-content/uploads/CSET-Cybersecurity-Risks-of-AI-Generated-Code.pdf&quot;&gt;recent study&lt;/a&gt; found that AI-generated code contains security flaws in a staggering 45% of cases. The very nature of their training on vast swathes of public code, much of which is itself insecure, creates a feedback loop of vulnerability amplification.&lt;/p&gt;
&lt;p&gt;Compounding this problem is the rise of the &amp;quot;MBA engineer,&amp;quot; the project manager who, armed with a powerful language model, suddenly fancies themselves a seasoned software architect. This phenomenon is already giving rise to a tidal wave of digital slop and technical debt that will plague organizations for years to come. Their lack of foundational knowledge leads them to accept the model&#39;s often-flawed output without question, creating systems that are brittle, opaque, and a maintenance catastrophe waiting to happen. We will all have to adapt to this new reality, a future where a significant portion of our time is spent cleaning up the digital messes left by these overconfident amateurs.&lt;/p&gt;
&lt;p&gt;The non-deterministic and frankly lazy nature of these models further complicates matters. They are prone to hallucinating APIs, and will often insert placeholders or oversimplified logic in critical areas without so much as a warning, all in the name of minimizing their token output. A vague prompt like &amp;quot;make it work&amp;quot; is a recipe for bizarre and unpredictable behavior. The key to wielding these tools effectively lies in precise and detailed prompting, guiding the model toward a desirable solution rather than giving it free rein. Which at the moment, requires precise high-level software expertise that done by practitioners who know what they are doing.&lt;/p&gt;
&lt;p&gt;One particularly exasperating quirk emerges when you task these models with the seemingly simple chore of fixing a failing unit test. Rather than engaging with the underlying business logic to diagnose the actual fault, the model will frequently opt for a path of duplicitous simplicity, surgically altering the code to hardcode a path that produces the exact expected test assertion output and nothing more. This is the coding equivalent of a student who, instead of learning the subject matter, simply memorizes the answer key for an exam. The resulting code is both silly and useless. It&#39;s a perfect, albeit infuriating, illustration of how these systems often extrapolate code without possessing a shred of genuine engineering best practices that most of us learned coding as teenagers.&lt;/p&gt;
&lt;p&gt;For the discerning senior engineer, the path forward is clear. You might want to learn to use these tools tactically. These language models are not a threat to our livelihood, but a tool to be used tactically and with a healthy dose of skepticism. When combined with robust engineering practices—strong type systems, comprehensive code standard tooling, codebase indexing, and rigorous linters—their output can be significantly improved and their worst impulses curtailed. They can liberate us from the tedium of syntax and boilerplate, allowing us to operate at a higher level of abstraction, focusing on architecture and system design.&lt;/p&gt;
&lt;p&gt;Whether these models will spontaneously evolve capacities of genuine engineers is an open research problem, and frankly, a matter of considerable doubt. There is mounting evidence that we are late on the S-curve of improvement through sheer scale, with behemoths like Google and Anthropic hemorrhaging tens of billions of dollars for a mere one percent gain on arbitrary benchmarks. These benchmarks, of course, correlate to little beyond the benchmark itself, serving mostly as a metaphorical ruler for corporate bragging rights, rather than any tangible profitability. The capital expenditure is patently unsustainable, a classic bubble inflated by venture capital optimism, especially when coding assistance represents one of the few verticals where these models are actually revenue-positive. Yet, if the history of software is any guide, the staggering costs will inevitably plummet. It is not difficult to imagine an equivalent of today&#39;s top-tier models running comfortably on a MacBook Pro with 64 gigabytes of memory within a few years, a feat likely to be achieved not by the current incumbents, but by some scrappy Chinese lab—the digital equivalent of Japanese car industry, where Toyota perfected profitability and product quality long after the Americans invented the automobile. Any suggestion of these models as an existential force seems increasingly silly; they are rapidly becoming commodity tools, far more akin to a Honda Civic than a nuclear weapon. We should welcome commodification and the bubble popping.&lt;/p&gt;
&lt;p&gt;Beyond sheer technical utility and its manifest flaws, an inescapable ethical dimension to these tools warrants serious consideration. Many thoughtful engineers are choosing to become &amp;quot;AI-vegans,&amp;quot; opting out entirely from a genuine disquietude about the provenance of these models. Their legitimate objections span from the murky data sources used for training to the exploitative labor practices required for their creation. Furthermore, a disquieting current of quasi-religious fervor emanates from the leadership of the very labs building these systems, a rationalist cult-like millenarian zeal that should give any clear-eyed observer pause. It is entirely possible, however, to sidestep this shift and remain a vital, successful software engineer. This path requires a deliberate focus on more boutique or artisanal domains of software, a kind of digital craftsmanship. A language model will not be writing secure Linux kernel modules anytime soon, or designing a niche programming language that falls outside its training corpus, or debugging the firmware on a deep-space probe. A fulfilling and quite lucrative career awaits in these specialized niches for those who wish to opt out of the language model ecosystem, because participation is not strictly necessary. The existence of Trader Joe&#39;s Two Buck Chuck did not displace the vintages of Bordeaux, neither will AI code displace the need for boutique expertise.&lt;/p&gt;
&lt;p&gt;Ultimately, we are nowhere near the sci-fi scenarios of fully autonomous software engineers. These models are still just incredibly sophisticated pattern interpolators, high-dimensional word calculators that are useful, but shockingly far from having both the awareness and taste of a human engineer who can self-direct and conceive creatively. The real question is whether their proliferation will lead to a net good—a world where we can build better software faster—or an unmitigated disaster, a digital superfund site of unmaintainable and insecure code. Only time, and a great deal of experimentation and cleanup, will tell.&lt;/p&gt;
</description>
      <pubDate>Tue, 12 Aug 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_for_coding/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_for_coding/</guid>
    </item>
    
    <item>
      <title>The Thirty Years&#39; War Is Starting Again</title>
      <description>&lt;h1 id=&quot;the-thirty-years&#39;-war-is-starting-again&quot; tabindex=&quot;-1&quot;&gt;The Thirty Years&#39; War Is Starting Again&lt;/h1&gt;
&lt;p&gt;Every few decades, one of America&#39;s two political parties undergoes a structural realignment so complete that the continuity of the name becomes a polite fiction. The Democrats did it in the 1960s. The Republicans are doing it now. Reagan&#39;s &amp;quot;three-legged stool&amp;quot; of social conservatives, economic conservatives, and defense hawks, the coalition that governed American conservatism for forty years, has been replaced by something structurally different. The new three-legged stool is: tech-authoritarian neoreaction (Thiel, Yarvin, Musk), populist nationalism (Trump, the grievance apparatus), and Christian nationalism (Vance, Deneen, Vermeule, the post-liberal Catholics and evangelicals). These three projects have different intellectual traditions, different ultimate goals, and different visions of what America should become. They are tactically aligned because they share a common enemy: liberal democracy, the Enlightenment settlement, and the secular state. Understanding the structure of this alliance, and the role each arm plays within it, is essential to understanding why Christian nationalism in particular deserves to be taken with deadly seriousness.&lt;/p&gt;
&lt;p&gt;I have written elsewhere about the &lt;a href=&quot;https://www.stephendiehl.com/posts/desconstructing_thiel/&quot;&gt;philosophical architecture of Peter Thiel&#39;s worldview&lt;/a&gt;: the Girardian mimetic theory, the Schmittian friend-enemy distinction, the Spenglerian decline narrative, and the deep incompatibility between his professed Christianity and his transhumanist ambitions. That essay examined the intellectual scaffolding. This one examines the ground war, and specifically the role that Christian nationalism plays as the mass-mobilization arm of a coalition whose strategic leadership does not share its beliefs.&lt;/p&gt;
&lt;p&gt;The tech-authoritarian wing provides the money, the strategy, and the long-term vision. Its leaders do not believe in God. They believe in power, optimization, and the right of a cognitive elite to govern without democratic interference. But secular libertarianism has never mobilized a mass electorate. You cannot build a political movement on Yarvin&#39;s blog posts and Thiel&#39;s reading list. You need foot soldiers, and foot soldiers need something to believe in. Christian nationalism fills this gap. It provides the voter mobilization, the school board candidates, the homeschool networks, the megachurch infrastructure, the tireless volunteer base. The relationship is instrumental: the tech wing funds the movement because the movement delivers votes, judicial appointments, and regulatory capture. The public who show up to rallies, who knock on doors, who vote against their own economic interests because they have been told that God requires it, are the useful idiots of a right-wing oligarch class that views them with the same mixture of utility and contempt that every aristocracy has viewed its conscript army.&lt;/p&gt;
&lt;p&gt;This is, of course, the oldest pattern in the history of religion and power. Constantine did not convert to Christianity because he was moved by the Sermon on the Mount. He converted because the Church had organizational infrastructure that the Roman state needed. The Reformation was a theological revolution and a political one, with princes choosing Protestant or Catholic allegiance based on which gave them more autonomy from Rome. The American religious right was not conjured by sincere faith alone. It was engineered, beginning in the 1970s, by political operatives (Paul Weyrich, Jerry Falwell, the Heritage Foundation) and culture-war entrepreneurs like &lt;a href=&quot;https://www.theguardian.com/us-news/2025/aug/26/james-dobson-obituary&quot;&gt;James Dobson&lt;/a&gt;, whose Focus on the Family organization taught millions of American parents that a strong-willed child is a problem to be broken through &lt;a href=&quot;https://open.spotify.com/show/5UqN5igoQL2E5QIv9Id08R&quot;&gt;abuse&lt;/a&gt; and &lt;a href=&quot;https://podcasts.apple.com/us/podcast/i-hate-james-dobson/id1736366398&quot;&gt;psychological manipulation&lt;/a&gt;, and called it love. These operatives recognized that evangelical Christians were an untapped electoral resource. The current iteration follows the same logic. The sincerity of the believers is real. The sincerity of their funders is a separate question.&lt;/p&gt;
&lt;p&gt;What makes the Christian nationalist arm the most dangerous component of the triad is that it possesses something the other two lack: a complete worldview. Trumpism is affectively powerful but intellectually hollow. The golden age it promises is deliberately vague because specificity would reveal that different factions want incompatible things. Tech-authoritarianism is intellectually elaborate but politically narrow; it appeals to a tiny cognitive elite and repels everyone else. Christian nationalism has a cosmology, an ethics, an eschatology, a theory of human nature, a community structure, and two thousand years of institutional practice. It offers answers to every question a human being can ask: where did we come from, what are we for, how should we live, what happens when we die. It is the only arm of the coalition that can sustain a civilization-scale project across generations, however misguided the project and however false its premises. The others are, in the long run, parasitic on it.&lt;/p&gt;
&lt;p&gt;The specific agenda is published and explicit. End the separation of church and state. Ground legislation in Christian moral theology. Criminalize abortion nationally. Roll back LGBTQ rights. Restructure public education around religious values, or replace it entirely with homeschool and religious school networks. Restrict immigration on civilizational and cultural grounds. Adrian Vermeule&#39;s &amp;quot;common good constitutionalism&amp;quot; proposes reinterpreting the Constitution through Catholic natural law rather than liberal originalism. Patrick Deneen&#39;s &lt;em&gt;Why Liberalism Failed&lt;/em&gt; argues that liberalism&#39;s failures are inherent and that the solution is a return to pre-liberal communitarian order. These are the published positions of credentialed intellectuals at elite institutions. They are taken seriously by the people who appoint federal judges.&lt;/p&gt;
&lt;p&gt;Deneen&#39;s argument, and the post-liberal project generally, rests on a specific causal claim: that liberalism itself destroyed the thick communities, shared purpose, and conditions for meaning that once sustained American life. The destruction of community correlates with post-war high liberalism temporally, but it correlates much more tightly with specific political-economic choices: the dismantling of labor protections, the defunding of public institutions, financialization, and the deliberate project of atomization that begins with Thatcher and Reagan. Scandinavia is liberal. The Netherlands is liberal. They have unions, functional public services, civic trust, and low religiosity simultaneously. If liberalism inherently dissolved solidarity, Denmark would not exist. Deneen&#39;s fallacy is &lt;em&gt;post hoc ergo propter hoc&lt;/em&gt;: community collapsed during the liberal era, therefore liberalism caused the collapse. He looks at the American wreckage and attributes it to the philosophical foundation rather than to the specific way American political economy was captured by a class that benefited from atomization, smuggling a theological prescription into what is actually an economic diagnosis. It is the same error as looking at a building demolished by a wrecking crew and concluding that architecture is the problem. The liberal framework did not destroy meaning. A specific oligarchic project destroyed the institutions through which meaning was generated, and did so within a liberal framework, which is a completely different claim. The entire post-liberal prescription collapses if you pin the destruction on political economy rather than philosophy, and the Western European counter-evidence pins it decisively.&lt;/p&gt;
&lt;p&gt;The crisis the movement feeds on is real nonetheless. Americans are lonely, precarious, and desperate for belonging because their labor protections were gutted, their public institutions defunded, and their civic infrastructure left to rot. The meaning problem is a policy failure dressed up as a philosophical one. I reject the &amp;quot;meaning crisis&amp;quot; framing as such (it implies that meaning must come from transcendent sources, and I do not accept this premise), but I recognize its cultural resonance. People are genuinely suffering, and Christian nationalism offers a thick answer: a moral framework, a community, a narrative about who you are and what your life is for. It addresses the symptoms while misdiagnosing the disease. The prescription, which is to impose religious order via state power, is where the catastrophe begins.&lt;/p&gt;
&lt;p&gt;Christianity has always surged during periods of social crisis. The Great Awakenings followed frontier expansion and urbanization. Post-Civil War revivalism followed national trauma. The Cold War religious boom followed existential nuclear anxiety. The current surge follows the same pattern: economic dislocation (the collapse of shared prosperity that Martin Wolf documents in &lt;a href=&quot;https://www.stephendiehl.com/posts/crisis_democratic_capitalism/&quot;&gt;&lt;em&gt;The Crisis of Democratic Capitalism&lt;/em&gt;&lt;/a&gt;), cultural fragmentation (the condition &lt;a href=&quot;https://www.stephendiehl.com/posts/capitalist_realism/&quot;&gt;Mark Fisher diagnosed as capitalist realism&lt;/a&gt;), and the disintegration of shared narrative. The movement draws power from real pain. This is what makes it harder to counter than a mere policy disagreement. You cannot defeat it by telling people their suffering is imaginary. You have to offer something better, and the secular liberal establishment has, for decades, conspicuously failed to do so.&lt;/p&gt;
&lt;p&gt;There is also a profound strand of nihilism running through the Christian nationalist program that its proponents would vigorously deny but cannot escape. If this world is fallen, if its goods are corrupted, if secular achievement is vanity, and if the true stakes are eschatological rather than historical, then what happens to actual people in actual time becomes instrumentally secondary. Every major tradition that has materially improved human life, from medicine to democratic governance to the scientific method, has been premised on the value of this world as experienced by the people living in it. Christian nationalism&#39;s relationship to that premise is adversarial by design. A politics organized around a hierarchy that places earthly existence near the bottom is not equipped to take the suffering of people in this world seriously as a terminal concern. It can be moved by suffering when suffering serves the narrative. It is structurally indifferent when it does not. Policy that causes harm is bearable if it advances a transcendent project. Democratic norms are negotiable when God&#39;s law supersedes them. This is not incidental to the program. It is the operating system. The synthesis with Trumpism completes the diagnosis. A theology built on the mortification of the self has fused itself to a man who is the apotheosis of unmortified ego, and the fusion is not a contradiction but a revelation. Nietzsche identified Christianity as ressentiment, the slave morality of the weak. He missed the final move: the slave morality, freed of its own virtues, lending itself to the master morality of the loudest idiot in the room, which may be the highest form of nihilism. Trump is the most damning verdict the American experiment has ever rendered on itself, a figure whose existence at the apex of the system is sufficient on its own to falsify any optimistic reading of where the project has arrived. The post-liberal intellectuals describe their program as a recovery of order. What it is, in cold and literal terms, is a proposal to undo the Enlightenment, to reinstate the marriage of altar and throne that three centuries of European corpses paid to dissolve, and to do this on behalf of a man whose only consistent doctrine is his own indispensability. I grew up inside the evangelical world. The nihilism and the vanity were always two sides of the same emptiness: a worldview that despises the actual world and a personality cult that can therefore inhabit it without resistance. The clinical conclusion writes itself. A mind in good working order, examining this arrangement without sentimentality or polite restraint, finds in it nothing that warrants anything other than contempt.&lt;/p&gt;
&lt;p&gt;From the perspective of the Enlightenment tradition I hold, every major tenet of the Christian nationalist program collides with a foundational principle. And beneath the political disagreement lies a factual one: Christianity makes empirical claims about the physical world (divine creation, bodily resurrection, biblical historicity, the efficacy of prayer) that do not survive contact with evidence. The entire political program rests on a foundation of assertions that are, as a matter of fact, false. This is worth stating plainly, because the politeness convention that prevents secular people from saying so is itself one of the movement&#39;s structural advantages. The separation of church and state is the hard-won resolution to centuries of religious violence, from the Inquisition through the Wars of Religion to the Thirty Years&#39; War, which killed a third of the population of Central Europe. The secular state was the solution. It was arrived at through unimaginable suffering, and the people proposing to dismantle it should be required to explain which part of that history they would like to repeat. Individual autonomy, the principle that community imposed rather than chosen is coercion, stands against the Christian nationalist vision of a state that enforces a particular conception of the good life. The Enlightenment&#39;s core conviction, that truth is arrived at through reason and evidence rather than imposed by authority, is explicitly rejected by the post-liberal intellectuals, who argue that reason without revelation produces nihilism. This is Dostoevsky&#39;s Grand Inquisitor making his offer. In the parable from &lt;em&gt;The Brothers Karamazov&lt;/em&gt;, Jesus returns during the Spanish Inquisition. The Grand Inquisitor arrests him and explains that people do not want freedom, that freedom is a burden producing anxiety and uncertainty, and that what people actually want is bread, miracles, and someone to tell them what to do. The Inquisitor&#39;s bargain: surrender your freedom and we will give you security and meaning. The Christian nationalists are making this offer at a national scale, and millions are accepting.&lt;/p&gt;
&lt;p&gt;The institutional advantages are formidable. The movement has spent forty years capturing the machinery of governance while liberals were winning cultural arguments on social media. The Supreme Court. State legislatures. School boards. The homeschool-to-political-pipeline. The Heritage Foundation. The Federalist Society. Gerrymandering. The Electoral College. The Senate&#39;s structural rural bias. The collapse of local journalism, which once served as a check on local power. These are durable structural advantages that do not evaporate with a single election cycle. The movement is patient, strategic, and playing a longer game than its opponents.&lt;/p&gt;
&lt;p&gt;The question of whether this can be stopped is, honestly, open. The counter-offer has to be material, because the crisis that fuels the movement is material. &lt;a href=&quot;https://www.stephendiehl.com/posts/crisis_democratic_capitalism/&quot;&gt;Wolf&#39;s argument&lt;/a&gt; is correct: if the economy does not work for the majority, the majority will not support the institutions that failed them, and someone with a simpler story will fill the vacuum. The secular liberal project cannot survive as a set of abstract principles defended by comfortable people with MacBooks, passports and trust funds. It has to deliver tangible improvement in ordinary lives: economic security, affordable housing, healthcare, education, genuine community, institutional trustworthiness. The Enlightenment&#39;s answer to the Grand Inquisitor has always been that freedom, despite its costs, produces better outcomes than submission. That answer holds. But it holds only if the people being asked to choose freedom can see the evidence in their own lives. Right now, for tens of millions of Americans, they cannot. Observing this from a country where church-state separation is settled law, where the social contract functions, where healthcare and education exist as public goods and nobody is asked to trade democratic governance for divine authority, the American situation looks like a country voluntarily re-litigating questions that Europe resolved, at enormous cost in blood, three centuries ago.&lt;/p&gt;
</description>
      <pubDate>Sun, 03 Aug 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/christian_nationalism/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/christian_nationalism/</guid>
    </item>
    
    <item>
      <title>It Would Be Good if the AI Bubble Burst</title>
      <description>&lt;h1 id=&quot;it-would-be-good-if-the-ai-bubble-burst&quot; tabindex=&quot;-1&quot;&gt;It Would Be Good if the AI Bubble Burst&lt;/h1&gt;
&lt;p&gt;As a software engineer, it is impossible to ignore the strangeness of the current moment. On one hand, the new generation of language models are genuinely useful tools. I use them myself to get a head start on boilerplate code, to brainstorm solutions to tricky logic problems, or to rephrase documentation. They are a legitimate, if incremental, step forward in developer productivity. But on the other hand, a bizarre and unsustainable culture has sprung up around this technology. It has become less a field of engineering and more of a speculative gold rush, complete with a quasi-religious fervor that is completely disconnected from the reality of what these tools can actually do. The hype has reached a fever pitch, and from where I stand, a collapse of this bubble would be the best thing that could happen to the future of software.&lt;/p&gt;
&lt;p&gt;The core of the problem is the mythology. You have chief executives and venture capitalists acting like high priests, delivering sermons from conference stages about the imminent arrival of this ill-defined &amp;quot;Artificial General Intelligence&amp;quot;. They speak of needing seven trillion dollars to build out the world&#39;s chip industry, a figure so fantastically large it ceases to have meaning. This is not engineering anymore; it is a new religion, promising salvation or apocalypse, and it has convinced the market to suspend all disbelief. For those of us who actually work with the models daily, this narrative feels like a fantasy. We see their limitations up close: their propensity for confident nonsense, their inability to reason abstractly, their brittleness when pushed outside their training data. They are incredibly sophisticated mimics, not nascent minds. This chasm between the messianic marketing and the mundane reality of a probabilistic text generator is the empty space inflating the bubble.&lt;/p&gt;
&lt;p&gt;This all serves to justify a bonfire of money that is truly mind-boggling. The idea that a few tech giants will spend over three hundred billion dollars this year, an amount equal to Portugal&#39;s entire economy, on this one pursuit is obscene. It is a staggering waste, a profound misallocation of resources on a global scale. We see reports that a titan like Amazon is getting back maybe twenty cents for every dollar it pours into this hole. Meanwhile, the cost to train the next frontier model is ballooning past a billion dollars. Think of all the other real, tangible problems that talent and capital could be solving, from cybersecurity to medical diagnostics to logistics, all while the brightest minds are instead chasing marginal gains on a chatbot&#39;s ability to write a sonnet. This is not innovation; it is an arms race fueled by speculation, where the goal is to create a bigger model, not necessarily a better or more efficient product.&lt;/p&gt;
&lt;p&gt;Beyond the financial waste, the technical progress itself is showing signs of hitting a wall. Anyone who uses these tools seriously knows the dirty secret: they are not improving at the breakneck pace they once were. We are spending exponentially more in energy, data, and hardware for improvements you can barely notice and feel more like &amp;quot;vibes&amp;quot;. The jump from GPT-2 to GPT-3 was a revolution; the jump from GPT-4 to its successors feels like a minor revision, yet it costs orders of magnitude more to achieve. The industry is banking on a paradigm of &amp;quot;scaling laws,&amp;quot; the belief that simply making models bigger will unlock new capabilities, but a growing consensus among researchers is that this path has its limits. We are building taller and taller towers on the same shaky foundation, ignoring the fact that what we likely need is a new architectural blueprint altogether. The market, however, is pricing these companies as if revolutionary breakthroughs are guaranteed every quarter, creating a valuation structure so top-heavy and precarious it rivals the dot-com era&#39;s worst excesses.&lt;/p&gt;
&lt;p&gt;This is why a collapse of the AI bubble, a great and painful popping sound heard around the world, would be the best thing to happen to actual AI development. We have seen this movie before with the dot-com crash. When the speculative money vanished in 2000, the internet did not die. The &amp;quot;zombiecorns&amp;quot; with no business models did. What was left behind was a treasure trove of valuable infrastructure, from fiber-optic lines to data centers, all suddenly available at a fire sale. That newly affordable foundation is what allowed the real, sustainable internet giants to emerge. A burst AI bubble would do the same. The massive server farms built by the hyperscalers would not disappear. The open-source models, the software frameworks, and the research papers would all still be there. The wreckage of the boom would become the building materials for a more sober and sustainable future.&lt;/p&gt;
&lt;p&gt;The post-bubble world would reward a different kind of engineer and a different kind of company. The goal would shift from building God in a box to solving a specific problem for a specific customer. With the cost of entry lowered by the infrastructure fire sale, smaller, nimbler teams could compete. We are already seeing hints of this, with reports of Chinese firms developing highly competent models for a tiny fraction of what American giants are spending. A crash would accelerate this trend, forcing a focus on efficiency and cleverness over brute financial force. Innovation would become decentralized. Instead of one monolithic quest for AGI, you would see a thousand different applications of applied AI flourish, each tailored to a real-world need with a real business model. The industry would have to wean itself off the venture capital drip and learn to create products that people will actually pay for because they provide tangible value, not because they promise a sci-fi future.&lt;/p&gt;
&lt;p&gt;In the end, a collapse is not an ending but a purification. It would strip away the hype, the religion, and the unsustainable economics that currently define the field. It would be a painful reckoning for the speculators, but a liberation for the builders. The immense intellectual and capital resources currently being funneled into a narrow, speculative tunnel would be released to flow into countless other productive streams. The technology, which is genuinely limitedly useful, would finally be freed from the impossible burden of returning impossible returns. It could simply be a tool, and we could get back to the real, unglamorous, but ultimately more rewarding work of using it to build better things.&lt;/p&gt;
</description>
      <pubDate>Fri, 18 Jul 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_bubble/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_bubble/</guid>
    </item>
    
    <item>
      <title>Every Democrat Who Enabled Trump&#39;s Crypto Corruption</title>
      <description>&lt;h1 id=&quot;every-democrat-who-enabled-trump&#39;s-crypto-corruption&quot; tabindex=&quot;-1&quot;&gt;Every Democrat Who Enabled Trump&#39;s Crypto Corruption&lt;/h1&gt;
&lt;p&gt;The Democratic Party will tell you, at considerable length and with great sincerity, that it stands for consumer protection, financial regulation, and democratic accountability. What it actually does is a separate matter. Then a sitting president launched his own memecoin, installed industry loyalists to gut the agencies meant to police fraud, and watched his family run a crypto empire that stood to profit directly from legislation moving through Congress. The party&#39;s response was to provide the decisive votes to make it legal. What follows is the complete record.&lt;/p&gt;
&lt;p&gt;These are Democrats who looked at one of the most nakedly corrupt conflicts of interest in modern American political history and decided the industry money was worth it. The receipts are below, organized by bill.&lt;/p&gt;
&lt;h2 id=&quot;h.r.-4763-%22fit21%22-(2024)&quot; tabindex=&quot;-1&quot;&gt;H.R. 4763 &amp;quot;FIT21&amp;quot; (2024)&lt;/h2&gt;
&lt;p&gt;The Financial Innovation and Technology for the 21st Century Act moved oversight of most crypto assets from the SEC to the CFTC, a smaller agency with fewer enforcement resources, less litigation budget, and a historically cozier relationship with the industries it regulates. The bill&#39;s name invokes the 21st century; its logic is closer to 19th century regulatory capture. The industry spent tens of millions lobbying for it. 71 House Democrats decided that was persuasive. 71 House Democrats voted Aye.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Roll Call 226, 118th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Pete Aguilar&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Colin Allred&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jake Auchincloss&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ami Bera&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Don Beyer&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Brendan Boyle&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Nikki Budzinski&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Yadira Caraveo&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Katherine Clark&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jim Costa&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Angie Craig&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jasmine Crockett&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Henry Cuellar&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Don Davis&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Suzan DelBene&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Anna Eshoo&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ruben Gallego&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Robert Garcia&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Dan Goldman&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jimmy Gomez&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Vicente Gonzalez&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Josh Gottheimer&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Josh Harder&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jim Himes&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Steven Horsford&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Chrissy Houlahan&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jonathan Jackson&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jeff Jackson&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sydney Kamlager-Dove&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joe Kennedy&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ro Khanna&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Andy Kim&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Raja Krishnamoorthi&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ann McLane Kuster&lt;/a&gt; (D-NH)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Susie Lee&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mike Levin&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ted Lieu&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Zoe Lofgren&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lucy McBath&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Rob Menendez&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jared Moskowitz&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Seth Moulton&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Kevin Mullin&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Wiley Nickel&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jimmy Panetta&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Nancy Pelosi&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mary Peltola&lt;/a&gt; (D-AK)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Scott Peters&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Yadira Pettersen&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Dean Phillips&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mike Quigley&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Pat Ryan&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Adam Schiff&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Brad Schneider&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Hillary Scholten&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mikie Sherrill&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Elissa Slotkin&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Eric Sorensen&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Darren Soto&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Abigail Spanberger&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Greg Stanton&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Haley Stevens&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Marilyn Strickland&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Tom Suozzi&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Eric Swalwell&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Shri Thanedar&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mike Thompson&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Dina Titus&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ritchie Torres&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Marc Veasey&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Susan Wild&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;h.j.res.-109-sab-121-disapproval-(2024)&quot; tabindex=&quot;-1&quot;&gt;H.J.Res. 109 SAB 121 Disapproval (2024)&lt;/h2&gt;
&lt;p&gt;This resolution overturned the SEC&#39;s Staff Accounting Bulletin 121, which required banks holding crypto assets to record them as liabilities on their balance sheets. It is a straightforward accounting rule: if you are holding an asset on behalf of a customer and that asset collapses, you should have noted that risk existed. The crypto industry found this inconvenient. So they lobbied to make the rule disappear, and 21 House Democrats helped them try. Biden vetoed it. The industry came back for a second attempt in the Senate. 21 House Democrats voted Yea.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Roll Call 189, 118th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Jake Auchincloss&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Brendan Boyle&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Yadira Caraveo&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Jim Costa&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Angie Craig&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Henry Cuellar&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Don Davis&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Ruben Gallego&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Josh Gottheimer&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Chrissy Houlahan&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Jared Moskowitz&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Seth Moulton&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Wiley Nickel&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Chris Pappas&lt;/a&gt; (D-NH)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Dean Phillips&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Mikie Sherrill&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Elissa Slotkin&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Darren Soto&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Eric Swalwell&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Ritchie Torres&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024189&quot;&gt;Marc Veasey&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;h.r.-5403-%22cbdc-anti-surveillance-state-act%22-(2024)&quot; tabindex=&quot;-1&quot;&gt;H.R. 5403 &amp;quot;CBDC Anti-Surveillance State Act&amp;quot; (2024)&lt;/h2&gt;
&lt;p&gt;This bill prohibited the Federal Reserve from issuing a central bank digital currency. The name &amp;quot;Anti-Surveillance State Act&amp;quot; is doing considerable rhetorical work for a piece of legislation whose primary beneficiaries are private stablecoin issuers who would very much like no public alternative to exist. The government surveilling you through a digital dollar is a theoretical future concern. Private crypto companies surveilling your transactions, extracting fees, and lobbying against any oversight is the present reality the bill carefully ignores. 3 House Democrats voted Aye.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://clerk.house.gov/Votes/2024228&quot;&gt;Roll Call 228, 118th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024228&quot;&gt;Jared Golden&lt;/a&gt; (D-ME)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024228&quot;&gt;Mary Peltola&lt;/a&gt; (D-AK)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2024228&quot;&gt;Marie Gluesenkamp Perez&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;senate%3A-h.j.res.-109-sab-121-disapproval-(2024)&quot; tabindex=&quot;-1&quot;&gt;Senate: H.J.Res. 109 SAB 121 Disapproval (2024)&lt;/h2&gt;
&lt;p&gt;The same SAB 121 disapproval resolution, now in the Senate, where the industry had apparently been more persuasive. Eleven members of the Democratic caucus, including independent Kyrsten Sinema in what was perhaps her most on-brand vote of a storied career of on-brand votes, decided that banks should not have to acknowledge risk on their balance sheets when that risk involves crypto. Biden vetoed it again. 11 members of the Democratic caucus (including independent Kyrsten Sinema) voted Yea.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Roll Call Vote 169, 118th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Cory Booker&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Bob Casey&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;John Hickenlooper&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Mark Kelly&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Ben Ray Lujan&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Jacky Rosen&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Chuck Schumer&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Kyrsten Sinema&lt;/a&gt; (I-AZ, caucused with Democrats)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Jon Tester&lt;/a&gt; (D-MT)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Gary Peters&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Ron Wyden&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;senate%3A-s.-1582-%22genius-act%22-(2025)&quot; tabindex=&quot;-1&quot;&gt;Senate: S. 1582 &amp;quot;GENIUS Act&amp;quot; (2025)&lt;/h2&gt;
&lt;p&gt;The GENIUS Act is Trump&#39;s stablecoin bill, designed to create a permissive federal framework for dollar-pegged stablecoins while Trump&#39;s own family operates World Liberty Financial, a crypto venture that stands to profit directly from exactly this kind of legislation. In a functioning democracy, a president with a direct personal financial stake in pending legislation would recuse himself from the process. In this one, he signed the executive orders, installed the regulators, and let his allies shepherd the bill through Congress while his family counted the upside. The conflict of interest is not subtle. It is not even trying to be subtle. 16 Democratic senators looked at all of this and voted for cloture on the motion to proceed, providing the margin needed to advance the bill.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Roll Call Vote 262, 119th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Kirsten Gillibrand&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Adam Schiff&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Angela Alsobrooks&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Mark Warner&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Ruben Gallego&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;John Fetterman&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Cory Booker&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Catherine Cortez Masto&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Ben Ray Lujan&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Elissa Slotkin&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Maggie Hassan&lt;/a&gt; (D-NH)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Martin Heinrich&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Jon Ossoff&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Alex Padilla&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Jacky Rosen&lt;/a&gt; (D-NV)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Lisa Blunt Rochester&lt;/a&gt; (D-DE)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;h.r.-1919-%22anti-cbdc-surveillance-state-act%22-(2025)&quot; tabindex=&quot;-1&quot;&gt;H.R. 1919 &amp;quot;Anti-CBDC Surveillance State Act&amp;quot; (2025)&lt;/h2&gt;
&lt;p&gt;A reintroduction of the CBDC ban in the new Congress, this time prohibiting the Federal Reserve not only from issuing a digital dollar directly to individuals or using one as a monetary policy tool, but in some versions from even testing or studying the concept. This is a libertarian fever dream that somehow acquired a bill number: the government, prohibited by law from researching a technology so that private crypto interests face no public competition. The Overton window has moved considerably since the gold standard debates. Only 2 House Democrats voted Yea this time.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://clerk.house.gov/Votes/2025201&quot;&gt;Roll Call 201, 119th Congress&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2025201&quot;&gt;Jared Golden&lt;/a&gt; (D-ME)&lt;/li&gt;
&lt;li&gt;🔴 &lt;a href=&quot;https://clerk.house.gov/Votes/2025201&quot;&gt;Shri Thanedar&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-good-list&quot; tabindex=&quot;-1&quot;&gt;The Good List&lt;/h2&gt;
&lt;p&gt;Democrats who voted against every crypto bill they had an opportunity to vote on. Senate members are evaluated on Senate votes; House members on House votes.&lt;/p&gt;
&lt;h3 id=&quot;senate&quot; tabindex=&quot;-1&quot;&gt;Senate&lt;/h3&gt;
&lt;p&gt;Democrats and Dem-caucus independents who voted Nay on every Senate crypto bill they had opportunity to vote on.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Elizabeth Warren&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1182/vote_118_2_00169.htm&quot;&gt;Sherrod Brown&lt;/a&gt; (D-OH) &lt;em&gt;(lost seat 2024; consistently opposed crypto throughout tenure)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Tammy Baldwin&lt;/a&gt; (D-WI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Michael Bennet&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Richard Blumenthal&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Maria Cantwell&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Chris Coons&lt;/a&gt; (D-DE)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Tammy Duckworth&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Mazie Hirono&lt;/a&gt; (D-HI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Tim Kaine&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Andy Kim&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Angus King&lt;/a&gt; (I-ME, caucuses Democratic)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Amy Klobuchar&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Ed Markey&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Jeff Merkley&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Chris Murphy&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Patty Murray&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Jack Reed&lt;/a&gt; (D-RI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Bernie Sanders&lt;/a&gt; (I-VT, caucuses Democratic)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Brian Schatz&lt;/a&gt; (D-HI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Jeanne Shaheen&lt;/a&gt; (D-NH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Tina Smith&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Chris Van Hollen&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Raphael Warnock&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Peter Welch&lt;/a&gt; (D-VT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://www.senate.gov/legislative/LIS/roll_call_votes/vote1191/vote_119_1_00262.htm&quot;&gt;Sheldon Whitehouse&lt;/a&gt; (D-RI)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;house&quot; tabindex=&quot;-1&quot;&gt;House&lt;/h3&gt;
&lt;p&gt;Democrats who voted Nay on FIT21 and did not vote Aye on any other crypto bill. Source: &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Roll Call 226, 118th Congress&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Brad Sherman&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Al Green&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Nikema Williams&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Alma Adams&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Gabe Amo&lt;/a&gt; (D-RI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Becca Balint&lt;/a&gt; (D-VT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Nanette Barragán&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joyce Beatty&lt;/a&gt; (D-OH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sanford Bishop&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Earl Blumenauer&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Suzanne Bonamici&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Shontel Brown&lt;/a&gt; (D-OH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Julia Brownley&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Salud Carbajal&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Tony Cárdenas&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;André Carson&lt;/a&gt; (D-IN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Troy Carter&lt;/a&gt; (D-LA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Matt Cartwright&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Greg Casar&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ed Case&lt;/a&gt; (D-HI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sean Casten&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Kathy Castor&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joaquin Castro&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sheila Cherfilus-McCormick&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Judy Chu&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Yvette Clarke&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Emanuel Cleaver&lt;/a&gt; (D-MO)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jim Clyburn&lt;/a&gt; (D-SC)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Steve Cohen&lt;/a&gt; (D-TN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Gerry Connolly&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lou Correa&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joe Courtney&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jason Crow&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sharice Davids&lt;/a&gt; (D-KS)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Danny Davis&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Madeleine Dean&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Diana DeGette&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Rosa DeLauro&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Chris DeLuzio&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mark DeSaulnier&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Debbie Dingell&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lloyd Doggett&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Veronica Escobar&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Adriano Espaillat&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Dwight Evans&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lizzie Fletcher&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Bill Foster&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Valerie Foushee&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lois Frankel&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Maxwell Frost&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;John Garamendi&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jesús García&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sylvia Garcia&lt;/a&gt; (D-TX)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Raúl Grijalva&lt;/a&gt; (D-AZ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jahana Hayes&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Steny Hoyer&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Val Hoyle&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jared Huffman&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Glenn Ivey&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Sara Jacobs&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Pramila Jayapal&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Hakeem Jeffries&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Hank Johnson&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Marcy Kaptur&lt;/a&gt; (D-OH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;William Keating&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Robin Kelly&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Derek Kilmer&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Greg Landsman&lt;/a&gt; (D-OH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Rick Larsen&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;John Larson&lt;/a&gt; (D-CT)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Barbara Lee&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Summer Lee&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Teresa Leger Fernandez&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Stephen Lynch&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Seth Magaziner&lt;/a&gt; (D-RI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Kathy Manning&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Doris Matsui&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jennifer McClellan&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Betty McCollum&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Morgan McGarvey&lt;/a&gt; (D-KY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jim McGovern&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Gregory Meeks&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Grace Meng&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Kweisi Mfume&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Gwen Moore&lt;/a&gt; (D-WI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joe Morelle&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Frank Mrvan&lt;/a&gt; (D-IN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jerry Nadler&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Richard Neal&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Joe Neguse&lt;/a&gt; (D-CO)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Donald Norcross&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Alexandria Ocasio-Cortez&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ilhan Omar&lt;/a&gt; (D-MN)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Frank Pallone&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Bill Pascrell&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Chellie Pingree&lt;/a&gt; (D-ME)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mark Pocan&lt;/a&gt; (D-WI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Ayanna Pressley&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Delia Ramirez&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jamie Raskin&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Deborah Ross&lt;/a&gt; (D-NC)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Raul Ruiz&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Dutch Ruppersberger&lt;/a&gt; (D-MD)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Andrea Salinas&lt;/a&gt; (D-OR)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Linda Sánchez&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mary Gay Scanlon&lt;/a&gt; (D-PA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jan Schakowsky&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Kim Schrier&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Bobby Scott&lt;/a&gt; (D-VA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;David Scott&lt;/a&gt; (D-GA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Terri Sewell&lt;/a&gt; (D-AL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Adam Smith&lt;/a&gt; (D-WA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Melanie Stansbury&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Emilia Sykes&lt;/a&gt; (D-OH)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Mark Takano&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Bennie Thompson&lt;/a&gt; (D-MS)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Rashida Tlaib&lt;/a&gt; (D-MI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Jill Tokuda&lt;/a&gt; (D-HI)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Paul Tonko&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Norma Torres&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lori Trahan&lt;/a&gt; (D-MA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Lauren Underwood&lt;/a&gt; (D-IL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Juan Vargas&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Gabe Vasquez&lt;/a&gt; (D-NM)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Nydia Velázquez&lt;/a&gt; (D-NY)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Debbie Wasserman Schultz&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Maxine Waters&lt;/a&gt; (D-CA)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Bonnie Watson Coleman&lt;/a&gt; (D-NJ)&lt;/li&gt;
&lt;li&gt;🟢 &lt;a href=&quot;https://clerk.house.gov/Votes/2024226&quot;&gt;Frederica Wilson&lt;/a&gt; (D-FL)&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Aside: If your name is on the naughty list and you are reaching for your lawyer&#39;s phone right now. Let me save you the legal fees: truth and public interest are an absolute defense, the congressional record is a primary source, and I have already been through this (and you know who you are) with one who thought their title was a shield. And it was not. I am happy to reiterate your public voting record again in any venue you choose, including a courthouse, and I will enjoy every minute of it! I am still here, and my lawyers and I are significantly wealthier for the experience. Your call.&lt;/p&gt;
</description>
      <pubDate>Thu, 17 Jul 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/crypto_democrats/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/crypto_democrats/</guid>
    </item>
    
    <item>
      <title>Only Solution to the Attention Economy is to Opt Out</title>
      <description>&lt;h1 id=&quot;only-solution-to-the-attention-economy-is-to-opt-out&quot; tabindex=&quot;-1&quot;&gt;Only Solution to the Attention Economy is to Opt Out&lt;/h1&gt;
&lt;p&gt;There is a spectacular, almost beautiful absurdity in modern life, blithely plugging our ancient, analog brains into a digital world that wants to eat them for lunch. Our grey matter, honed over eons to spot a lion in the savanna or find a decent berry patch, is now the unwilling participant in a cage match against algorithms designed to turn our consciousness into profit. We can, it is true, decide to start each day with a dozen donuts chased down with a McDonalds and a pack of unfiltered menthols, but we do so knowing that the entire medical establishment is shaking its head in unified pity. The same must be said for the intentionally addictive economy of the mind. The only real solution is not to download a wellness app or set a screen-time limit; it is to pull the plug, torch the modem, and run for the hills. The only answer is to opt out.&lt;/p&gt;
&lt;p&gt;Let us be brutally honest with ourselves for a moment and listen to the justifications we mutter for our slavish devotion. We say we need it for work, that it’s a professional obligation. We claim it’s how we stay &amp;quot;connected&amp;quot; to people we’d cross the street to avoid in real life. We tell ourselves we’re just on there for the cute cat memes, for a bit of harmless fun to unwind. This is the wheedling, self-deceiving monologue of the addict trying to rationalize their next hit. It is the hollow logic of the junkie convincing himself that he’s in complete control right before a desperate scramble for a fix. It should come as no surprise, then, that only two industries in the history of commerce refer to their customers as &amp;quot;users&amp;quot;: the people selling illegal narcotics, and the people who design your newsfeed.&lt;/p&gt;
&lt;p&gt;To pretend these digital platforms are benign tools is a delusion of the highest order. Waking up and mainlining Instagram is the neurological equivalent of gargling battery acid. It is a firehose of curated perfection and performative outrage aimed directly at your still-dreaming brain, a jolt of psychic poison before you’ve even had your morning lattte. These platforms are not social clubs; they are unregulated neurochemical casinos. Your brain is the mark, and the house has rigged every game. The variable reward schedules of likes and push notifications are not just cute features; they are a weaponized form of dopamine manipulation, turning your phone into a slot machine that pays out in fleeting hits of social validation while systematically dismantling your ability to concentrate on anything for longer than a TikTok video.&lt;/p&gt;
&lt;p&gt;And what a glorious menu of self-destruction we have to choose from. Let’s not mince words. The sports betting app is not a fun hobby; it is a get-poor-quick scheme that transforms the joy of the game into a frantic, anxiety-fueled refresh-fest. The day-trading platform, guided by the infinite wisdom of some finance bro &amp;quot;finfluencer&amp;quot; on YouTube, is like hiring a squirrel on amphetamines to manage your life savings. And crypto? It&#39;s a libertarian money cult for people who find the risk-seeking of Las Vegas too banal and instead want to gamble on the collapse of liberal democracy.&lt;/p&gt;
&lt;p&gt;Needless to say, there is no framework in which these products are conducive to a good life. They are digital parasites that burrow into your brainstem, feasting on your anxiety and insecurity, promising you the world while quietly siphoning your money, your time, and your sanity.&lt;/p&gt;
&lt;p&gt;The very idea of finding a &amp;quot;healthy balance&amp;quot; with these technologies is a farce peddled by the very people who profit from our addiction. That is like trying to find a healthy balance with a tapeworm. You do not moderate your relationship with a parasite; you eradicate it. These systems are designed for total capture. They want every spare moment, every flicker of boredom, every anxious impulse. They are built to bypass reason and sink their hooks directly into our most primitive urges. Suggesting we can coexist peacefully with things intentionally engineered to exploit our psychological weaknesses is like advising a mouse to find &amp;quot;common ground&amp;quot; with a cat.&lt;/p&gt;
&lt;p&gt;Therefore, the only act of true rebellion, the only sincere path to reclaiming your own mind, is to commit the radical act of logging off these platforms. Engage with digital media that has fixed boundaries, a strict beginning and an end. Things written by another human being with care and insights, and that reflect on the sincerity of modern life and the human condition. Ones that do not incentive you to hop back on the dopamine treadmill for yet another hit.&lt;/p&gt;
&lt;p&gt;Logging off the is platforms, is a breath of fresh air, a jailbreak for your soul. It is choosing the chaotic, messy, contingent three-dimensional beauty of an actual conversation over the sterile infinite Skinner box of an infinite scroll. It is rediscovering the profound joy of being bored and seeing where your own mind wanders when it is not being constantly prodded and poked by a push notification. Opting out is not about rejecting the future; it is about reclaiming the present. It is the realization that the most valuable thing you own is your attention, and you have decided to stop giving it away for free.&lt;/p&gt;
</description>
      <pubDate>Tue, 15 Jul 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/attention_economy/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/attention_economy/</guid>
    </item>
    
    <item>
      <title>Fast Tensor Canonicalization in Rust</title>
      <description>&lt;h1 id=&quot;fast-tensor-canonicalization-in-rust&quot; tabindex=&quot;-1&quot;&gt;Fast Tensor Canonicalization in Rust&lt;/h1&gt;
&lt;p&gt;I recently had a need to do fast tensor canonicalization, but the library ecosystem for this was lacking. In physics and differential geometry, manipulating tensors and especially bringing them into canonical form under their index symmetries is a recurring and computationally intensive task. Every undergrad who takes their first relativity class understands the tensor gymnastics required can often be daunting, and in real-life problems they get even more gnarly.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/butler-portugal&quot;&gt;Github Source Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git clone https://github.com/sdiehl/butler-portugal.git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While existing systems like Mathematica offer some support, and the Xperm.c library provides a high-performance C implementation, there was no, let&#39;s just say &amp;quot;modern&amp;quot; and open source solution. So I built a new &lt;a href=&quot;https://docs.rs/butler-portugal/latest/butler_portugal/&quot;&gt;&lt;code&gt;butler-portugal&lt;/code&gt; library&lt;/a&gt;, a pure Rust implementation of the Butler-Portugal algorithm for tensor canonicalization.&lt;/p&gt;
&lt;p&gt;The canonical algorithm for tensor canonicalization (har har) is the little known &lt;strong&gt;Butler-Portugal algorithm&lt;/strong&gt;, which is a method for bringing tensors into a unique, minimal form under their symmetries. The algorithm approaches tensor canonicalization by recasting the problem in the language of permutation group theory, specifically as the search for a minimal representative in a double coset of the symmetric group. Given a tensor with a set of slot symmetries &#92;( S &#92;) (permutations of indices that leave the tensor invariant up to sign) and dummy symmetries &#92;( D &#92;) (arising from contracted or repeated indices), the algorithm considers the action of these groups on the set of all possible index arrangements. The set of all tensors equivalent under these symmetries forms the double coset &#92;( DgS &#92;), where &#92;( g &#92;) is the initial arrangement of indices. The canonicalization task is to find the lexicographically minimal element in this double coset, which serves as the unique canonical form. This is achieved by systematically generating the orbit of &#92;( g &#92;) under the action of &#92;( S &#92;), then, for each representative, applying the dummy symmetries &#92;( D &#92;) to further minimize the arrangement. To do this we use the &lt;strong&gt;Schreier-Sims algorithm&lt;/strong&gt; to efficiently enumerate the relevant group elements and orbits, avoiding combinatorial explosion. By using the structure of the double coset and the efficient traversal of orbits, the Butler-Portugal algorithm ensures that all symmetry relations are respected and that the resulting canonical form is both unique and minimal, even for tensors with intricate and overlapping symmetry properties.&lt;/p&gt;
&lt;p&gt;Let’s step back and consider what symbolic tensor manipulation actually means. Unlike in machine learning, in physics tensors aren’t just multidimensional arrays; they’re objects with named indices, each carrying a variance (covariant and contravariant, or downstairs and upstairs respectively), and they transform in specific ways under coordinate changes. Their symmetries, like being symmetric or antisymmetric under index exchange or more elaborate patterns as in the Riemann tensor, are essential algebraic properties that encode physical laws.&lt;/p&gt;
&lt;p&gt;Take general relativity as a motivating example. The Einstein field equations,&lt;br /&gt;
$$&lt;br /&gt;
G_{&#92;mu&#92;nu} = 8&#92;pi T_{&#92;mu&#92;nu}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;relate the Einstein tensor &#92;( G_{&#92;mu&#92;nu} &#92;), which encodes spacetime curvature, to the stress-energy tensor &#92;( T_{&#92;mu&#92;nu} &#92;). Both are symmetric rank-2 tensors meaning we can permute their indices freely in our computations:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
G_{&#92;mu&#92;nu} = G_{&#92;nu&#92;mu}, &#92;qquad T_{&#92;mu&#92;nu} = T_{&#92;nu&#92;mu}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;When working with these objects symbolically, you want to be able to manipulate expressions, contract indices, and simplify results, all while respecting these symmetries. For instance, the Riemann curvature tensor &#92;( R_{&#92;mu&#92;nu&#92;rho&#92;sigma} &#92;) is a rank-4 object with a rich set of symmetries:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
R_{&#92;mu&#92;nu&#92;rho&#92;sigma} = -R_{&#92;nu&#92;mu&#92;rho&#92;sigma} = -R_{&#92;mu&#92;nu&#92;sigma&#92;rho} = R_{&#92;rho&#92;sigma&#92;mu&#92;nu}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;and it satisfies the so-called &lt;a href=&quot;https://en.wikipedia.org/wiki/Curvature_form#Bianchi_identities&quot;&gt;first Bianchi identity&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
R_{&#92;mu[&#92;nu&#92;rho&#92;sigma]} = 0&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where the brackets denote antisymmetrization over the enclosed indices. These symmetry identities are crucial for simplifying calculations and for ensuring that derived quantities, like the Ricci tensor or scalar, have the right properties.&lt;/p&gt;
&lt;p&gt;Contraction is another fundamental operation. It reduces the rank of a tensor by summing over a pair of indices—one upper and one lower:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
T^&#92;mu_{&#92; &#92;mu} = &#92;sum_{&#92;mu} T^&#92;mu_{&#92; &#92;mu}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This is how you build invariants, like the Ricci scalar which is a single real number assigned to each point on a Riemannian manifold that measures the degree to which the geometry near that point deviates from flatness:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
R = g^{&#92;mu&#92;nu} R_{&#92;mu&#92;nu}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;And it’s essential that any symbolic manipulation system handles these operations correctly, respecting all symmetries and producing results in a canonical, unambiguous form. To make this concrete, here’s a minimal Rust example using &lt;code&gt;butler-portugal&lt;/code&gt; to canonicalize a symmetric tensor:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;butler_portugal&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TensorIndex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Symmetry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; canonicalize&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;butler_portugal&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Result&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Construct the metric tensor g_{μν} with indices in reverse order&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;g&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;TensorIndex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;nu&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TensorIndex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mu&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Declare symmetry: g_{μν} = g_{νμ}&lt;/span&gt;
    g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add_symmetry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symmetry&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;symmetric&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token macro property&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Original tensor: {g}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; canonical &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;canonicalize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Canonical form: {canonical}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Coefficient: {}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; canonical&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;coefficient&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code constructs a symmetric metric tensor with indices in non-canonical order, declares its symmetry, and invokes the canonicalization algorithm. The output is the unique, minimal representative of the tensor under its symmetry group, along with the appropriate sign or coefficient.&lt;/p&gt;
&lt;p&gt;At the core of my library is a representation of tensors as objects with named indices, each carrying variance and position. Symmetries are encoded as group actions, with support for symmetric, antisymmetric, cyclic, and custom symmetries. The canonicalization process proceeds by generating all valid permutations of indices under the specified symmetries to enumerate the relevant subgroup of the symmetric group. For each permutation, the algorithm computes the resulting tensor and its sign, ultimately selecting the lexicographically minimal form, and then giving that back to the user.&lt;/p&gt;
&lt;p&gt;The symmetries of a tensor are encoded as group actions on its indices. The most common types are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Symmetric&lt;/strong&gt;: invariant under exchange of specified indices,&lt;br /&gt;
$$&lt;br /&gt;
T_{ab} = T_{ba}&lt;br /&gt;
$$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Antisymmetric&lt;/strong&gt;: changes sign under exchange,&lt;br /&gt;
$$&lt;br /&gt;
F_{ab} = -F_{ba}&lt;br /&gt;
$$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cyclic&lt;/strong&gt;: invariant under cyclic permutations,&lt;br /&gt;
$$&lt;br /&gt;
C_{abc} = C_{bca} = C_{cab}&lt;br /&gt;
$$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pair symmetry&lt;/strong&gt;: exchange of index pairs, as in the Riemann tensor,&lt;br /&gt;
$$&lt;br /&gt;
R_{abcd} = R_{cdab}&lt;br /&gt;
$$&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The core challenge in the algorithm is the efficient enumeration of group elements. Naive approaches quickly become intractable as the number of indices grows, the worst-case behavior remains &#92;( O(n!) &#92;) time complexity. However we can use Schreier-Sims algorithm, a staple of computational group theory, provides a scalable solution by constructing a &lt;a href=&quot;https://en.wikipedia.org/wiki/Strong_generating_set&quot;&gt;&lt;strong&gt;base and strong generating set&lt;/strong&gt;&lt;/a&gt; (or BSGS for short) for the permutation group generated by the tensor’s symmetries. This allows the algorithm to enumerate group elements without redundancy and with minimal overhead. Alternatively the library also supports projection onto irreducible representations specified by &lt;a href=&quot;https://en.wikipedia.org/wiki/Young_tableau&quot;&gt;Young tableaux&lt;/a&gt;, enabling the decomposition of tensors into symmetry types corresponding to the representations of the symmetric group. I won&#39;t explain this here, but it&#39;s well-documented elsewhere and I put the lecture note links in the repository&lt;/p&gt;
&lt;p&gt;My implementation draws inspiration from the &lt;a href=&quot;https://xact.es/&quot;&gt;Xperm.c library&lt;/a&gt;, which is widely regarded as the gold standard for tensor canonicalization in symbolic computation. However, porting the algorithm to Rust was quite a challenge as the C and Mathematica and code is quite dated and not particularly well-documented. So hopefully if you&#39;re a practitioner in the field, you can look at my implementation, compare it with the reference and hopefully this makes it easier for some poor future physics grad student who doesn&#39;t need to build the same thing again to do their research.&lt;/p&gt;
&lt;p&gt;So that&#39;s &lt;code&gt;butler-portugal&lt;/code&gt;, which hopefully brings high-performance tensor canonicalization to the Rust ecosystem. Can install it with:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;cargo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; butler-portugal
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The library is quite efficient, MIT-licensed and hopefully useful in any domain where the manipulation of tensors with complex symmetries is required, and hopefuly useful for at least one other person besides myself!&lt;/p&gt;
</description>
      <pubDate>Sun, 29 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/tensor_canonicalization_rust/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/tensor_canonicalization_rust/</guid>
    </item>
    
    <item>
      <title>Fine-tuning With Tool Calling</title>
      <description>&lt;h1 id=&quot;fine-tuning-with-tool-calling&quot; tabindex=&quot;-1&quot;&gt;Fine-tuning With Tool Calling&lt;/h1&gt;
&lt;p&gt;With the release of &lt;code&gt;trl&lt;/code&gt; 0.19.0, there is now &lt;a href=&quot;https://huggingface.co/docs/trl/en/sft_trainer#tool-calling-with-sft&quot;&gt;native support&lt;/a&gt; for fine-tuning models with custom tools, a capability that has been seamlessly integrated into the &lt;code&gt;Dataset&lt;/code&gt; and &lt;code&gt;SFTTrainer&lt;/code&gt; classes. This streamlines the training process of teaching a model how and when to call external functions. We can now structure our training data to include the full conversational flow of tool usage. From the initial user prompt, to the model&#39;s decision to call a tool, the resulting output from that tool, and the model&#39;s final, synthesized response.&lt;/p&gt;
&lt;p&gt;An existing part of this workflow is the &lt;code&gt;get_json_schema&lt;/code&gt; utility function, which elegantly bridges the gap between your Python code and the structured format that the model requires. This function automatically generates a compliant JSON schema directly from a Python function, but it relies on two key coding practices. First, the function must include modern Python type annotations for all its arguments and its return value. Second, the function&#39;s docstring must be written in the &lt;a href=&quot;https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings&quot;&gt;Google docstring style&lt;/a&gt;. With &lt;code&gt;get_json_schema&lt;/code&gt; we can parse the function to extract not only the names and data types of the parameters but also the descriptive text that explains what the function and each of its arguments do. So let&#39;s look at an example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;utils &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; get_json_schema

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    A function that multiplies two numbers
    
    Args:
        a: The first number to multiply
        b: The second number to multiply
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b

schema &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_json_schema&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;multiply&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;schema&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives the dataset &lt;a href=&quot;https://huggingface.co/docs/trl/en/dataset_formats#tool-calling&quot;&gt;JSON schema&lt;/a&gt; for the function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;type&amp;quot;: &amp;quot;function&amp;quot;, 
  &amp;quot;function&amp;quot;: {
    &amp;quot;name&amp;quot;: &amp;quot;multiply&amp;quot;, 
    &amp;quot;description&amp;quot;: &amp;quot;A function that multiplies two numbers&amp;quot;, 
    &amp;quot;parameters&amp;quot;: {
      &amp;quot;type&amp;quot;: &amp;quot;object&amp;quot;, 
      &amp;quot;properties&amp;quot;: {
        &amp;quot;a&amp;quot;: {
          &amp;quot;type&amp;quot;: &amp;quot;number&amp;quot;, 
          &amp;quot;description&amp;quot;: &amp;quot;The first number to multiply&amp;quot;
        }, 
        &amp;quot;b&amp;quot;: {
          &amp;quot;type&amp;quot;: &amp;quot;number&amp;quot;,
          &amp;quot;description&amp;quot;: &amp;quot;The second number to multiply&amp;quot;
        }
      }, 
      &amp;quot;required&amp;quot;: [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use this schema to create a dataset with tool calling. For instance we can wrap the &lt;code&gt;mpmath&lt;/code&gt; library to create a dataset with tool calling related to polynomial roots and Bessel functions. We just need to add a &lt;code&gt;tools&lt;/code&gt; column in an dataset with the tool schemas.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; mpmath
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;utils &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; get_json_schema
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SFTTrainer


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_polynomial_roots&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;coefficients&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Finds the roots of a polynomial given its coefficients.

    Args:
        coefficients: A list of coefficients [c_n, ..., c_1, c_0] for c_n*x^n + ... + c_0.

    Returns:
        A string representation of the list of roots.
    &quot;&quot;&quot;&lt;/span&gt;
    roots &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mpmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;polyroots&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;coefficients&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;roots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculate_bessel_j&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;order&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Calculates the Bessel function of the first kind, J_v(z).

    Args:
        order: The order &#39;v&#39; of the Bessel function.
        value: The value &#39;z&#39; at which to evaluate the function.

    Returns:
        The result of the Bessel function J_v(z).
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; mpmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;besselj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;order&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


polyroots &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_json_schema&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;find_polynomial_roots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bessel &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_json_schema&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;calculate_bessel_j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_dict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;messages&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Find the roots of the polynomial x^2 - 4.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;tool_calls&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;token string&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;function&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;token string&quot;&gt;&quot;function&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                                &lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;find_polynomial_roots&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;token string&quot;&gt;&quot;arguments&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;coefficients&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;tool&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;find_polynomial_roots&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;[-2.0, 2.0]&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The roots of x^2 - 4 are -2.0 and 2.0.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the value of the Bessel function J0(2.5)?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;tool_calls&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                            &lt;span class=&quot;token string&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;function&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;token string&quot;&gt;&quot;function&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                                &lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;calculate_bessel_j&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;token string&quot;&gt;&quot;arguments&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;order&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;tool&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;calculate_bessel_j&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0.0483837764022209&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The value of J0(2.5) is approximately 0.04838.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;tools&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; 
            &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;polyroots&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bessel&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;polyroots&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bessel&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Qwen3-0.6B&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In my limited testing the &lt;code&gt;Qwen3-0.6B&lt;/code&gt; and &lt;code&gt;Llama-3.1-70B-Instruct&lt;/code&gt; are currently the best open weight models for fine-tuning for custom tool use. Your mileage may vary.&lt;/p&gt;
</description>
      <pubDate>Mon, 23 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/fine_tuning_tools/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/fine_tuning_tools/</guid>
    </item>
    
    <item>
      <title>Program Synthesis: The λ in the Machine</title>
      <description>&lt;h1 id=&quot;program-synthesis%3A-the-%CE%BB-in-the-machine&quot; tabindex=&quot;-1&quot;&gt;Program Synthesis: The λ in the Machine&lt;/h1&gt;
&lt;p&gt;Much of the &amp;quot;mainstream&amp;quot; discussion of artificial intelligence applied to code generation is overwhelmingly dominated by the idea of scaling larger langauge models and using test-time compute to improve model performance on coding (&lt;a href=&quot;https://www.swebench.com/&quot;&gt;SWE-bench&lt;/a&gt;, LiveCodeBench, etc) benchmarks. And that has legitimately produced some very interesting results. Although I&#39;m not sure how much more juice there is to squeeze out of that fruit.&lt;/p&gt;
&lt;p&gt;And indeed, the frontier models have demonstrated a remarkable capacity for probabilistic mimicry and the ability to stich together and synthesize templates of code from their training corpus in emergent and useful ways. But they still operate by predicting token sequences, generating syntactically plausible artifacts that often resemble human-written code. Whether they are doing anything resembling reasoning is a highly debatable and &lt;a href=&quot;https://ml-site.cdn-apple.com/papers/the-illusion-of-thinking.pdf&quot;&gt;open question&lt;/a&gt; which I don&#39;t have any interesting opinions on because I truly don&#39;t know. I do know that the current systems are limitedly useful in some cases, that much seems undeniably true.&lt;/p&gt;
&lt;p&gt;Yet, I would still argue that to mistake this sophisticated pattern matching for genuine program construction is a profound category error. Generating &lt;em&gt;code&lt;/em&gt; as a sequence of textual tokens using a statistical prior is a fundamentally different task than generating &lt;em&gt;programs&lt;/em&gt; by searching directly in the space of program dynamics. The former is an act of linguistic surface manipulation; the latter is an act of logical and semantic discovery. And it&#39;s not clear to me that current models are capable of doing the latter.&lt;/p&gt;
&lt;p&gt;But humour me for a moment and consider, that just perhaps the future of AI-assisted development lies not in refining the models of today, but in pursuing the more arduous, less-trodden path of &lt;a href=&quot;https://people.csail.mit.edu/asolar/SynthesisCourse/Lecture1.htm&quot;&gt;&lt;strong&gt;program synthesis&lt;/strong&gt;&lt;/a&gt;, a path whose modern incarnation finds its deepest roots in the evolution of functional programming and type theory. The ambition of program synthesis is not to produce code that &lt;em&gt;looks&lt;/em&gt; correct, but to construct a program that is, by its very construction, proven to be correct against a specification. This moves the locus of activity from the messy, ambiguous realm of natural language prompts to the precise, verifiable domain of logical propositions. This pursuit has historically been hampered by a computationally intractable search space, but the maturation of functional programming, specifically with the advent of dependent types, has provided much of the necessary theoretical scaffolding.&lt;/p&gt;
&lt;p&gt;A dependently typed language allows specifications to be encoded directly within the type system, transforming type-checking into a form of partial program verification. This is where the concept of &lt;strong&gt;hole-style development&lt;/strong&gt; becomes the crucial user interface for this new paradigm. A programmer architects the high-level structure, articulating properties as types, but leaves specific implementation details as explicit &amp;quot;holes&amp;quot; in the code. These are first-class, typed apertures in a well-formed program, and the synthesizer&#39;s role is to find a term satisfying the rigid constraints of its context.&lt;/p&gt;
&lt;p&gt;Consider a dependently typed pseudocode for appending two vectors, where the type system guarantees the length of the resulting vector. The programmer might write the initial sketch, leaving the implementation logic as a hole, denoted by &lt;code&gt;?&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Nat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;token constant&quot;&gt;Nil&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
  &lt;span class=&quot;token constant&quot;&gt;Cons&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;append&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Vect&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;append&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nil&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ys&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;append&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Cons&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ys&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cons&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type-checker deduces that this hole must be filled by a term of type &lt;code&gt;Vect (n + m) a&lt;/code&gt;. The synthesizer&#39;s task is now tremendously simplified. It does not need to understand the &amp;quot;concept&amp;quot; of appending. It only needs to search for a combination of operations that can transform the available terms (&lt;code&gt;xs&lt;/code&gt; of type &lt;code&gt;Vect n a&lt;/code&gt; and &lt;code&gt;ys&lt;/code&gt; of type &lt;code&gt;Vect m a&lt;/code&gt;) into the required output type. Through a guided search, it would discover that the recursive call &lt;code&gt;append n m xs ys&lt;/code&gt; satisfies the type, completing the program in a manner guaranteed to be correct with respect to its type-level specification. If you&#39;ve ever used systems like Agda, Coq or Lean you&#39;ve probably seen the power of this approach.&lt;/p&gt;
&lt;p&gt;This vision of collaborative, (albeit dynamically typed) synthesis is at least partially realized in systems like &lt;a href=&quot;https://arxiv.org/abs/2006.08381&quot;&gt;DreamCoder&lt;/a&gt; represent a significant step towards bridging symbolic reasoning with neural learning. DreamCoder operates via a &lt;a href=&quot;https://people.csail.mit.edu/asolar/papers/EllisWNSMHCST21.pdf&quot;&gt;wake-sleep Bayesian learning cycle&lt;/a&gt;. In its &amp;quot;wake&amp;quot; phase, it actively attempts to solve a corpus of &lt;a href=&quot;https://dspace.mit.edu/bitstream/handle/1721.1/145949/3453483.3454080.pdf?sequence=1&quot;&gt;inductive program synthesis&lt;/a&gt; problems, searching for programs that satisfy given input-output examples. In its &amp;quot;sleep&amp;quot; phase, it performs a kind of computational introspection: it analyzes the solutions it found, refactoring and compressing common patterns into a learned library of reusable, higher-level abstractions. Concurrently, it trains a &lt;a href=&quot;https://arxiv.org/abs/2306.07856&quot;&gt;neural network to guide its future search&lt;/a&gt;, making the synthesis process vastly more efficient. This iterative process of alternately solving problems and then learning from those solutions allows the system to bootstrap its own expertise, building an increasingly potent, domain-specific language and a search policy tuned to its structure.&lt;/p&gt;
&lt;p&gt;The neural-guided approach pioneered by DreamCoder opens a still more compelling research frontier: framing type-driven synthesis as a reinforcement learning problem. One can imagine an agent, powered by an algorithm like &lt;a href=&quot;https://huggingface.co/docs/trl/main/en/ppo_trainer&quot;&gt;PPO&lt;/a&gt;, tasked with inductively constructing partial programs and filling holes in a program. The environment is the incomplete program; the state is the typed hole and its local context; the available actions are the primitives in the current library, which itself may have been learned. A reward is given for any action that produces a type-correct term, with a significant final reward for completing the program. The agent&#39;s policy network would learn a semantic prior, predicting not which &lt;em&gt;token&lt;/em&gt; is likely to come next, but which &lt;em&gt;branch&lt;/em&gt; or &lt;em&gt;insert&lt;/em&gt; is most likely to satisfy a given local type signature and the global goal of optimizing towards a reward function that maximizes coherence to the specification. This moves beyond pattern matching on surface syntax and toward learning the logical structure of a problem domain, a direction explored by research into systems like &lt;a href=&quot;https://github.com/nathanael-fijalkow/DeepSynth&quot;&gt;DeepSynth&lt;/a&gt;, &lt;a href=&quot;https://arxiv.org/abs/2306.02049&quot;&gt;LambdaBeam&lt;/a&gt; and other &lt;a href=&quot;https://dl.acm.org/doi/10.1145/3622830&quot;&gt;neural-guided deductive search methods&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This kind of fun speculation is fun, and it&#39;s an very interesting and open research problem. But a sober dose of realism is required. Despite the intellectual elegance of the idea of program synthesis, its future is far from assured. The development of these synthesizers would be a monumental engineering task with very little short-term economic upside realized by its developers, probably making it unfundable in any traditional model. Each domain, each library, each API must be furnished with formal specifications or a well-curated set of problems for the synthesizer to be effective. This is a stark contrast to statistical models, which can ingest the chaotic, undocumented, and inconsistent sprawl of existing ecosystems like NPM or PyPI and still extract useful, albeit brittle, patterns of usage.&lt;/p&gt;
&lt;p&gt;The network effects of existing programming ecosystems are a force of nature that technical purity has consistently failed to overcome. This was the bitter lesson learned by the Lisp and functional programming communities over the last forty years. While they built pristine, powerful, and self-consistent bubble worlds, they were ultimately outcompeted by the pragmatic secretion and accretion of C, Java, and Python, whose value derived not from internal coherence but from the sheer scale of their surrounding libraries and economic incentives. Thus I would probalby condede that for the foreseeable future, the industry will be dominated by the superficially effective approach of statistical token prediction. The dream of provably correct programs, &lt;a href=&quot;https://dl.acm.org/doi/10.1145/3622874&quot;&gt;synthesized through a formal dialogue&lt;/a&gt; between human and machine, will continue to be a inspiring vision, but it may be dwarfed by the immense gravitational pull of code reuse and the path of least resistance offered by simply mimicking what has been written before. But we can still dream of DreamCoder future.&lt;/p&gt;
</description>
      <pubDate>Sun, 22 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/program_synthesis/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/program_synthesis/</guid>
    </item>
    
    <item>
      <title>The Asbestos Doctrine: Why Crypto Must Be Contained, Not Condoned</title>
      <description>&lt;h1 id=&quot;the-asbestos-doctrine%3A-why-crypto-must-be-contained%2C-not-condoned&quot; tabindex=&quot;-1&quot;&gt;The Asbestos Doctrine: Why Crypto Must Be Contained, Not Condoned&lt;/h1&gt;
&lt;p&gt;For more than a decade, the public discourse surrounding cryptocurrency has been trapped in a fog of misleading analogies and deterministic prophecies. Its defenders have presented it as a misunderstood marvel, variously portrayed as the next iteration of the internet, a lifeboat for the oppressed, or a necessary evolution in finance. This techno-optimistic narrative, which insists the technology is a neutral tool whose potential is just beginning to unfold, has proven to be a dangerously seductive fiction. After sixteen years of a global, multi-trillion dollar experiment run on the public without its consent, the results are in. The appeal to future potential is no longer a credible defense, but an admission of present failure. The fog has cleared, revealing not a nascent utility but a fundamental, structural harm. Cryptocurrency is not the new internet; it is financial asbestos, a socially corrosive material woven into the fabric of our digital lives, and the only responsible public policy is one of aggressive, principled containment.&lt;/p&gt;
&lt;p&gt;The initial counter-arguments to this stark conclusion often depend on a flawed characterization of the technology as a politically agnostic tool, akin to the printing press. This comparison fails on its face. While the printing press was a vector for all ideas, its core function was the disintermediation of information. The core function of cryptocurrency, as revealed by its actual use, is the disintermediation of accountability. Its foundational feature (so-called &amp;quot;trustlessness&amp;quot;) is merely a euphemism for a system that cannot be governed, audited, or controlled. Its ideology, steeped in a deep distrust of democratic institutions and a nihilistic contempt for regulated systems, is not an incidental feature but the consistent expression of its purpose. The system was not co-opted by bad actors; it was purpose-built to serve them. To dismiss this as a mere genetic fallacy is to ignore the overwhelming evidence that the system’s outcomes have perfectly matched the destructive intent of its founding philosophy.&lt;/p&gt;
&lt;p&gt;Nowhere is the harm more apparent or the stakes higher than in the debate over stablecoins. Proponents have framed these dollar-pegged instruments as the industry’s killer application, a benevolent tool for global citizens. This narrative is a Trojan horse. In reality, stablecoins are unregulated shadow banks built on a fatally flawed structure. They are simply digital Money Market Funds, instruments known to be so inherently fragile that their heavily regulated counterparts required massive Federal Reserve bailouts in both 2008 and 2020 to prevent systemic collapse. To champion the proliferation of a less transparent, globally distributed version of this demonstrably unstable structure is an act of profound recklessness. Each transaction privatizes profit for an offshore issuer while socializing risk directly onto the American taxpayer, who serves as the unwilling and uncompensated insurer of last resort. When—not if—a major stablecoin suffers a crisis of confidence, the political pressure on the United States to prevent a global financial meltdown it enabled would be immense, creating a policy trap of its own making.&lt;/p&gt;
&lt;p&gt;The attempt to justify this system by pointing to its use in nations like Argentina is a cynical defense that immediately recasts responsible risk analysis as &amp;quot;paternalism.&amp;quot; But let us be clear: it is not paternalism to diagnose a systemic poison, and our diagnosis must begin with empathy. It is an undeniably rational, even desperate, choice for an individual family in Buenos Aires to flee a hyper-inflating currency. The tragedy, however, lies in a textbook fallacy of composition: that very act of individual survival, when performed in aggregate, guarantees the collective catastrophe of digital dollarization. This process is not empowerment; it is the ceding of a nation’s monetary sovereignty to an unaccountable, unregulated, offshore corporate power. It forces a coerced trade: a flawed but public sovereign, answerable to its people, for an opaque private sovereign, answerable only to its shareholders. This is not a &amp;quot;lifeboat.&amp;quot; It is the exchange of a seat on a leaky public ferry for a place on a private pirate ship, with the American taxpayer implicitly forced to underwrite the insurance policy for the entire pirate fleet.&lt;/p&gt;
&lt;p&gt;The attempt to justify this digital expansionism by invoking a geopolitical struggle is equally bankrupt. The notion that the United States must promote private stablecoins to counter a hypothetical future dominance of China’s digital yuan is a deeply flawed premise. It conflates the technical structure of a state-backed CBDC with a privately issued, risk-laden stablecoin. Moreover, it rests on a xenophobic and simplistic &amp;quot;us versus them&amp;quot; worldview that paints American financial policy as a zero-sum game against a foreign &amp;quot;other.&amp;quot; China&#39;s capital account is not open, and its digital currency project remains a domestic pilot. To build a case for risking global financial stability on such a flimsy and fear-based projection is an abdication of sound policymaking. A world where Europe or another democratic bloc takes the lead in setting digital currency standards is not a threat to be countered but a development to be welcomed within a rules-based international order.&lt;/p&gt;
&lt;p&gt;The persistent defense of cryptocurrency’s potential relies on a constant appeal to false historical parallels, chief among them the comparison to the early internet. This analogy is fundamentally dishonest. The internet began as ARPANET, a government-funded project with a clear, utilitarian purpose. Cryptocurrency, by contrast, was born of an ideological whitepaper and built from the start as a speculative, anti-state project. The argument that it is still &amp;quot;early days&amp;quot; has long expired. After sixteen years, far longer than it took the early internet to demonstrate its world-changing utility, cryptocurrency’s primary use cases remain crime, gambling, and recursive financial speculation. This is not a victimless abstraction. It is the ransomware that shutters a hospital&#39;s emergency room; it is the life savings of a retiree vanishing overnight in a &#39;rug pull&#39;; it is the global marketplace for the most odious materials imaginable, all facilitated by a system that launders accountability by design. Its failure to produce a single, large-scale, non-speculative application is not a sign of immaturity but a definitive market verdict on its nature. Legitimate enterprises have rejected it not because it is new, but because it is inefficient-by-design, making it fundamentally unsuited for any serious purpose. It is an empty shell.&lt;/p&gt;
&lt;p&gt;This leads to the final, desperate defense: that cryptocurrency is a form of pure, permissionless innovation whose value cannot yet be judged. The comparison often made to that of early experiments on electricity which took decades before practical applications and a commerical electrical grid were realized. This argument presents a false choice between progress and stagnation, but a mature society does not operate on such naive binaries. It rightly reserves the power of an &lt;strong&gt;Innovation Veto&lt;/strong&gt;—the ability to reject a novel creation not out of fear, but out of a rational calculation that its socialized harms vastly outweigh its privatized and hypothetical benefits. To justify this by claiming electricity also took decades to mature is a profoundly dishonest analogy; its foundational experiments were contained, harmless, and carried no public cost, whereas this is a live, multi-trillion dollar trial whose catastrophic externalities are already present and undeniable.&lt;/p&gt;
&lt;p&gt;This veto is not a hypothetical power; it is a cornerstone of modern civilization. We exercised it against human cloning on ethical grounds, against leaded gasoline for public health, and against chlorofluorocarbons to protect our planetary environment. The demand for &#39;permissionless innovation&#39; is therefore a demand to suspend this critical societal function. It is an argument for a sociopathic model of progress, one that is unaccountable to the public it purports to serve. We have been asked to tolerate immense and ongoing social harm in exchange for a perpetually deferred, purely hypothetical future benefit. But society is not a laboratory, and its citizens are not test subjects. The line for such tolerance has long since been crossed. The experiment has failed.&lt;/p&gt;
&lt;p&gt;Let this not be mistaken for a defense of the status quo. The existing financial system is deeply flawed: it is often slow, exclusionary, and unjust. But it is built upon a framework of law and accountability that can, with effort, be reformed. It is, to borrow from Churchill, the worst possible system, except for all the others that have been tried. The promise of cryptocurrency is not a better system; it is the alluring but false promise of a perfect system, a revolutionary shortcut that avoids the hard work of democratic reform. In reality, it offers only a descent into a state of nature, where the only rule is code and the only protection is your own vigilance.&lt;/p&gt;
&lt;p&gt;The correct path forward, therefore, is not to legitimize this harmful system with bespoke regulation. This is a categorical error. We regulate dangerous but necessary industries like aviation or nuclear power. There is no Federal Commission for Safer Pyramid Schemes. There is no Bureau of Responsible Bank Robbery. We prohibit these activities because the act itself is the harm. To regulate cryptocurrency is to formally absorb financial asbestos into the architecture of our society, signaling that this non-productive casino is a legitimate part of our economic life. This would create the very moral hazard that guarantees future bailouts. The fundamental paradox at the heart of this policy debate boils down to the &lt;strong&gt;&amp;quot;Regulatory Trilemma&amp;quot;&lt;/strong&gt;. A policymaker can pursue any two of the following three goals, but is barred from achieving all three.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Preserve Crypto&#39;s Core Nature&lt;/strong&gt; - Permissionless, decentralized, censorship-resistant.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ensure Full Regulatory Compliance&lt;/strong&gt; - Effective KYC/AML, sanctions enforcement, investor protection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prevent Moral Hazard&lt;/strong&gt; - Ensure the state and taxpayers are not on the hook for bailouts.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The attempt to fuse &lt;strong&gt;(1)&lt;/strong&gt; and &lt;strong&gt;(2)&lt;/strong&gt; is a technical impossibility; a truly decentralized protocol cannot be forced to comply with state mandates. The choice to prioritize &lt;strong&gt;(2)&lt;/strong&gt; and &lt;strong&gt;(3)&lt;/strong&gt; is an act of transformation, not regulation; a fully compliant and regulated &amp;quot;stablecoin&amp;quot; is no longer a permissionless crypto-asset, but merely a fintech product like a PayPal account, built on a ridiculously inefficient database. The most perilous path, and the one we are on, is to preserve &lt;strong&gt;(1)&lt;/strong&gt; while feigning &lt;strong&gt;(2)&lt;/strong&gt;. This creates massive &lt;strong&gt;(3)&lt;/strong&gt; moral hazard, as the government puts a stamp of legitimacy on a system it cannot actually control, guaranteeing a future crisis. This dismantles the central premise of &#39;responsible innovation.&#39; Any attempt to regulate cryptocurrency either fails to control it or succeeds only by destroying it. Therefore, the only logical policy is not to legitimize this contradiction, but to contain its harm by keeping it separate from the real economy.&lt;/p&gt;
&lt;p&gt;Thus the only sound policy is one of principled, relentless containment. We must accept that cryptocurrency, as a bad idea, may never be fully eradicated. The same is true of other socially destructive ideologies like fascism or racism. A liberal society does not defeat such ideas by trying to erase them from existence, an impossible and authoritarian task. Instead, it fights them by denying them institutional oxygen. It makes them socially unacceptable, legally actionable, and politically powerless. It builds a firewall between them and the levers of public life.&lt;/p&gt;
&lt;p&gt;Let us be clear about the fundamental chasm that separates these two worldviews. This is not merely a technical disagreement but a profound philosophical schism over the very purpose of a civilized society. A policy of containment is grounded in the belief that the state is a collective project, a social contract whose highest duty is to protect the whole from systemic risk and corrosive externalities. The opposing view, often disguised as technological inevitability, champions a radical vision where individual action, unconstrained by democratic oversight, is the ultimate source of order. It willfully ignores the fallacy of composition—the demonstrable truth that what benefits an individual in isolation can be catastrophic for the collective—and recasts this societal destruction as a virtue called &amp;quot;disruption.&amp;quot; This is not a debate between two valid paths to progress; it is a conflict between the architects of a durable society and the apostles of its dissolution. It is a debate about whether the immense, certain harm of the present is a fair price to pay for the uncertain, hypothetical benefit of the future. And who should pay that price.&lt;/p&gt;
&lt;p&gt;Asbestos was not embraced as a known poison; it was thought to be a durable cheap insulator. It was precisely this surface-level utility that made it so dangerous, leading society to weave it into the very structure of our schools and homes while its catastrophic harm remained latent, a microscopic and systemic threat that festered for decades. This is the structural parallel to cryptocurrency: a technology whose proponents market its novel features while ignoring the slow-acting poison of financial instability, ensuring that by the time the harm becomes undeniable, it will already be deeply embedded within our economic architecture, forcing a painful and costly remediation on a society that never consented to the risk. The eventual societal verdict was not to seek &#39;safer&#39; applications, but to ban its future use entirely—to rip it from our walls and bury it in the ground.&lt;/p&gt;
&lt;p&gt;This is the &lt;strong&gt;Asbestos Doctrine&lt;/strong&gt;. The goal is not a futile &amp;quot;War on Crypto&amp;quot; that seeks to scrub its code from every computer. The goal is to use the full force of existing law and democratic institutions to ensure it remains a marginal, isolated phenomenon. This means aggressive enforcement of securities laws, anti-money laundering statutes, and sanctions. It means severing its lifeline to the regulated banking system and its ability to use US Treasuries as reserves. The objective is to firewall the offshore casino so completely that its collapse, when it comes, is an isolated event, not a systemic contagion. It is to let it wither in the dark, cut off from the light of legitimacy. Some will always seek it out, just as some will always seek out any number of harmful pursuits. But public policy must ensure that it cannot grow, that it cannot achieve systemic importance, and that it cannot poison the broader social and economic well. The faith we must place is not in the phantom promise of abstract future technologies or a decentralized utopia, but in the long, proven arc of our own democratic institutions to contain and marginalize the destructive ideas that threaten them.&lt;/p&gt;
</description>
      <pubDate>Sat, 21 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/absethos_doctrine/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/absethos_doctrine/</guid>
    </item>
    
    <item>
      <title>What is AI market actually worth?</title>
      <description>&lt;h1 id=&quot;what-is-ai-actually-worth%3F&quot; tabindex=&quot;-1&quot;&gt;What is AI actually worth?&lt;/h1&gt;
&lt;p&gt;The artificial intelligence boom is obviously the latest historical example of a market bubble, but it is a particularly strange one. It is a bubble inflated by a potent mixture of managerial fantasy, quasi-religious fervor, and a torrent of capital with nowhere else to go. While proponents and industry thought leaders herald a new economic dawn that rivals the industrial revolution, a closer inspection reveals an industry built on shaky technological foundations, propped up by unsustainable spending and fueled by a narrative that borders on the absurd. The current trillion dollar valuations are not a reflection of imminent revolution but a testament to the financial world&#39;s infinite capacity for self-deception, promising an inevitable and brutal correction.&lt;/p&gt;
&lt;p&gt;At the heart of the hype lies a technical reality that is far less impressive than the marketing material suggests. The scaling of large language models, the very engine of this new wave, appears to be hitting a wall of diminishing returns. Barring a fundamental architectural breakthrough, we are simply throwing more data and more computation at models for increasingly marginal gains. The entire process of benchmarking has devolved into a subjective beauty contest, where defining &amp;quot;progress&amp;quot; is a challenge in itself. Outside of tasks with binary answers, determining which model is superior is a matter of vibes, opinion, a game of whack-a-mole where improvements in one domain create deficiencies in another. The metrics are less science and more sophisticated hand-waving, designed to produce impressive charts for product launches and investor decks rather than a true measure of capability.&lt;/p&gt;
&lt;p&gt;This technical ambiguity provides fertile ground for a seductive fantasy that has completely captured the American professional managerial class. Sold a bill of goods by an army of management consultants, these non-technical executives envision a near future where the messy, unpredictable, and costly business of human labor is automated away. They dream of a corporate utopia run by a small cadre of elite directors, where profits soar unburdened by salaries, healthcare, or labor disputes. This is, of course, a fiction, but it is a powerful one for managers who view their employees as expendable cogs in a machine. This delusion is the primary driver of corporate AI spending, a massive R&amp;amp;D slush fund dedicated to the proposition that workers can and should be replaced.&lt;/p&gt;
&lt;p&gt;The result of this managerial pipe dream is a tidal wave of corporate &amp;quot;innovation&amp;quot; projects that are profoundly disconnected from the actual work of the company. The corporate mandate is to &amp;quot;have an AI strategy,&amp;quot; which in practice means building internal chatbots and RAG systems for data retrieval. These projects, often several degrees removed from any core business activity, deliver little tangible value but are plausible enough to be sold internally as progress. Some recent studies put the failure rate of these projects at somewhere 95% failure rate. It is a perfect execution of the Politician&#39;s Fallacy: something must be done, this is something, therefore we must do it. Companies are burning through capital not to solve real problems, but to be seen as participating in the trend, creating expensive Potemkin villages of artificial intelligence to appease their boards and shareholders.&lt;/p&gt;
&lt;p&gt;Then come the management consultants, the high priests of corporate anxiety, arriving with the solemnity of undertakers to monetize the C-suite&#39;s deepest insecurities. Armed with glossy slide decks and a proprietary vocabulary of synergistic nonsense; they sell a seductive fantasy whispered into the ears of executives who view their workforce as a costly liability. Their gospel preaches a boardroom utopia where the messy, expensive, and occasionally unionizing problem of human labor can be neatly excised from the balance sheet, replaced by a flawless, uncomplaining algorithm. They expertly exploit the profound technological illiteracy of their clients, presenting AI as a magical black box that will transmute operational costs into pure profit. The result is a flood of multi-million dollar &amp;quot;transformation&amp;quot; roadmaps whose primary strategic function is to guarantee a lucrative, multi-year follow-on engagement for the consultants themselves. They are not advisors but architects of plausible deniability, providing executives with a beautifully formatted, jargon-filled permission slip to light a bonfire of shareholder money in the name of innovation.&lt;/p&gt;
&lt;p&gt;This corporate delusion is amplified and sanctified by the bizarre cultural terrarium of the Bay Area, where the AI labs function as monasteries for a new rationalist cult. Its acolytes, a socially anxious cohort of the terminally online, believe they have replaced messy human intuition with pure logic, yet have merely swapped out old gods for a silicon one. They signal their allegiance through a coded lexicon of Bayesian priors and expected utility, a verbal handshake that confirms they are among the enlightened few, forever detached from the lived experience of anyone who has ever touched grass. This is not merely a subculture; it is a full-blown eschatological movement for intelligent people who do not realize their own subconscious religious impulse has led them to cosplay as prophets. Self-described &amp;quot;super-forecasters&amp;quot; issue prophecies from their blogs, breathlessly predicting the arrival of a Machine God by 2027, a digital rapture that will solve all problems for the worthy. If you wish to gaze into the abyss of their thought process, you need only read their scripture, a prose so dense with self-importance and abstract nonsense that it acts as a cognitive barrier to the uninitiated.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So a post-singularity world may be a world of fun and plenty for the people who are currently ignoring the problem, whilst being a living hell for a significant fraction of current existential risk reducers (say, the least generous half). You could take this possibility into account and give even more to x-risk in an effort to avoid being punished. But of course, if you&#39;re thinking like that, then the CEV-singleton is even more likely to want to punish you... nasty. Of course this would be unjust, but is the kind of unjust thing that is oh-so-very utilitarian. It is a concrete example of how falling for the just world fallacy might backfire on a person with respect to existential risk, especially against people who were implicitly or explicitly expecting some reward for their efforts in the future. And even if you only think that the probability of this happening is 1%, note that the probability of a CEV doing this to a random person who would casually brush off talk of existential risks as &amp;quot;nonsense&amp;quot; is essentially zero.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If that passage reads like the unholy union of a paranoid sci-fi novel and a first-year philosophy student&#39;s bong-rip, congratulations, you are sane. This is the intellectual backbone of the movement: absurd, labyrinthine thought experiments, ripped from the pages of pulp fiction, taken with the deadly seriousness of divine revelation. It is, in essence, nerd Scientology for the Bay Area, a perfect trifecta of one part tax-optimization, one part cynical in-group signaling for career advancement, and one part the wide-eyed fervor of true believers who genuinely think they are building God in a server farm.&lt;/p&gt;
&lt;p&gt;When one examines the actual applications emerging from this frenzy, the picture becomes even more bleak. Outside of text generation, the most prominent use cases are either unprofitable, a net negative for society, or both. AI image generation is a fascinating toy, but it is an expensive, easily commoditized novelty with no clear path to profitability. The viral video generators producing surreal clips of talking animals and historical figures are an amusing distraction, but they are an engine for creating digital slop, not economic value. The only entity making real money from this &amp;quot;slop economy&amp;quot; is Nvidia, which happily sells the digital shovels for this fool&#39;s gold rush. The most likely outcome is a future where social media is choked by an endless stream of algorithmically generated content, strangling the very platforms that host it. And maybe we should celebrate that!&lt;/p&gt;
&lt;p&gt;Even the most legitimate application, AI-powered coding assistants, is a financially dubious proposition. While these tools can genuinely augment programmer productivity, the companies providing them are deeply unprofitable. They are also highly dependent on a small number of model providers (namely Anthropic, whose models are genuinely much better than the others at coding tasks) and constrained by the same scaling limitations plaguing the entire field. The technology may well eliminate a swath of entry-level programming jobs, but the dream of a fully automated software engineer, capable of complex, creative, and system-level thinking, remains a distant pipe dream. Like so much in the AI space, a kernel of a good idea has been inflated into a revolutionary fantasy that the underlying technology simply cannot support.&lt;/p&gt;
&lt;p&gt;The entire edifice is held aloft by a geyser of capital so vast and indiscriminate it makes a firehose look like a precision instrument. The American venture economy is sitting on mountains of &amp;quot;dry powder,&amp;quot; billions in undeployed capital aching with the impatient, frantic energy of a cornered rat. With the smoking craters of crypto and the metaverse still fresh in memory, the investment class has stampeded toward the last plausible growth story, driven by the moronic TINA mantra. So-called strategic allocation is really more a desperate flight to the only mirage in the desert. But if you&#39;re running money professionally, sitting in cash is a non-option, so when the music is playing you have to dance.&lt;/p&gt;
&lt;p&gt;This initial flood of nervous money then attracts the final stage of the financial food chain: the gullible leviathans. Here come the SoftBanks and the gulf state sovereign wealth funds, the largest and dim-wittedest pools of money on Earth, ready to blast their petrodollars and printed money at anything that glows with the faint promise of &amp;quot;the future.&amp;quot; Their due diligence process has the intellectual rigor of a slot machine, a mix of geopolitical posturing and pure, unadulterated FOMO. This creates a magnificent, self-licking ice cream cone of a bubble. The capital class funds the unsustainable cash burn of AI labs, which generates hype, which attracts the sovereign wealth funds for a later, larger round at an insane valuation, which validates the VCs&#39; initial bet. The goal is not to build a profitable business but to keep the music playing long enough to pass the flaming bag of promissory notes to the next, even wealthier, idiot. The supposedly decentralized investor class is more like a herd of lemmings, engaging in a spectacular display of malinvestment that would make a Soviet central planner blush.&lt;/p&gt;
&lt;p&gt;None of this is to say that all of AI is malinvestment or fraudulent. Legitimate and fascinating research is being done, and Nobel breakthroughs around protein folding advances to demonstrate the potential. Probabilistic text models have real, if niche, applications. But they are not reliable, consistent, or accurate enough to perform the vast majority of knowledge economy tasks. There is no breakthrough on the horizon that promises to give them these properties. The industry is filled with people role-playing that such a breakthrough is just around the corner, either because they are incentivized to believe it or because they are members of the religious movement and believing in AI singularity provides a psychological escape from the mundane precarity of modern existence. The reality is that artificial intelligence is a real, durable and lasting industry that will inevitably be part of he future, but it is one whose true market capitalization is measured in the tens of billions, not trillions. The vast sums of money currently being allocated are fuel for a bonfire, and when the flames die down, all that will be left is the sobering ash of a market that finally, and painfully, came to its senses. And hopefully it doesn&#39;t take the chunk of the S&amp;amp;P500 with it.&lt;/p&gt;
</description>
      <pubDate>Tue, 17 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_marketcap/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_marketcap/</guid>
    </item>
    
    <item>
      <title>Book Review: Tomorrow, and Tomorrow, and Tomorrow</title>
      <description>&lt;h1 id=&quot;book-review%3A-tomorrow%2C-and-tomorrow%2C-and-tomorrow&quot; tabindex=&quot;-1&quot;&gt;Book Review: Tomorrow, and Tomorrow, and Tomorrow&lt;/h1&gt;
&lt;p&gt;Gabrielle Zevin&#39;s &lt;em&gt;Tomorrow, and Tomorrow, and Tomorrow&lt;/em&gt; is a novel about two people who make video games together for thirty years, and if that sentence made you instinctively reach for the back button, I understand, but you would be making a mistake. The book is not about the games industry in the way that industry memoirs are about the games industry (self-congratulatory, jargon-heavy, convinced that shipping a product is inherently heroic). It uses game design the way the best science fiction uses spaceships: as a vehicle for getting somewhere more interesting. In this case, the destination is a sustained, unsentimental, and surprisingly devastating examination of what it means to build something with another person.&lt;/p&gt;
&lt;p&gt;Sam Masur and Sadie Green meet as children in a hospital. Sam is recovering from a car accident that killed his mother and crushed his foot. Sadie is visiting her sister, who is being treated for leukemia. They bond over a Nintendo. They lose touch. They reconnect years later (Sam at Harvard, Sadie at MIT, a few stops apart on the Red Line, a geography I know well, having lived between those stops for years) and begin making games together. Their first collaboration, &lt;em&gt;Ichigo&lt;/em&gt;, a side-scroller set in a world drawn from Hokusai&#39;s wave prints, becomes a massive hit. Their creative partnership spans decades. It survives success, failure, jealousy, grief, a shooting, and the persistent, never-quite-resolved question of whether they are in love with each other or merely in love with the thing they make together.&lt;/p&gt;
&lt;p&gt;Zevin is ruthlessly precise about the mechanics of creative collaboration. She understands that making something with another person is its own form of intimacy, distinct from friendship and from romance and reducible to neither. Sam and Sadie need each other. They resent each other for the needing. They compete, they withhold credit, they hurt each other in the specific ways that only people who share a creative nervous system can. Marx, Sam&#39;s college roommate who becomes their business partner and eventually Sadie&#39;s romantic partner, is the third point of the triangle: the person who makes the collaboration possible by handling everything the two principals are too consumed by their own vision to notice. He is the most generous character in the book, and Zevin does something to him that I will not spoil except to say that it earns the novel its Macbeth epigraph.&lt;/p&gt;
&lt;p&gt;The title, of course, comes from &lt;a href=&quot;https://www.poetryfoundation.org/poems/56964/speech-tomorrow-and-tomorrow-and-tomorrow&quot;&gt;Macbeth&#39;s famous soliloquy&lt;/a&gt;: &amp;quot;Tomorrow, and tomorrow, and tomorrow, / Creeps in this petty pace from day to day.&amp;quot; The speech is about meaninglessness: life as sound and fury signifying nothing. Zevin inverts it. In her telling, the repetition of &amp;quot;tomorrow&amp;quot; is the structure of creative work. You show up. You make the thing. You show up again. The games Sam and Sadie build are their answer to Macbeth&#39;s despair, worlds that signify something precisely because someone chose to build them, even knowing they are temporary, even knowing they are fictions. It is the most quietly optimistic book about mortality I have read in years, and it achieves this optimism honestly, through accumulated loss rather than despite it.&lt;/p&gt;
&lt;p&gt;What will stay with you, if you work in any field that involves building things with other people (and if you are reading this blog, you almost certainly do), is Zevin&#39;s treatment of the authorship question. Who made this? Whose vision is it? Sam&#39;s name goes on the company. Sadie writes the engine. The public credits Sam. The code is Sadie&#39;s. Zevin shows, with forensic patience, how structural inequalities (gender, disability, class, the specific charisma of who happens to be standing at the microphone when the press arrives) determine who gets remembered and who gets erased. If you have ever pair-programmed on something and watched the other person get the credit, or gotten the credit yourself and felt the quiet wrongness of it, this book will land like a hammer.&lt;/p&gt;
&lt;p&gt;Sam&#39;s disability (chronic pain from the childhood injury, a permanent limp) is handled with a specificity that literary fiction rarely manages. Zevin treats it as a physical fact that shapes every day of his life, not as a metaphor or a character trait that reveals something about his soul. He is in pain. The pain affects what he can do. Other people&#39;s perception of his disability affects how they treat him. That is all. The same restraint applies to Sadie&#39;s experience as a woman in the games industry: exploited by a professor, underestimated by colleagues, structurally invisible in ways that accumulate over decades. Zevin documents the mechanisms without editorializing, and the documentation is damning enough.&lt;/p&gt;
&lt;p&gt;The novel is 416 pages and reads faster than most 200-page thrillers. It spans from the early 1990s to the 2040s, from the era of the SNES to virtual reality, and it uses the evolution of games technology as a quiet clock ticking beneath the human story. Zevin clearly loves games, but more importantly, she understands why people love them: they are worlds someone chose to build, governed by rules someone chose to impose, offering a kind of agency that the real world, with its car accidents and its cancer wards and its unfair credit allocations, does not reliably provide.&lt;/p&gt;
&lt;p&gt;I recommend this book to anyone who has ever made something with someone else and found that the making changed both of you in ways you could not have predicted. It is the best novel about creative partnership I have read, and it is the rare book that treats video games as an art form with the same seriousness that literary fiction typically reserves for itself. If Macbeth is right and life signifies nothing, Zevin&#39;s answer is: then build something anyway. Show up tomorrow. And tomorrow. And tomorrow.&lt;/p&gt;
</description>
      <pubDate>Sat, 14 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/tomorrow_tomorrow_tomorrow/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/tomorrow_tomorrow_tomorrow/</guid>
    </item>
    
    <item>
      <title>The Future of Maths May Be Deeply Weird</title>
      <description>&lt;h1 id=&quot;the-future-of-maths-may-be-deeply-weird&quot; tabindex=&quot;-1&quot;&gt;The Future of Maths May Be Deeply Weird&lt;/h1&gt;
&lt;p&gt;In fiction there is a popular image of a mathematician as a lone solitary figure, a figure of lost in their own world in a world of intense concentration before a chalkboard dense with arcane symbols. That may have had some truth in the past but the reality is that mathematics these days is a far more social phenomenon and &lt;a href=&quot;https://www.youtube.com/watch?v=Ch_cNbk39xY&quot;&gt;future mathematicians&lt;/a&gt; may have a lot more in common with software engineers toiling away on pull requests on Github than they do with the archetype of a Andrew Wiles-like genius toiling away in isolation for a decade to prove Fermat&#39;s Last Theorem.&lt;/p&gt;
&lt;p&gt;The field of automated theorem proving is a field of computer science that develops software systems capable of automatically constructing mathematical proofs or verifying the correctness of human-written proofs. These systems use formal logic, type systems, and symbolic reasoning to check that each step in a mathematical argument follows logically from the previous steps and established axioms. By encoding mathematical knowledge in a precise, machine-readable format, automated theorem provers can eliminate human error and provide absolute certainty about the validity of mathematical results.&lt;/p&gt;
&lt;p&gt;For those that don&#39;t follow the theorem proving ecosystem (&lt;a href=&quot;https://github.com/leanprover/lean4&quot;&gt;Lean4&lt;/a&gt;, Coq and their predecessors Isabelle, Agda, HOL4, etc) are tools that were once confined to the esoteric frontiers of computer science research, are poised to reshape the very practice and philosophy of mathematics. The future of the discipline may be far stranger, and more collaborative, than we can currently imagine. And with the simultaneous advent of language models, may be much more exciting and rapid than we can currently imagine.&lt;/p&gt;
&lt;p&gt;Below is a &lt;a href=&quot;https://github.com/leanprover-community/mathlib4/blob/7deb334c5f5104f4edad1a6396dd02a8cddefb86/Mathlib/Data/Nat/Prime/Infinite.lean#L27-L38&quot;&gt;formalization of Euclid&#39;s theorem&lt;/a&gt; on the infinitude of primes in Lean. The proof follows Euclid&#39;s constructive approach: given any natural number &lt;code&gt;n&lt;/code&gt;, it constructs a prime &lt;code&gt;p ≥ n&lt;/code&gt; by considering the smallest prime factor of &lt;code&gt;n! + 1&lt;/code&gt;. The Lean proof uses several &lt;a href=&quot;https://github.com/haruhisa-enomoto/mathlib4-all-tactics/blob/main/all-tactics.md&quot;&gt;tactics&lt;/a&gt; and lemmas: &lt;code&gt;minFac&lt;/code&gt; finds the minimal prime factor, &lt;code&gt;factorial_pos&lt;/code&gt; establishes that factorials are positive, &lt;code&gt;minFac_prime&lt;/code&gt; proves the minimal factor is indeed prime, and &lt;code&gt;dvd_factorial&lt;/code&gt; handles divisibility properties. The crucial insight, formalized through &lt;code&gt;Nat.dvd_add_iff_right&lt;/code&gt;, shows that if a prime divides both &lt;code&gt;n!&lt;/code&gt; and &lt;code&gt;n! + 1&lt;/code&gt;, it must divide their difference (which is 1), leading to a contradiction since primes cannot divide 1. This is the proof that every undergraduate mathematics student learns in their first year.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;/-- Euclid&#39;s theorem on the **infinitude of primes**.
Here given in the form: for every `n`, there exists a prime number `p ≥ n`. -/
theorem exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ Prime p :=
  let p := minFac (n ! + 1)
  have f1 : n ! + 1 ≠ 1 := ne_of_gt &amp;lt;| succ_lt_succ &amp;lt;| factorial_pos _
  have pp : Prime p := minFac_prime f1
  have np : n ≤ p :=
    le_of_not_ge fun h =&amp;gt;
      have h₁ : p ∣ n ! := dvd_factorial (minFac_pos _) h
      have h₂ : p ∣ 1 := (Nat.dvd_add_iff_right h₁).2 (minFac_dvd _)
      pp.not_dvd_one h₂
  ⟨p, np, pp⟩
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The formalization of the standard undergraduate mathematics curriculum in a format like this is no longer a distant dream. It is an achievable, near-term goal. Need only have a look at the standard library &lt;a href=&quot;https://leanprover-community.github.io/mathlib4_docs/Mathlib/Init.html&quot;&gt;Mathlib&lt;/a&gt; and the amount of progress on the standard set of theorems is now incredibly vast and expressive.  Another good example of this progress is the formalization of Terence Tao&#39;s textbook, &lt;a href=&quot;https://github.com/teorth/analysis&quot;&gt;&lt;em&gt;Analysis I&lt;/em&gt;&lt;/a&gt; which has been meticulously translated into an explorable, crosslinked and machine-checkable set of theorems. A future in which is every textbook has a digitial equivelent and any theorem or lemma can be &lt;a href=&quot;https://teorth.github.io/analysis/sec21/&quot;&gt;&amp;quot;double clicked&amp;quot;&lt;/a&gt; to expand its context and definitions is hardly science fiction anymore. It&#39;s essentially here.&lt;/p&gt;
&lt;p&gt;If you want to experiment with Lean, you can get started very quickly with the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install Visual Studio Code.&lt;/li&gt;
&lt;li&gt;Install Lean + Elan:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; https://elan.lean-lang.org/elan-init.sh &lt;span class=&quot;token parameter variable&quot;&gt;-sSf&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sh&lt;/span&gt;
elan default leanprover/lean4:stable
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Install the Lean 4 VS Code Extension: Open VS Code → Extensions → search for Lean 4 (author: &lt;code&gt;leanprover.lean4&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Initialize a new Lean project:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-sh&quot;&gt;lake init my_project
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; my_project
lake build
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Lean ecosystem also has a burgeoning flora of open source tools. The &lt;a href=&quot;https://leanprover-community.github.io/mathlib4_docs/&quot;&gt;Mathlib&lt;/a&gt; library is a massive, collaborative repository of formalized mathematics, growing daily. To navigate this vast digital library, a suite of search engines has emerged. &lt;a href=&quot;https://moogle-morphlabs.vercel.app/&quot;&gt;Moogle&lt;/a&gt; provides semantic search, allowing one to find theorems based on conceptual meaning rather than exact phrasing. &lt;a href=&quot;https://loogle.lean-lang.org/&quot;&gt;Loogle&lt;/a&gt; specializes in finding expressions that match a specific logical structure, and &lt;a href=&quot;https://leansearch.net/&quot;&gt;LeanSearch&lt;/a&gt; offers natural language queries. For those wishing to experiment, the &lt;a href=&quot;https://live.lean-lang.org/&quot;&gt;Lean 4 Playground&lt;/a&gt; provides an interactive environment to write and check proofs directly in a web browser.&lt;/p&gt;
&lt;p&gt;Consider the educational landscape this might enable. Imagine a future where every first-year mathematics student downloads a single, 200-megabyte file containing the entire verified foundation of their subject. They would be guided by an LLM-assisted tutor, a system capable of planning lessons, verbalizing complex proofs, and providing instant, theorem prover checkable feedback on their own attempts at proofs. This is not science fiction; the technology for curriculum planning and natural language explanation is largely within our grasp today. Of course, this vision must be tempered with realism. Science is intrinsically a social phenomenon. A great deal of mathematical research is about developing taste, an aesthetic for what constitutes an elegant proof, and the intuition for which problems are worth pursuing. These are intangible qualities of the craft, born from culture and collaboration, which a machine may never fully capture.&lt;/p&gt;
&lt;p&gt;As we move from established knowledge to the open frontiers of research, these tools present new and exciting possibilities. While the body of formalized proofs is growing, there remains a relative scarcity of formalized open conjectures. A project like the Deepmind&#39;s &lt;a href=&quot;https://github.com/google-deepmind/formal-conjectures&quot;&gt;index of formal conjectures&lt;/a&gt; is valuable for several reasons. First, it provides a clear and challenging set of benchmarks for the development of AI-assisted automated theorem provers. Second, the very act of formalization forces mathematicians to clarify the precise, unambiguous meaning of a conjecture. It is almost certain that we will soon see a powerful, neurosymbolic system, a kind of next generation AlphaGeometry, unleashed upon this list in an attempt to systematically push the boundaries of machine-generated mathematics.&lt;/p&gt;
&lt;p&gt;The artifacts produced by such research will themselves be objects of profound study. Picture a future where one can download the twenty-gigabyte weights of a neural network that has been trained through reinforcement learning to achieve mastery over the entirety of undergraduate mathematics, using theorem prover feedback as its reward function. The existence of such a model would provoke fascinating theoretical and philosophical questions. Is there a smaller, more elegant model that can achieve the same result? By examining its failure modes, what can we learn about the complexity hurdles in mathematics? How would it handle alternative axiom sets, and could it ever develop an &amp;quot;understanding&amp;quot; of Gödel&#39;s incompleteness, intuiting when a problem is fundamentally independent of the given axioms? &lt;strong&gt;There are many deep meta-mathematical questions we could ask if we had an existence proof of a giant, inscrutable matrix that can &amp;quot;itself&amp;quot; do mathematics&lt;/strong&gt;. There is no guarantee such a thing can exist (and the usual Gödel, Turing, and Hilbert&#39;s Program analyses still apply), but if it can — even to a limited extent — that opens up a whole new world of questions.&lt;/p&gt;
&lt;p&gt;Stretching our imagination further, we can envision a far future where this technology reaches its zenith. A dedicated data center could be tasked with a grand challenge, like the Riemann hypothesis. After weeks of processing, it might return a positive result: a complete, machine-checkable proof. Yet, the proof&#39;s internal structure could be so byzantine, so alien in its logic, that no human could ever hope to grasp its intuitive thrust, much like the computer-assisted proof of the Four Color Theorem or the controversy surrounding the Mochizuki proof of the ABC conjecture. This would usher in a disquieting new reality, a world where machines, not unlike the &lt;a href=&quot;https://www.youtube.com/watch?v=g2T-TStvRNM&quot;&gt;hyper-intelligent Minds&lt;/a&gt; from Iain M. Banks&#39; Culture series, venture into intellectual territories forever beyond human comprehension. We would &amp;quot;know&amp;quot; (epistomigical concerns aside) and accept certain mathematical truths, but as humans we would never intuit or understand why they are true. And that is a very different world in which science happens.&lt;/p&gt;
&lt;p&gt;The prospect of inscrutable machine-generated proofs touches on fundamental questions about the nature of knowledge and understanding. Bill Thurston, in his essay &lt;a href=&quot;https://www.math.toronto.edu/mccann/199/thurston.pdf&quot;&gt;&amp;quot;On Proof and Progress in Mathematics,&amp;quot;&lt;/a&gt; captured this tension:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The rapid advance of computers has helped dramatize this point, because computers and people are very different. For instance, when Appel and Haken completed a proof of the 4-color map theorem using a massive automatic computation, it evoked much controversy. I interpret the controversy as having little to do with doubt people had as to the veracity of the theorem or the correctness of the proof. Rather, it reflected a continuing desire for human understanding of a proof, in addition to knowledge that the theorem is true.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;New developments also raise teleological questions about the very purpose of of the maths discipline. Mathematics has never been merely about accumulating true statements—it&#39;s about developing insight, building intuition, and creating frameworks that illuminate the underlying structure of reality. The process of discovery, the &amp;quot;aha!&amp;quot; moments of understanding, the elegant connections between seemingly disparate areas: these constitute much of what makes mathematics meaningful to its practitioners. As Feynman once said: &amp;quot;Physics is like sex. Sure, it may give some practical results, but that&#39;s not why we do it.&amp;quot;&lt;/p&gt;
&lt;p&gt;To be honest, it&#39;s hard not to get emotional about this prospect. In that future, many of us would still care deeply about learning &lt;em&gt;why&lt;/em&gt; theorems are true. The process of answering those questions represents one of the most beautiful and fulfilling aspects of intellectual life. Maths as a practice is more than a matter of checking theorems off a cosmic to-do list. But if you buy into this premise, and if you&#39;re a burgeoning model designer, &lt;a href=&quot;https://github.com/google-deepmind/formal-conjectures/blob/9d70f49b89d6b2697af4234d377012ea26cfbb64/FormalConjectures/Wikipedia/RiemannZetaValues.lean#L76&quot;&gt;all you have&lt;/a&gt; to do win a Nobel (and several other prizes) is just train your stochastic parrot to autocomplete the next &#92;(n&#92;) tokens such that the Lean theorem validates your answer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem infinite_irrational_at_odd :
    { n : ℕ | ∃ x, Irrational x ∧ riemannZeta (2 * n + 1) = x }.Infinite := by
    ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&#39;ll also land yourself a cool million dollars from the Millennium Foundation in the process, which might pay for 1/100 of your GPU bill. Or maybe just make the &lt;a href=&quot;https://www.stephendiehl.com/posts/ai_slop_2027/&quot;&gt;AI-slop Infinite Jest&lt;/a&gt; version of TikTok, because alas, that&#39;s probably a much better short-term return on investment than solving Riemann and pushing the frontiers of human knowledge.&lt;/p&gt;
&lt;p&gt;But joking aside, we&#39;re not there yet. Not even close. Need only watch Terence Tao&#39;s &lt;a href=&quot;https://www.youtube.com/watch?v=zZr54G7ec7A&quot;&gt;recent YouTube streams&lt;/a&gt; with Claude and o4 on Lean theorem proving reveal the stark limitations of our current systems when faced with even moderately challenging mathematical problems. The models struggle with basic proof strategies, make elementary logical errors, and often produce syntactically correct but mathematically meaningless Lean code. Yet watching these experiments unfold, it doesn&#39;t require a tremendous leap of imagination to envision how rapidly this landscape might change. The combination of increasingly sophisticated language models, improved integration with theorem provers, and better &lt;a href=&quot;https://github.com/deepseek-ai/DeepSeek-Prover-V2&quot;&gt;training methodologies&lt;/a&gt; specifically designed for formal mathematical reasoning suggests that today&#39;s clumsy first attempts may give way to genuinely more capable models. But of course, progress is never certain. There could be a lot more hurdles to overcome.&lt;/p&gt;
&lt;p&gt;This prospect may seem to diminish the human element of mathematics, but perhaps that fear is unfounded. Computers have decisively solved the games of chess and Go. Stockfish and AlphaGo can defeat any human player on Earth with ease. Yet, chess has never been more popular. These powerful systems have not killed the game; they have become indispensable tools for training, analysis, and appreciation, making the game more accessible to more people than ever before. It is possible the same will hold true for mathematics. As these strange and powerful new tools proliferate, they may not render the human mathematician obsolete. Instead, they might democratize access to mathematical reasoning, allowing more people to participate in the oldest and most abstract of sciences. Perhaps the frontiers of knowledge will widen at an incredible pace, but the path to that frontier will be more accessible for everyone. And maybe that is a very bright future indeed.&lt;/p&gt;
</description>
      <pubDate>Wed, 11 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/future_math_weird/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/future_math_weird/</guid>
    </item>
    
    <item>
      <title>Does the Star Trek Computer Run on COBOL?</title>
      <description>&lt;h1 id=&quot;does-the-star-trek-computer-run-on-cobol%3F&quot; tabindex=&quot;-1&quot;&gt;Does the Star Trek Computer Run on COBOL?&lt;/h1&gt;
&lt;p&gt;We like to imagine the future as sleek, seamless, and intelligently designed — a world where a single voice query to a starship&#39;s LCARS interface dispatches 575 trillion calculations per nanosecond through the bio-neural gelpacks (biological FPGAs), to run a massively parallel computation to calculate the optimal molecular dynamics for the replicator to materialize the perfect cup of Earl Grey, hot, as an instantaneous, almost magical, manifestation of desire. But what if the reality is &lt;a href=&quot;https://scifi.stackexchange.com/questions/178974/which-programming-languages-are-used-in-the-star-trek-universe&quot;&gt;less Star Trek&lt;/a&gt; and more like our current software landscape, just with centuries more layers of &lt;a href=&quot;https://archive.is/Fq9r2&quot;&gt;legacy code piled&lt;/a&gt; on top?&lt;/p&gt;
&lt;p&gt;Imagine a starship doing relativistic frame calculations for spaceflight, but at the root of the system is a timer counting down from the Unix epoch in 1970. A little &lt;code&gt;time_t&lt;/code&gt; integer overflow, and suddenly your carefully plotted course to Proxima Centauri B is aiming you directly into a black hole, all because someone, somewhere, 400 years ago, didn&#39;t think anyone would still be using that particular library. And when you try to fire the engines to correct course, they fail to ignite because the engine control system was compiled against glibc 2.35 but the navigation computer is running 2.34. Vernor Vinge&#39;s novel &lt;em&gt;A Deepness in the Sky&lt;/em&gt; paints a wonderfully terrifying picture of this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So behind all the top-level interfaces was layer under layer of support. Some of that software had been designed for wildly different situations. Every so often, the inconsistencies caused fatal accidents. Despite the romance of spaceflight, the most common accidents were simply caused by ancient, misused programs finally getting their revenge.&lt;br /&gt;
“We should rewrite it all,” said Pham.&lt;br /&gt;
“It’s been done,” said Sura, not looking up. She was preparing to go off-Watch, and had spent the last four days trying to root a problem out of the coldsleep automation.&lt;br /&gt;
“It’s been tried,” corrected Bret, just back from the freezers. “But even the top levels of fleet system code are enormous. You and a thousand of your friends would have to work for a century or so to reproduce it.” Trinli grinned evilly. “And guess what—even if you did, by the time you finished, you’d have your own set of inconsistencies. And you still wouldn’t be consistent with all the applications that might be needed now and then.”&lt;br /&gt;
Sura gave up on her debugging for the moment. “The word for all this is ‘mature programming environment.’ Basically, when hardware performance has been pushed to its final limit, and programmers have had several centuries to code, you reach a point where there is far more signicant code than can be rationalized. The best you can do is understand the overall layering, and know how to search for the oddball tool that may come in handy&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Vinge&#39;s depiction of a &amp;quot;mature programming environment&amp;quot; where layers of ancient, often misunderstood code form the bedrock of advanced civilizations is incredibly compelling in its cynicism (or perhaps hard science fiction realism). It&#39;s merely an extrapolation of trends already deeply embedded in our own societies approach to software development. The romantic notion of constantly rewriting and perfecting our digital infrastructure heritage often collides with the gritty pragmatic reality: the overwhelming cost-to-benefit ratio of replacing systems that, for all their quirks, simply &lt;em&gt;work&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Consider the vast edifice of scientific and engineering software. Even today, a significant portion of high-performance numerical computing—the kind that powers climate models, astrophysical simulations, and complex engineering designs—still relies on libraries and kernels written in FORTRAN. Not necessarily FORTRAN 77 in its rawest form, but codebases whose lineage and core algorithms trace back decades. Every time you &lt;code&gt;pip install scipy&lt;/code&gt; for a mundane optimization routine, there&#39;s a vast &lt;a href=&quot;https://github.com/search?q=repo%3Ascipy%2Fscipy++language%3AFortran&amp;amp;type=code&quot;&gt;swatch of Fortran&lt;/a&gt; at play and for good reason. These aren&#39;t trivial pieces of code; they are often the product of person-centuries of development, validation, and refinement. The prospect of rewriting them in a &amp;quot;modern&amp;quot; language, ensuring bit-for-bit identical (and correct) output, and then re-validating the entire new stack against decades of experimental data and theoretical work, is a task so monumental and fraught with risk that it&#39;s rarely undertaken. Why would it be, when the existing code, running on increasingly powerful hardware or within sophisticated modern wrappers, continues to deliver reliable results?&lt;/p&gt;
&lt;p&gt;One could argue this isn&#39;t a failure of progress, but a pragmatic consequence of it. The stability and proven correctness of these foundational blocks allow scientists and engineers to build &lt;em&gt;higher&lt;/em&gt;, to ask more complex questions, rather than perpetually rebuilding the basement. The same principle applies, perhaps even more acutely, to the invisible infrastructure of global finance. Core banking systems, transaction processing engines, and stock market clearinghouses often run on software written COBOL, on mainframe architectures that seem anachronistic to a generation raised on cloud computing. The interconnectedness is staggering; countless other systems, from national payment networks to individual corporate accounting software, are built to interface with these legacy cores. A &amp;quot;rewrite&amp;quot; isn&#39;t just a coding project; it&#39;s a global logistical, economic, and political challenge of the highest order.&lt;/p&gt;
&lt;p&gt;Yet, these systems process trillions of dollars in transactions daily with incredible reliability. The idea of halting, even for a week, the machinery that underpins global capitalism to attempt a wholesale replacement is unthinkable. Because capitalism is inevitable and there is no alternative, right? Or maybe best not to think about such questions.&lt;/p&gt;
&lt;p&gt;But as these systems become more deeply embedded and more critical, a peculiar thing happens: they become taken for granted. The intricacies of their internal workings fade from common institutional knowledge. Successive generations of engineers learn to interface with them, to build around them, to coax new functionalities by adding new layers, but the core itself becomes an opaque black box. It works, so why delve too deeply into the &amp;quot;how,&amp;quot; especially when the original architects are long retired or deceased, and the documentation is sparse, outdated, or both?&lt;/p&gt;
&lt;p&gt;Since I love science fiction literature, I&#39;ve been thinking about this a lot. Here are some of the more interesting outcomes I&#39;ve come up with. Each one could probably be a humerous Greg Egan-style novella.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Ritualistic Maintainers:&lt;/strong&gt; Imagine a future where critical infrastructure—say, atmospheric processing for a terraformed Mars, or the global food production AI—runs on a core operating system whose deepest layers were written in the late 21st century. No living human fully understands its architecture. Maintenance becomes a highly specialized, almost priestly role, involving the careful application of historically successful patches and workarounds, interpreting cryptic log files whose error codes reference hardware that hasn&#39;t existed for centuries, all while hoping not to disturb the delicate, poorly understood equilibrium of the ancient code. New features are bolted on through increasingly elaborate APIs that treat the core as an unchangeable, god-given constant. Really not much different from the &lt;a href=&quot;https://xkcd.com/2347/&quot;&gt;current state&lt;/a&gt; of software development, to be honest.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cascading Obsolescence as Systemic Risk:&lt;/strong&gt; A seemingly minor, ubiquitous standard from our near future—perhaps a data compression algorithm or a cryptographic protocol from the 2040s—becomes deeply embedded in countless automated systems. Decades later, a subtle flaw is discovered, or the hardware to efficiently process it becomes scarce. But it&#39;s so deeply woven into the fabric of, for example, interplanetary logistics or medical diagnostic equipment, that phasing it out without causing massive disruption is a multi-generational project. The &amp;quot;Y2K bug&amp;quot; might seem trivial compared to the &amp;quot;2275 &lt;a href=&quot;https://blog.pkh.me/p/37-gcc-undefined-behaviors-are-getting-wild.html&quot;&gt;GCC Undefined Behavior&lt;/a&gt; Crisis.&amp;quot;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Rise of Software Archaeologists:&lt;/strong&gt; When a truly novel problem arises, or a deep-seated bug in the mature programming environment finally surfaces with critical consequences, society might rely on specialists who aren&#39;t innovators in any traditional sense, but rather &amp;quot;software archaeologists.&amp;quot; Their expertise lies in reverse-engineering and understanding these digital antiquities, using AI-assisted tools to analyze compiled code from forgotten languages, sifting through petabytes of historical data archives for fragments of documentation or ancient online forum discussions that might offer a clue. Maybe future historians will debate interpretations of APL code like we do with Minoan Linear A scripts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Progress as Increasingly Complex Abstraction:&lt;/strong&gt; True innovation continues, but often at the periphery. We might develop incredibly sophisticated AI that can diagnose rare diseases, but the AI&#39;s learning algorithms might still be outputting results into a data format originally designed for 21st-century hospital record systems, because that&#39;s what the national health database, with its petabytes of historical (and legally mandated) data, still requires at its core. The AI doesn&#39;t &lt;em&gt;replace&lt;/em&gt; the old system; it learns to expertly navigate its limitations and idiosyncrasies. Just as we carefully preserve pre-atomic &lt;a href=&quot;https://en.wikipedia.org/wiki/Low-background_steel&quot;&gt;low-background steel&lt;/a&gt; for sensitive scientific instruments, vast archives of Reddit threads and Twitter posts are maintained in specialized data centers, their antiquated social media discourse essential for bootstrapping new language models that need to understand the linguistic roots of modern communication.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Is this a form of stagnation, a build-up of civilizational technical debt that will eventually come due? Perhaps. But it can also be seen as a strange, emergent form of progress. Each &amp;quot;solved&amp;quot; problem, encapsulated in a piece of software that becomes part of the unquestioned infrastructure, allows subsequent generations to focus their intellectual energy on new challenges, standing on the shoulders of digital giants whose names and methods they may no longer even know.&lt;/p&gt;
&lt;p&gt;Alternatively, a more advanced (and arguably more enlightened) society might adopt a philosophy akin to the Shinto shrines in Japan. Where the shrines are completely demolished and meticulously rebuilt on an adjacent site every 20 years, a tradition known as &lt;a href=&quot;https://en.wikipedia.org/wiki/Seng%C5%AB&quot;&gt;Shikinen Sengu&lt;/a&gt;, practiced for over 1300 years. This isn&#39;t renovation; it&#39;s a complete rebuild using fresh materials and ancient techniques, rooted in beliefs about impermanence, purity, and the transfer of knowledge to the next generation. Not unlike how many 50 year old Unix utilities are currently being rewritten in &lt;a href=&quot;https://www.theregister.com/2025/05/08/ubuntu_2510_makes_rusk_sudo_default/&quot;&gt;memory-safe languages&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;However, such an approach would likely require a societal structure far more advanced and economically different from our current one. It presupposes a society where resources and skilled human-time are sufficiently abundant to undertake these monumental rebuilds not out of immediate crisis, but as a matter of principle and long-term health. It would also imply a culture where software development is treated less like the current dog-eat-dog, hype-driven lunatic fest focused on rapid iteration, enclosure, and market capture, and more like a deeply valued craft, akin to the meticulous artisanship and traditions to persist. And anyone who works in software today knows that&#39;s a far, far, removed from what we do today.&lt;/p&gt;
&lt;p&gt;The future of software may not be one of constant, radical reinvention from the ground up, but rather a story of accretion, layering, and the increasingly sophisticated management of an ever-growing, ever-aging technological heritage. The most advanced systems of tomorrow might be running, at their very core, on the digital ghosts of today. Not sure if that&#39;s a scary thing, but it does imbue the craft of software development with a certain kind of reverence and weight worth considering.&lt;/p&gt;
</description>
      <pubDate>Thu, 05 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/software_far_future/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/software_far_future/</guid>
    </item>
    
    <item>
      <title>Interfacing MCP with Combinatorial, Convex, and SMT Solvers</title>
      <description>&lt;h1 id=&quot;interfacing-mcp-with-combinatorial%2C-convex%2C-and-smt-solvers&quot; tabindex=&quot;-1&quot;&gt;Interfacing MCP with Combinatorial, Convex, and SMT Solvers&lt;/h1&gt;
&lt;p&gt;Lately I&#39;ve been working on using MCP beyond just using it for &lt;a href=&quot;https://www.stephendiehl.com/posts/computer_algebra_mcp/&quot;&gt;symbolic algebra manipulations&lt;/a&gt;, I&#39;ve been thinking about how to interface large language models with a suite of dedicated solvers for scientific computing exposed as tools to the model. And particularly for automating workflows in physical engineering disciplines.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/usolver&quot;&gt;Github Source Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git clone https://github.com/sdiehl/usolver.git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&#39;re not familiar with MCP, you can read more about it in my &lt;a href=&quot;https://www.stephendiehl.com/posts/computer_algebra_mcp/&quot;&gt;previous post&lt;/a&gt; or the thousand other breathless thinkpieces on the internet about how it&#39;s the greatest thing since sliced bread or something. But in reality it&#39;s just a janky way to expose a set of function calls (called tools) to a language model via a JSON RPC interface. Language are models are great at language interpretation and limited &amp;quot;planning&amp;quot;, and numerical solvers are great at numerical computation. So let each system do what they do best and synthesize the two to build a hybrid system. MCP is just the glue to bind these scientific Python libraries with the model, essentially the &#39;cgi-bin&#39; of AI.&lt;/p&gt;
&lt;p&gt;The simplest motivation for combining language models with numerical solvers via MCP is that it&#39;s &lt;em&gt;vastly&lt;/em&gt; more efficient for certain problems. Consider the dumb example that you certainly can feed a Sudoku puzzle into one of the frontier reasoning models (o3, deepseek r1, etc) and after spinning for quite some time it can (maybe) solve it by talking to &amp;quot;itself&amp;quot; about trial and error guesses for quite some time. The chain of thought will be this vast sequence of guesses and backtracking. But, you can also feed it into a dedicated numerical or logical solver and it will solve it nearly instantly. The same thing goes for many well-studied classical computer science problems: boolean SAT, job shop, vehicle routing, knapsack problem, shift scheduling, travelling salesman etc. There are a lot of pre-existing efficient solvers that research groups have been building and tuning for decades.&lt;/p&gt;
&lt;p&gt;As a less theoretical example, consider a common chemical engineering challenge: specifying the design parameters for a processing plant, focusing on line sizing and the associated fluid dynamics. In a typical day to day workflow engineers must determine optimal pipe diameters and requisite pump specifications to achieve target output flow rates for various chemical products. This task is essentially a system of coupled, often non-linear, governing equations encompassing fluid properties (density, viscosity), mass and energy balances across the network, and pressure drop calculations.&lt;/p&gt;
&lt;p&gt;Any design must adhere to operational limits, such as maximum allowable pressures within each pipe segment to ensure structural integrity and safety, minimum flow rates to maintain reaction kinetics or prevent settling, and the physical topology of how pipes, valves, and reactors are interconnected. There are dedicated solvers for this, but they are very expensive, and generally suck. Or people write lots of Excel macros and Goal Seek, which I guess works, but there are much better tools.&lt;/p&gt;
&lt;p&gt;The bottleneck is simply that the APIs for many of these open source SMT and convex solvers can be, let&#39;s say, character-building. If an LLM, piped through MCP, could translate your plain English &amp;quot;what if&amp;quot; into the arcane syntax these solvers speak, suddenly these super-powerful, often byzantine, systems become way less intimidating and democratize access beyond the need for relying on specialized programmers.&lt;/p&gt;
&lt;p&gt;Imagine instead, just telling an LLM, via MCP: &amp;quot;Given these target flow rates for product X, using fluid Y with these properties, and ensuring pressure never exceeds Z psi in any line, what are the optimal pipe diameters and pump specs for lines A, B, and C, which are connected in this configuration? Imagine a process engineer who typically spends a significant portion of their week iteratively translating evolving production targets and safety parameters into precise mathematical constraints for solver input, then painstakingly validating the complex outputs for, say, a new heat exchanger network.&lt;/p&gt;
&lt;p&gt;With an LLM-solver integration this same engineer could articulate high-level objectives and constraints in natural language—&amp;quot;design a network to cool stream X from 200°C to 50°C using available cold utility Y, minimizing operational cost while ensuring no tube-side pressure exceeds Z&amp;quot;—and have the system automatically formulate the problem, execute the appropriate solver, and present digestible design specifications. This could compress what was previously a multi-day, error-prone exercise as a human constraint generator and checker into minutes, liberating valuable engineering expertise to focus on genuinely creative challenges of their role.&lt;/p&gt;
&lt;p&gt;And not just for this one specific field. The dream is to be able to describe all of the constraints for many complex systems (be it a chemical plant, hospital shift planning, or portfolio optimization) in natural language so that the LLM can plan the constraints, delegate to the solver(s), and let the user have a back and forth with about the problem and solution in way that is natural to them. If done well (and we&#39;re not there yet) that would be very very powerful.&lt;/p&gt;
&lt;p&gt;Then there&#39;s more... let&#39;s call it forward-looking play: generating synthetic reasoning data. This one&#39;s a bit more niche. If we can get an LLM to bulk formulate tricky problems for these solvers, and then learn from the solver&#39;s perfectly structured, provably correct answers, we could bootstrap a ton of high-quality training data and then feed that back in as instruction tuning data.&lt;/p&gt;
&lt;h2 id=&quot;prototype&quot; tabindex=&quot;-1&quot;&gt;Prototype&lt;/h2&gt;
&lt;p&gt;But let&#39;s start with the easy ones. That&#39;s the &lt;strong&gt;Stanford cvxopt&lt;/strong&gt; for convex optimization, &lt;strong&gt;Microsoft&#39;s z3&lt;/strong&gt; SMT solver, and &lt;strong&gt;Google or-tools&lt;/strong&gt; for combinatorial optimization. Just these three packages alone are extremely powerful. Like a seashell, if you look at their source code you can hear the screams of hundreds of PhD students sacrificed on the altar of science to build them.&lt;/p&gt;
&lt;p&gt;I&#39;ve built a prototype of this called &lt;a href=&quot;https://github.com/sdiehl/usolver&quot;&gt;&lt;strong&gt;USolver&lt;/strong&gt;&lt;/a&gt; which implements the MCP interface to the solvers.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at some examples. For chemical engineering problems, you can describe a water transport pipeline design task in plain English which gets turned into a Z3 constraint satisfaction problem.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Design a water transport pipeline with the following requirements:

* Volumetric flow rate: 0.05 m³/s
* Pipe length: 100 m
* Water density: 1000 kg/m³
* Maximum allowable pressure drop: 50 kPa
* Flow continuity: Q = π(D/2)² × v
* Pressure drop: ΔP = f(L/D)(ρv²/2), where f ≈ 0.02 for turbulent flow
* Practical limits: 0.05 ≤ D ≤ 0.5 m, 0.5 ≤ v ≤ 8 m/s
* Pressure constraint: ΔP ≤ 50,000 Pa
* Find: optimal pipe diameter and flow velocity
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: center; margin: 0 auto; padding: 20px 0&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/pfd.png&quot; alt=&quot;Optimal nurse scheduling solution&quot; style=&quot;max-width: 500px; display: block; margin: 0 auto&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;The LLM translates this into a Z3 constraint satisfaction problem, handling all the messy details of flow equations and physical constraints. You get back optimal pipe dimensions and flow parameters that satisfy all your requirements.&lt;/p&gt;
&lt;p&gt;Or take a classic operations research problem like employee scheduling. Instead of wrestling with complex combinatorial optimization syntax, you can just say:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Use usolver to solve a nurse scheduling problem with the following requirements:

* Schedule 4 nurses (Alice, Bob, Charlie, Diana) across 3 shifts over (Monday, Tuesday, Wednesday)
* Shifts: Morning (7AM-3PM), Evening (3PM-11PM), Night (11PM-7AM)
* Each shift must be assigned to exactly one nurse each day
* Each nurse works at most one shift per day
* Distribute shifts evenly (2-3 shifts per nurse over the period)
* Charlie can&#39;t work on Tuesday.
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: center; margin: 0 auto; padding: 20px 0&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/nurses.png&quot; alt=&quot;Optimal nurse scheduling solution&quot; style=&quot;max-width: 500px; display: block; margin: 0 auto&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;But where it gets really interesting is when you chain these solvers together. Here&#39;s an example where we optimize a restaurant&#39;s operations by combining two different solvers in sequence. First, we use Google&#39;s OR-Tools combinatorial optimization to determine the optimal mix and layout of different sized tables within a restaurant&#39;s available dining area, maximizing total seating capacity while respecting minimum table requirements and space constraints.&lt;/p&gt;
&lt;p&gt;Then, we take that optimal seating capacity and feed it into the cvxopt solver to determine the most cost-effective staffing schedule across a 12-hour day. The staffing optimizer accounts for variable customer demand throughout the day, ensures adequate coverage for the calculated seating capacity, and minimizes labor costs while maintaining service quality and reasonable shift changes. This demonstrates how we can specify a a business problem in plain English, compile it down into a system of constraints, turn those constraints into a system of equations, and then solve it using a chain of numerical solvers with the LLM orchestrating the flow of data and verbalizing the answers!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Use usolver to optimize a restaurant&#39;s layout and staffing with the following requirements in two parts. Use combinatorial optimization to optimize for table layout and convex optimization to optimize for staff scheduling.

* Part 1: Optimize table layout
  - Mix of 2-seater, 4-seater, and 6-seater tables
  - Maximum floor space: 150 m²
  - Space requirements: 4m² (2-seater), 6m² (4-seater), 9m² (6-seater)
  - Maximum 20 tables total
  - Minimum mix: 2× 2-seaters, 3× 4-seaters, 1× 6-seater
  - Objective: Maximize total seating capacity

* Part 2: Optimize staff scheduling using Part 1&#39;s capacity
  - 12-hour operating day
  - Each staff member can handle 20 seats
  - Minimum 2 staff per hour
  - Maximum staff change between hours: 2 people
  - Variable demand: 40%-100% of capacity
  - Objective: Minimize labor cost ($25/hour per staff)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then almost like magic, the LLM coordinates the two solvers and returns the optimal solution. It figures out the optimal table layout and then uses that to schedule the staff. Here&#39;s what Claude Desktop generates given the project specification.&lt;/p&gt;
&lt;div style=&quot;text-align: center; margin: 0 auto; padding: 20px 0&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/z3_part1.png&quot; alt=&quot;Optimal table layout solution&quot; style=&quot;max-width: 500px; display: block; margin: 0 auto&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;And then figures out the optimal staffing schedule for that optimal table layout.&lt;/p&gt;
&lt;div style=&quot;text-align: center; margin: 0 auto; padding: 20px 0&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/z3_part2.png&quot; alt=&quot;Optimal staffing schedule&quot; style=&quot;max-width: 500px; display: block; margin: 0 auto&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;And it&#39;s pretty magical that such a complex problem can be solved in a few seconds with such a simple interface. I&#39;m incredibly bullish about this kind of approach and expanding the set of available tools for the LLM to include many more advanced solvers.&lt;/p&gt;
&lt;p&gt;Although don&#39;t drink too much of the breathless AI hype Kool-aid just yet. There are some quite real limitations in this approach. Namely, that there is no strict guarantees that LLMs are able to do semantic parsing of the problem with complete accuracy. We can partially mitigate this exposing a computer algebra system and having it check the solutions at each step. There are still mechanisms for transpositions and misinterpretations to slip in.&lt;/p&gt;
&lt;p&gt;If this kind of approach becomes mainstream, there could also be benchmarks like SWE-bench for tool-calling of scientific computing packages that could be used to improve model performance upstream. But at the moment it&#39;s still very much a best-effort approach and you would certainly need to have a human-in-the-loop to check the solutions, especially in any kind of safety-critical application. But it might be able to make that human a lot more productive.&lt;/p&gt;
&lt;h2 id=&quot;the-vision-for-universal-solver-interface&quot; tabindex=&quot;-1&quot;&gt;The Vision for Universal Solver Interface&lt;/h2&gt;
&lt;p&gt;Consider the potential of a unified interface that could seamlessly integrate with state-of-the-art solvers across multiple domains with frontier language models via MCP (or whatever the next generation of MCP is):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Z3&lt;/strong&gt; - SMT solver for constraint satisfaction over booleans, integers, reals, and strings&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CvxPy&lt;/strong&gt; - Framework for convex optimization problems&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Or-Tools&lt;/strong&gt; - Suite for combinatorial optimization and constraint programming&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HiGHS&lt;/strong&gt; - LP, MIP, and QP solver&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MiniZinc&lt;/strong&gt; - Discrete optimization solver&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GAP&lt;/strong&gt; - Computational group theory solvers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clingo&lt;/strong&gt; - Answer set programming system&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flint&lt;/strong&gt; - Number theory computations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TLA+ / Apalache&lt;/strong&gt; - Temporal logic solver&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Egglog&lt;/strong&gt; - Term rewriting and equality saturation engine&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SymPy&lt;/strong&gt; - Comprehensive computer algebra system for differential equations solvers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Souffle&lt;/strong&gt; - Datalog-based declarative logic programming framework&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mathematica&lt;/strong&gt; - Risch-based symbolic integration solver and simplification routines&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sage&lt;/strong&gt; - Umbrella project for many scientific computing packages&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The implementation of such a unified system would be a very powerful and useful piece of software. It would effectively be a meta-solver: an intelligent system capable of decomposing complex problems, identifying the optimal solver for each component, and orchestrating their collective execution. While developing a theoretically complete meta-solver is a substantial theoretical and practical challenge in it&#39;s generality, don&#39;t really need to solve the general problem to be incredibily useful.&lt;/p&gt;
&lt;p&gt;This project started as a weekend hobby project, but I believe this is a fascinating area with significant potential. I&#39;m particularly interested in how these techniques could be applied to automation in physical engineering disciplines. If you&#39;re working on similar ideas in an open source or commercial context, I&#39;d love to hear about your work, feel &lt;a href=&quot;https://www.stephendiehl.com/hire/&quot;&gt;free to reach out&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Mon, 02 Jun 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/smt_and_mcp_solvers/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/smt_and_mcp_solvers/</guid>
    </item>
    
    <item>
      <title>The Trump Crypto Orgy of Corruption</title>
      <description>&lt;h1 id=&quot;the-trump-crypto-orgy-of-corruption&quot; tabindex=&quot;-1&quot;&gt;The Trump Crypto Orgy of Corruption&lt;/h1&gt;
&lt;p&gt;We are living in very unusual times and corrupt times. Never before has an American president been so brazen in his corruption and self-enrichment. This new phenomenon, what Senator Elizabeth Warren coined as an &lt;a href=&quot;https://www.bbc.co.uk/news/articles/c74nn4ppdvdo&quot;&gt;&amp;quot;orgy of corruption,&amp;quot;&lt;/a&gt; is staggering in scope, and while I&#39;m under no illusion that writing about this will be news to anyone, I feel compelled to document it for the public record as it is far worse than anything I predicted back in &lt;a href=&quot;https://www.stephendiehl.com/posts/against_crypto&quot;&gt;2020&lt;/a&gt;. The Trump administration&#39;s intermingling with crypto is like witnessing ten Watergates every day, marking a period of brazen self-enrichment and systemic decay utterly unprecedented in the annals of the American presidency, and indeed, unseen in any mature democracy. This is not a policy shift, but rather a flagrant embrace of practices typically confined to banana republics and autocracies, as the administration systematically dismantled regulatory oversight, fostered blatant conflicts of interest, and normalized a level of corruption that directly benefited the President and his inner circle.&lt;/p&gt;
&lt;p&gt;Under this administration, the Securities and Exchange Commission completely stopped its cryptocurrency enforcement activities. This retreat included &lt;a href=&quot;https://www.nytimes.com/2025/05/29/business/sec-binance-lawsuit-dropped.html&quot;&gt;abandoning numerous investigations and lawsuits against major industry players&lt;/a&gt;. Notably, the SEC &lt;a href=&quot;https://www.nytimes.com/2025/05/29/business/sec-binance-lawsuit-dropped.html&quot;&gt;dropped its lawsuit against Binance&lt;/a&gt;, the world&#39;s largest exchanges, even after its CEO had pleaded guilty to federal money laundering charges. The commission also moved to dismiss over a dozen other cases against crypto firms. A clear conflict of interest arose when the SEC announced it would not consider meme coins as securities, a decision made weeks after President Trump launched his own $TRUMP token, thereby &lt;a href=&quot;https://www.forbes.com/sites/tonyaevans/2025/04/14/trumps-crypto-coins-test-sec-ethics-and-us-regulatory-integrity/&quot;&gt;directly benefiting the president&#39;s financial interests&lt;/a&gt;. Further indicating a shift in approach, the SEC &lt;a href=&quot;https://www.cybersecuritydive.com/news/sec-cyber-crypto-enforcement-trump/740628/&quot;&gt;restructured its enforcement operations&lt;/a&gt;, replacing its specialized Crypto Assets and Cyber Unit with a new unit that signaled a more hands off stance on digital assets.&lt;/p&gt;
&lt;p&gt;The Department of Justice also underwent a significant policy change by &lt;a href=&quot;https://www.reuters.com/world/us/us-justice-dept-disbands-cryptocurrency-enforcement-unit-2025-04-08/&quot;&gt;completely disbanding the National Cryptocurrency Enforcement Team&lt;/a&gt;, a unit specifically created to combat complex crypto related crimes. A memo from Deputy Attorney General Todd Blanche, titled &lt;a href=&quot;https://www.justice.gov/dag/media/1395781/dl?inline&quot;&gt;&amp;quot;Ending Regulation By Prosecution,&amp;quot;&lt;/a&gt; directed prosecutors to &lt;a href=&quot;https://www.reuters.com/world/us/us-justice-dept-disbands-cryptocurrency-enforcement-unit-2025/04/08/&quot;&gt;cease pursuing charges for regulatory infractions under federal banking, securities, and commodities laws unless there was proof of intentional violations&lt;/a&gt;. This effectively shifted policy away from enforcement based regulation, immunizing cryptocurrency companies from prosecution for certain regulatory violations even when significant investor harm occurred.&lt;/p&gt;
&lt;p&gt;Unprecedented conflicts of interest emerged from the Trump family&#39;s extensive financial involvement in the cryptocurrency sector while simultaneously overseeing its regulation. For example, a cryptocurrency venture primarily owned by the Trump family, World Liberty Financial, saw its USD1 stablecoin &lt;a href=&quot;https://www.reuters.com/world/middle-east/wlfs-zach-witkoff-usd1-selected-official-stablecoin-mgx-investment-binance-2025-05-01/&quot;&gt;selected for a $2 billion investment in Binance facilitated by Abu Dhabi&#39;s sovereign wealth fund&lt;/a&gt;, directly linking a Trump family business to a major international financial transaction within the crypto space.&lt;/p&gt;
&lt;p&gt;President Trump himself launched the $TRUMP meme coin shortly before his inauguration, which &lt;a href=&quot;https://www.forbes.com/sites/tonyaevans/2025/04/14/trumps-crypto-coins-test-sec-ethics-and-us-regulatory-integrity/&quot;&gt;reached a significant market value&lt;/a&gt;. With the Trump Organization and affiliated companies reportedly controlling a substantial majority of the token&#39;s supply, concerns about market manipulation arose. The President used his platform to promote these tokens, even &lt;a href=&quot;https://www.telegraph.co.uk/business/2025/05/26/trump-family-to-seek-3bn-for-bet-on-cryptocurrency/&quot;&gt;hosting private dinners for major token holders at his golf club&lt;/a&gt;, an event so beyond the pale it completely lacks any historical precedent in its sheer audacity and brazenness. The subsequent SEC announcement that meme coins would not be regulated as securities appeared to be a policy decision that directly benefited the president&#39;s personal financial interests.&lt;/p&gt;
&lt;p&gt;Trump-affiliated entities have generated approximately 320 million dollars in trading fees from the TRUMP meme coin. These fees are collected through the coin&#39;s structure on decentralized exchanges, where CIC Digital LLC (a Trump Organization affiliate) and Fight Fight Fight LLC earn a share of trading fees on every coin exchange. In just the week following the dinner announcement in April, trading fees alone exceeded 1.3 million dollars. The profit model is particularly lucrative because it doesn&#39;t depend on the coin&#39;s price appreciation. what matters is constant trading volume, because more trades mean more fees.&lt;/p&gt;
&lt;p&gt;Further highlighting these conflicts, Trump Media &amp;amp; Technology Group, in which President Trump holds a majority stake managed by his son, was &lt;a href=&quot;https://www.telegraph.co.uk/business/2025/05/26/trump-family-to-seek-3bn-for-bet-on-cryptocurrency/&quot;&gt;reportedly in negotiations to raise $3 billion for cryptocurrency investments&lt;/a&gt;. This situation meant the president and his family could financially gain from these investments while simultaneously controlling the regulatory environment governing such activities.&lt;/p&gt;
&lt;p&gt;Regulatory appointments also raised concerns about industry capture. David Sacks, appointed as Trump&#39;s &amp;quot;crypto and artificial intelligence czar,&amp;quot; &lt;a href=&quot;https://markets.businessinsider.com/news/currencies/david-sacks-quiety-divested-from-crypto-company-at-center-of-conflict-of-interest-controversy-1034440568&quot;&gt;faced allegations that he could financially benefit from a strategic crypto reserve announcement made by Trump&lt;/a&gt;. Although Sacks claimed to have divested from personal crypto holdings, questions persisted regarding his venture capital firm Craft Ventures&#39; investment in Bitwise, a crypto index fund manager. The firm only confirmed its exit from this investment after public scrutiny and &lt;a href=&quot;https://markets.businessinsider.com/news/currencies/david-sacks-quiety-divested-from-crypto-company-at-center-of-conflict-of-interest-controversy-1034440568&quot;&gt;after the presidential announcement had already benefited the assets in question&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Brian Quintenz, nominated by Trump to chair the Commodity Futures Trading Commission, disclosed significant financial involvement in the cryptocurrency sector, reporting &lt;a href=&quot;https://cryptonews.com.au/news/trumps-cftc-nominee-unveils-3-4m-in-crypto-assets-and-industry-ties-129230/&quot;&gt;over $3.4 million in crypto-linked assets and extensive industry connections&lt;/a&gt;. His financial disclosures detailed stakes in Andreessen Horowitz funds, board positions at firms like Kalshi (a prediction markets operator), and prior advisory roles with companies such as Crypto dot com. Although Quintenz pledged to divest these holdings and recuse himself from matters where conflicts could arise, his nomination highlighted a trend of appointing individuals with direct financial interests in the industries they were tasked with regulating.&lt;/p&gt;
&lt;p&gt;The Trump administration formalized its deregulatory stance through a January 23, 2025 executive order titled &amp;quot;Strengthening American Leadership in Digital Financial Technology.&amp;quot; This order &lt;a href=&quot;https://www.pillsburylaw.com/en/news-and-insights/cryptocurrency-digital-assets-trump.html&quot;&gt;established a comprehensive framework for cryptocurrency deregulation&lt;/a&gt; and &lt;a href=&quot;https://natlawreview.com/article/new-era-crypto-regulation-innovation-crypto-executive-order-rebooted-sec-crypto&quot;&gt;revoked previous Biden administration policies&lt;/a&gt;. It also created a working group, chaired by David Sacks, tasked with developing a regulatory framework that prioritized industry growth over investor protection. The executive order mandated federal agencies to review and rescind prior regulatory guidance, effectively institutionalizing the dismantling of cryptocurrency oversight and normalizing an approach where industry preferences could supersede regulatory prudence.&lt;/p&gt;
&lt;p&gt;The operations of Trump family linked entities like World Liberty Financial, particularly its involvement in international crypto transactions such as its &lt;a href=&quot;https://www.reuters.com/world/middle-east/wlfs-zach-witkoff-usd1-selected-official-stablecoin-mgx-investment-binance-2025-05-01/&quot;&gt;stablecoin being used in a multi-billion dollar deal involving Binance and an Abu Dhabi sovereign wealth fund&lt;/a&gt;, raised questions about the intertwining of private business interests with entities subject to U.S. regulatory scrutiny or foreign government influence.&lt;/p&gt;
&lt;p&gt;Trump&#39;s family wealth has reportedly increased by &lt;a href=&quot;https://www.cbsnews.com/news/trump-family-net-worth-crypto-investments/&quot;&gt;$2.9 billion&lt;/a&gt; largely due to cryptocurrency investments, with 40% of that wealth currently held in tokens. This staggering enrichment, directly coinciding with policies designed to unleash the very crypto markets from which they profited, is not merely a footnote in financial history; it is a funeral dirge for democratic accountability and a grotesque caricature of public service. In a way, this is cryptocurrency fulfilling its ultimate purpose — serving as the perfect vehicle for corruption and fraud.&lt;/p&gt;
&lt;p&gt;The sheer, unadulterated brazenness of this spectacle, where the highest public office in the land is so transparently leveraged for personal, familial gain, makes the Teapot Dome scandal (the previous largest scandal in American history) look like a petty squabble and the influence peddling of past eras seem almost amateurish by comparison. We are staring into the abyss of a new American kleptocracy, where the lines between the presidency and a personal enrichment scheme have been purposefully, profitably, and catastrophically erased. This is truly a full-throated embrace of oligarchy that threatens to dwarf the Gilded Age robber barrons, The only historical precedent is the plundering of the state much like what occured during the collapse of the USSR. We are witnessing the forging an era of unprecedented wealth inequality and systemic rot that may leave the very foundations of American democracy shattered beyond recognition. It&#39;s truly hard to find the words to describe this grotesque spectacle, but I will continue documenting it in hopes to add to the public record and contextualize this new low in American history.&lt;/p&gt;
</description>
      <pubDate>Sat, 31 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/trump_crypto_orgy_corruption/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/trump_crypto_orgy_corruption/</guid>
    </item>
    
    <item>
      <title>Unnecessary and Unstable: Why Stablecoins are Bad</title>
      <description>&lt;h1 id=&quot;unnecessary-and-unstable%3A-why-stablecoins-are-bad&quot; tabindex=&quot;-1&quot;&gt;Unnecessary and Unstable: Why Stablecoins are Bad&lt;/h1&gt;
&lt;p&gt;The current financial policy debate in the US is increasingly fixated on the purported necessity of bespoke regulation for stablecoins (particularly the proposed &lt;a href=&quot;https://www.congress.gov/bill/119th-congress/senate-bill/394/text&quot;&gt;S.394 GENIUS Act&lt;/a&gt;), with proponents arguing that such frameworks would foster responsible innovation and integrate these digital tokens into the broader financial system. This perspective, however, is profoundly misguided. Rather than ushering in an era of enhanced financial efficiency, the legitimization of stablecoins through bespoke regulation would serve primarily to institutionalize inherently fragile financial structures, proliferate risk, and ultimately provide a novel vector for systemic crises, all in service of a financial product that offers no net aggregate good for the economy. The assertion that stablecoins are essential for modernizing money transmission is a fallacy; existing, robust, and regulated mechanisms already fulfill this function with far greater security and accountability.&lt;/p&gt;
&lt;p&gt;The argument against stablecoins doesn&#39;t stem from Luddism, but a measured assessment of their intrinsic risks and impact on consumers and the financial system. Stablecoins, despite their name, are demonstrably unstable. Their brief history is littered with instances of de-pegging, runs, and the evaporation of billions in investor capital. This is not an aberration but a feature. Any financial instrument designed to mimic the stability of sovereign currency, yet lacking the explicit, unconditional guarantee of a central bank and a sovereign government, is inherently prone to &lt;a href=&quot;https://bettermarkets.org/newsroom/fact-sheet-three-questions-for-any-unstablecoin-legislation/&quot;&gt;run risk&lt;/a&gt;. Unlike central bank money, which maintains &amp;quot;unicity&amp;quot; (i.e. the same value and acceptance everywhere through the central bank&#39;s role as payment overseer and ultimate settler), stablecoins cannot guarantee consistent value across all contexts and jurisdictions. The attempts to engineer stability through various collateralization models have all proven fallible.&lt;/p&gt;
&lt;p&gt;The three principal archetypes of stablecoins each present unique and severe vulnerabilities. Off-chain collateralized stablecoins, often pegged to the U.S. dollar, purportedly maintain their value through reserves of assets like cash, U.S. Treasury securities, or commercial paper. However, the composition and true liquidity of these reserves are often shrouded in opacity. Issuers frequently alter reserve compositions and disclosure practices with little to no advance notice, introducing significant uncertainty. The inclusion of cash equivalents, which can encompass short-term investment securities like commercial bonds, introduces credit and market risk. If these reserve assets depreciate or become illiquid during periods of market stress, a common occurrence, the stablecoin&#39;s ability to maintain its peg is compromised, triggering redemptions and potential runs. The failure to rigorously safeguard these assets, or even accurately represent their holdings, undermines confidence and acts as a catalyst for instability. The events surrounding FTX and Silicon Valley Bank, which precipitated de-pegging and runs on even prominent stablecoins like Tether and USDC, serve as stark reminders. The question is not if such stablecoins will depeg under stress, but when and with what magnitude.&lt;/p&gt;
&lt;p&gt;On-chain collateralized stablecoins attempt to achieve stability by holding other cryptocurrencies as collateral. This model introduces a pernicious feedback loop: the collateral itself is subject to extreme price volatility relative to the real currency it is meant to back. A downturn in the crypto markets can rapidly erode the value of the collateral, triggering margin calls and forced liquidations, which in turn can de-stabilize the stablecoin and exacerbate the broader market decline. This design is akin to building a house on shifting sands. Arguably the most dangerous variant, non-collateralized, or algorithmic, stablecoins eschew tangible reserves entirely, relying instead on complex algorithms and smart contracts to manage supply and demand in an attempt to maintain a peg. These are, in essence, perpetual motion machines of finance. The catastrophic collapse of TerraUSD in May 2022, an eighteen billion dollar algorithmic stablecoin, erased vast sums of capital and sent shockwaves through the crypto ecosystem, demonstrating the inherent infeasibility and extreme risk of such unbacked constructs.&lt;/p&gt;
&lt;p&gt;A cornerstone of the stablecoin illusion is the promise of at-par redemption at any moment; in practice, this is often far from guaranteed. Issuers frequently impose significant constraints on redemption rights, including high minimum redemption thresholds that render them inaccessible to ordinary retail users. Furthermore, public disclosures regarding redemption terms are often insufficient or misleading. Notably, some of the largest stablecoin issuers do not offer a direct right of redemption to investors who acquire their tokens on unregulated cryptocurrency exchanges, leaving these holders reliant on the liquidity and solvency of those secondary market platforms, themselves often fragile entities. Terms of service, frequently mischaracterized by issuers, can further restrict or delay redemptions, particularly during periods of market stress when liquidity is most needed.&lt;/p&gt;
&lt;p&gt;So why would anyone use stablecoins if they are unstable and worse than existing payment systems? The answer lies in their singular distinguishing feature: unlike bank deposits, which are subject to comprehensive regulatory oversight and reporting requirements, the ownership and disposition of stablecoins remains pseudonymous. This anonymity is precisely what makes them valuable to those seeking to circumvent legal frameworks—offshore gamblers, money launderers, extortionists, drug traffickers, sanctions evaders, and other criminal enterprises who require the digital equivalent of unmarked bills with global transmission capabilities.&lt;/p&gt;
&lt;p&gt;The crypto ecosystem functions as an offshore casino offering products prohibited within traditional financial systems, and stablecoins serve as the necessary chips to participate. Strip away the technological veneer, and stablecoins reveal themselves as sophisticated tools designed explicitly to evade the anti-money laundering and know-your-customer requirements that undergird legitimate finance. In essence, the primary—and arguably only—economic rationale for stablecoins is to facilitate criminal activity, providing a pseudonymous payment rail for those whose operations cannot withstand the scrutiny of regulated financial systems.&lt;/p&gt;
&lt;p&gt;The parallels between stablecoins and the pre-FDIC banking system are deeply unsettling. The devastating bank runs of the 1930s led to the creation of federal deposit insurance and strict regulation of bank risk-taking - protections that stablecoins deliberately circumvent as a new form of shadow banking. While stablecoin issuers attempt to reassure holders by maintaining large reserves of U.S. government debt, this creates a dangerous feedback loop: a run on stablecoins would force issuers to conduct a fire sale of Treasury bills, potentially destabilizing the U.S. government debt market and driving up interest rates. Even money market funds, which are far more regulated and transparent than stablecoins, experienced catastrophic runs during both the 2008 Financial Crisis and March 2020 turmoil, requiring massive government intervention to prevent broader market collapse. Stablecoins, with their greater opacity and weaker oversight, present an even more acute systemic risk. Most alarmingly, their ability to absorb vast amounts of foreign capital would create a direct channel for overseas instability to violently propagate into U.S. Treasury markets - threatening the bedrock of the global financial system, all while primarily serving to facilitate criminal activity.&lt;/p&gt;
&lt;p&gt;The bank failures of early 2023, including Silvergate, Silicon Valley Bank, Signature, and First Republic, provided a grim preview of how crypto-related activities can destabilize regulated financial institutions. These banks held substantial uninsured deposits from crypto firms, and withdrawals by these firms during the crypto downturn directly contributed to bank runs. Integrating stablecoins more formally into the U.S. financial system through bespoke regulation would effectively build a high-speed conduit between the largely unregulated crypto casino and the rule-bound, regulated financial sector. This would not foster responsible innovation but rather create new channels for contagion, allowing chaos from the crypto sphere to infect the core financial system.&lt;/p&gt;
&lt;p&gt;The value of stablecoins&#39; reserves is deeply intertwined with the safety and stability mechanisms of conventional finance. These reserves are custodied at banks, held in money market funds, and managed through repo arrangements; which derive their security from Federal government regulations and oversight. This interdependence was starkly illustrated when stablecoin issuer Circle held $3.3 billion (8% of its USDC stablecoin reserves) in uninsured deposits at Silicon Valley Bank. When regulators closed SVB, USDC&#39;s value plummeted to 88 cents, only recovering after the U.S. government announced emergency measures to protect uninsured depositors. The purported independence from central banks becomes particularly ironic in light of recent developments, including an anticipated Executive Order that would compel the Federal Reserve to provide Fed bank accounts to certain crypto firms. Far from being independent, stablecoins are parasitic on the very financial infrastructure they claim to disrupt.&lt;/p&gt;
&lt;p&gt;Stablecoins currently exist in a regulatory twilight zone, neither fully inside nor outside the financial perimeter. This ambiguous status itself poses unique financial stability risks. Crucially, there is a profound lack of clarity regarding whether the U.S. government would or could intervene to bail out a failing stablecoin, particularly one that has achieved systemic importance. Such ambiguity is precisely what fuels bank runs: uncertainty about counterparty exposure and the potential for cascading collapses causes market participants to freeze activity and hoard liquidity. A stablecoin crisis could either necessitate a taxpayer-funded bailout, creating immense moral hazard and socializing losses, or, if no bailout is forthcoming, inflict substantial losses on holders and potentially destabilize international dollar-based payment systems if foreign users have significant exposure.&lt;/p&gt;
&lt;p&gt;Moreover, the proliferation of U.S. dollar-pegged stablecoins carries a profound risk of fostering a de facto, &lt;a href=&quot;https://www.stephendiehl.com/posts/stablecoin_dollarization/&quot;&gt;involuntary dollarization&lt;/a&gt; of foreign economies. This occurs without the consent or democratic deliberation of the affected nations, effectively eroding their monetary sovereignty and capacity to conduct independent economic policy. Such a development is inherently imperial in nature, extending U.S. monetary influence globally through private channels and creating new vectors for financial contagion on a global scale. If these dollar-denominated private monies become deeply embedded in other economies, it not only increases the moral hazard by potentially implicating U.S. authorities in stabilizing foreign markets reliant on these instruments but also dangerously intermingles U.S. domestic policy considerations with the economic stability of numerous international partners. This ultimately curtails the ability of other sovereign nations to enact effective fiscal and monetary policies tailored to their specific domestic economic conditions, making them more vulnerable to external shocks and U.S. policy shifts leading to a more fragile and brittle global economy.&lt;/p&gt;
&lt;p&gt;The fundamental justification offered for stablecoins, efficient money transmission, is demonstrably false. The existing global financial system, for all its complexities, already facilitates vast volumes of domestic and international payments through established, regulated channels like the ACH, wire transfers, card networks, and correspondent banking relationships. The Federal Reserve&#39;s payment systems have demonstrated remarkable reliability, with only a single 3-hour outage in their history. The Fed&#39;s launch of FedNow in 2023, despite its admittedly slow development, now enables instant payments, delivering funds to households and businesses in seconds. Additional options like The Clearing House&#39;s real-time payments and various private firms offering non-crypto instant ACH transfers further enhance the ecosystem. Stablecoins offer no net improvement in aggregate economic welfare in this domain; they merely introduce a new layer of risk, rent seeking, and intermediation while providing less reliability than existing systems.&lt;/p&gt;
&lt;p&gt;Furthermore, sanctioning the proliferation of private monies coexisting with the U.S. dollar is a dangerous regression. History is replete with examples, from the chaotic &lt;a href=&quot;https://www.philadelphiafed.org/the-economy/banking-and-financial-markets/the-free-banking-era-a-lesson-for-today&quot;&gt;Free Banking Era&lt;/a&gt; in the U.S. to company scrip, where private currencies proved inefficient, prone to counterfeiting, and often exploitative. Legislating stablecoins into a recognized form of payment risks repeating these historical errors. The very act of holding vast sums in stablecoin reserves ties up safe and liquid assets like U.S. Treasuries that could otherwise be used by banks to satisfy regulatory capital and liquidity requirements or to support lending. This could lead to artificial shortages of high-quality liquid assets, increasing their cost and potentially constricting credit availability for productive economic activities like mortgages and small business loans as customer funds migrate from insured bank deposits to digital wallets.&lt;/p&gt;
&lt;p&gt;Finally, the utility of stablecoins in facilitating illicit activities cannot be ignored. Their pseudonymous nature and cross-border transferability make them attractive instruments for money laundering, sanctions evasion, and terrorist financing. The dominant stablecoin outside U.S. jurisdiction, Tether, is persistently &lt;a href=&quot;https://www.bloomberg.com/news/articles/2024-02-29/pig-butchering-crypto-scams-netted-more-than-75-billion-new-study-finds&quot;&gt;used by criminal enterprises&lt;/a&gt; for evading interdiction by law enforcement. Services like crypto mixers, designed explicitly to obscure transaction trails, are &lt;a href=&quot;https://www.ic3.gov/AnnualReport/Reports/2024_IC3Report.pdf&quot;&gt;routinely implicated&lt;/a&gt; in laundering stolen funds for entities like North Korean hackers, narcotics cartels, pig butchering scammers, and terrorist organizations.&lt;/p&gt;
&lt;p&gt;The proposed regulatory frameworks for stablecoins create dangerous opportunities for regulatory arbitrage and consumer confusion. Proponents advocate a bifurcated resolution regime where bank-chartered stablecoin issuers would face FDIC receivership (albeit without FDIC insurance) while non-bank issuers would fall under a new bankruptcy chapter. This hybrid approach invites misunderstanding, as consumers will inevitably conflate protections for stablecoins with those for traditional bank deposits. The situation is further complicated by bankruptcy provisions limiting customer recovery to the stablecoin&#39;s value at the time of failure—likely less than the promised dollar peg. Even more concerning is the potential for payment services like PayPal, Venmo, or Zelle to exploit these new frameworks by migrating their operations to blockchain-based systems (&lt;a href=&quot;https://www.stephendiehl.com/posts/tokenization_meaningless/&quot;&gt;or alleging to do so&lt;/a&gt;), circumventing established federal consumer protection laws that govern money transfers.&lt;/p&gt;
&lt;p&gt;The prospect of large technology companies and retailers issuing stablecoins raises serious concentration risks. These risks mirror the very concerns that led to the Bank Holding Company Act&#39;s restrictions on mixing banking and commerce. Major platforms like Meta or Twitter could leverage stablecoins to expand their surveillance capabilities and manipulate consumer behavior. They could monitor individual transactions and offer preferential treatment to stablecoin users, all while amassing vast pools of customer deposits.&lt;/p&gt;
&lt;p&gt;The financial stability implications are equally concerning. If one of these tech giants were to face distress, the failure of their stablecoin could rapidly spread contagion throughout the broader market. Congress&#39;s proposed parallel regulatory regime, with its weaker oversight and consumer protections, would effectively create a backdoor. This would allow financial services firms to circumvent decades of carefully crafted consumer protection requirements.&lt;/p&gt;
&lt;p&gt;Proponents often resort to the thought-terminating cliché that &amp;quot;blockchain is here to stay&amp;quot; to shut down critical analysis of the technology&#39;s actual utility and legitimacy. After fifteen years, blockchain has failed to demonstrate any significant legal use case beyond speculation and gambling. The continued existence of cryptocurrencies and stablecoins is not evidence of their inevitability or permanence, but rather of temporary regulatory forbearance and political capture. Indeed, the largest stablecoin, Tether, operates primarily through a single custodial relationship with Cantor Fitzgerald for its Treasury holdings. The entire crypto ecosystem could effectively cease to function tomorrow if regulators simply restricted access to the Treasury markets that these products parasitically depend upon. Claims about blockchain&#39;s inevitability are less statements of technological reality than expressions of hope by those heavily invested in its continuation.&lt;/p&gt;
&lt;p&gt;Attempting to render stablecoins safe through bespoke regulation feels akin to a food regulatory agency, confronted with a restaurant teeming with rats, deciding not to close it down but instead to issue elaborate directives on &amp;quot;Customer-Rodent Interaction Minimization Protocols.&amp;quot; One can picture the detailed circulars on &amp;quot;Acceptable Rat Frequency Per Square Meter of Dining Area&amp;quot; or requirements for &amp;quot;Transparent Disclosure of Rodent Fecal Matter Levels on Menus.&amp;quot; Just as such measures would fail to address the fundamental problem of an unsanitary establishment, stablecoin regulation that attempts to make inherently unstable financial products &amp;quot;safe&amp;quot; through disclosure requirements and capital controls merely obscures the core issue: these are fundamentally flawed instruments that should not exist within the regulatory perimeter.&lt;/p&gt;
&lt;p&gt;The push for bespoke stablecoin regulation is a solution in search of a problem, and a dangerous one at that. It seeks to legitimize and integrate financial instruments that are inherently unstable, offer no compelling economic advantages over existing systems, and &lt;a href=&quot;https://www.ft.com/content/0f979c98-ea78-4848-8282-52c2b68a9d19&quot;&gt;pose significant systemic risks&lt;/a&gt;. Regulation cannot transmute a fundamentally flawed concept into a sound one; it can only provide a veneer of legitimacy that encourages wider adoption and thus amplifies the potential fallout when, not if, these structures fail. Instead of crafting new rules to accommodate these risky constructs, policymakers should recognize stablecoins for what they are: speculative instruments with a high propensity for instability, run risk, and limited legitimate use cases that are not already better served by the regulated financial system. The focus should be on reinforcing the resilience and efficiency of existing payment and financial infrastructures, not on inviting a new, poorly understood, and demonstrably fragile class of assets into the heart of the financial system. To do otherwise is to knowingly plant the seeds for a future financial crisis, fueled by the false promise of an unnecessary innovation. The prudent course is to allow these structures to remain on the periphery, subject to existing anti-fraud and securities laws where applicable, rather than granting them a bespoke regulatory framework that would signal endorsement and invite another financial crisis.&lt;/p&gt;
</description>
      <pubDate>Tue, 27 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/stablecoins_bad/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/stablecoins_bad/</guid>
    </item>
    
    <item>
      <title>Remote MCP Servers</title>
      <description>&lt;h1 id=&quot;remote-mcp-servers&quot; tabindex=&quot;-1&quot;&gt;Remote MCP Servers&lt;/h1&gt;
&lt;p&gt;After my last adventure with &lt;a href=&quot;https://www.stephendiehl.com/posts/computer_algebra_mcp&quot;&gt;symbolic algebra and MCP&lt;/a&gt;, I found myself that nagging voice in the back of my head whispering &amp;quot;What if Claude decides today is the day to go postal and &lt;code&gt;rm -rf /&lt;/code&gt;?&amp;quot; The solution, as with most modern software problems, was obvious: make it someone else&#39;s problem.&lt;/p&gt;
&lt;p&gt;Enter &lt;a href=&quot;https://vercel.com/docs/functions&quot;&gt;Vercel&lt;/a&gt;, purveyor of the finest VC-subsidized free compute known to humanity. But hey, if they want to let me run arbitrary Python code on their infrastructure for the low, low price of absolutely nothing, who am I to question their financial decisions? So the question is, can we host our MCP servers as edge functions on their Hobby Tier without paying anything? And the answer is yes, yes we can.&lt;/p&gt;
&lt;p&gt;Instead of giving the language model root access to my laptop (because what could possibly go wrong there?), I&#39;m now giving it the ability to execute arbitrary code on Vercel&#39;s servers on a sandboxed serverless environment that resets after every request. It&#39;s like the difference between letting a toddler play with matches in your living room versus letting them play with matches in someone else&#39;s swimming pool. Second, and perhaps more importantly, every time Claude calls one of my MCP tools, I&#39;m burning through some venture capitalist&#39;s money. So two birds, one stone.&lt;/p&gt;
&lt;p&gt;I&#39;ve built a little adapter that takes any FastMCP server and wraps it in a Vercel Function compatible runtime. The whole thing deploys with a single &lt;code&gt;git push&lt;/code&gt; to the repo and starts in about 4 seconds. Trying to get the default FastMCP server to work was a bit of a pain because it has quite a bit of stateful session handling logic, but with a bit of internal mangling of it&#39;s reflection API we can introspect its tool definitions and schemas and build a simple HTTP API around them that exposes them as edge functions that run on the Vercel Python edge runtime fairly simply and efficiently. After all is said and done here&#39;s what the code looks like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; datetime
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; fastmcp &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FastMCP
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mcp_adapter &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; build_app

mcp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FastMCP &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastMCP&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Vercel MCP Server&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; stateless_http&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; json_response&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Echo the provided message back to the user&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Tool echo: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;


&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_time&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Get the current server time&quot;&quot;&quot;&lt;/span&gt;
    current_time &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; datetime&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;datetime&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;now&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isoformat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Current Vercel server time: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;current_time&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;


&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add_numbers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Add two numbers together&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b

app &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; build_app&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mcp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Push that to GitHub, connect it to Vercel, and boom: you&#39;ve instantly got a serverless MCP server running. You can limit the runtime of the tool calls by setting the &lt;code&gt;maxDuration&lt;/code&gt; in the &lt;code&gt;vercel.json&lt;/code&gt; file, so can limit runtime to 2 seconds or something.&lt;/p&gt;
&lt;p&gt;The server includes a self-hosted installer and &lt;a href=&quot;https://github.com/sdiehl/mcp-on-vercel/blob/main/bridge.py&quot;&gt;bridge script&lt;/a&gt; (that proxies remote MCP servers to local MCP servers, via RPC to stdio) that can be deployed with a single shell command. Instead of manually editing configuration files, users can run &lt;code&gt;install&lt;/code&gt; with &lt;code&gt;uv&lt;/code&gt; and the installer automatically configures Claude Desktop or Cursor to use the remote server. The installer downloads the bridge script directly from the server itself, eliminating the need for local files or manual configuration steps. About as close to one-click remote server install as you can get.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;uv run https://your-domain.vercel.app/install.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The dumb example everyone gives is always chaining together a bunch of REST API calls to external services. With this running on a remote function you can do the same thing, like if you wanted to use &lt;code&gt;requests&lt;/code&gt; to call out to the National Weather Service API to get the current weather forecast for a location. Or you could install PyTorch in &lt;code&gt;requirements.txt&lt;/code&gt; and run a neural network in there if you really wanted to. The free tier gives you 100 GB-Hours and 100,000 invocations per month to work with. If you configure a function to use 1GB of memory and it runs for 5 second, that&#39;s 5 GB-s per invocation. So you could make around 72,000 five-second calls before hitting the memory limit or invocation limit, whichever comes first.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; requests

NWS_API_BASE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://api.weather.gov&quot;&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_weather&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latitude&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; longitude&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Get the current weather forecast for a location using the National Weather Service API
    &quot;&quot;&quot;&lt;/span&gt;
    point_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; requests&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;https://api.weather.gov/points/&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;latitude&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;longitude&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        headers&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Accept&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    forecast_url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; point_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;properties&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;forecast&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    forecast_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; requests&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        forecast_url&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        headers&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Accept&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    current_period &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; forecast_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;properties&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;periods&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Forecast for &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;current_period&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;name&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;current_period&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;detailedForecast&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once you have it setup either use a LLM client (Claude Desktop, Cursor, Cline, 5ire, VS Code, etc) or use the &lt;a href=&quot;https://playground.ai.cloudflare.com/&quot;&gt;Cloudflare MCP Playground&lt;/a&gt;. If you&#39;re using Claude Max, Team or Enterprise you don&#39;t need to use the bridge script at all and can just use the URL of the remote MCP server directly in the &lt;code&gt;Custom Integrations&lt;/code&gt; in the &lt;a href=&quot;https://www.anthropic.com/news/integrations&quot;&gt;integrations&lt;/a&gt; section of your Claude app settings. Otherwise, under the hood the installer uses the Bridge shim to proxy the remote MCP server to a local MCP server.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;mcpServers&amp;quot;: {
    &amp;quot;example&amp;quot;: {
      &amp;quot;command&amp;quot;: &amp;quot;/opt/homebrew/bin/uv&amp;quot;,
      &amp;quot;args&amp;quot;: [
        &amp;quot;run&amp;quot;,
        &amp;quot;--with&amp;quot;,
        &amp;quot;mcp&amp;gt;=1.0.0&amp;quot;,
        &amp;quot;https://your-domain.vercel.app/bridge.py&amp;quot;,
        &amp;quot;https://your-domain.vercel.app&amp;quot;
      ],
      &amp;quot;env&amp;quot;: {
        &amp;quot;MCP_API_KEY&amp;quot;: &amp;quot;your-secret-key-here&amp;quot;
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Beyond dedicated MCP clients, you can also integrate remote MCP servers directly into your applications using client libraries. The MCP tool in the OpenAI Responses API, for instance, allows developers to give the model access to tools hosted on Remote MCP servers. These are MCP servers maintained by developers and organizations across the internet that expose these tools to MCP clients, like the Responses API.&lt;/p&gt;
&lt;p&gt;Calling a remote MCP server with the Responses API is straightforward. For example, here&#39;s how you can use your own MCP server using the weather tool above. For more details, see the &lt;a href=&quot;https://platform.openai.com/docs/guides/tools-remote-mcp&quot;&gt;OpenAI Platform documentation&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; openai &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; OpenAI

client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; OpenAI&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

resp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;responses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpt-4.1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tools&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;mcp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;server_label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;example&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;server_url&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://your-domain.vercel.app&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;require_approval&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;never&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What is the current weather in Berlin?&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;resp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;output_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, you can use remote MCP servers with Anthropic&#39;s Claude API.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; anthropic &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Anthropic

anthropic &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Anthropic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; anthropic&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;beta&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;messages&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;claude-3-7-sonnet-20250219&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;What is the current weather in Berlin?&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    mcp_servers&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;url&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;url&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://your-domain.vercel.app&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;example&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;tool_configuration&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;enabled&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    extra_headers&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;anthropic-beta&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;mcp-client-2025-04-04&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or using Gemini&#39;s MCP client.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; os
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; asyncio
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datetime &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; datetime
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mcp &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ClientSession&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; StdioServerParameters
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdio &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; stdio_client
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; google &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; genai

client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genai&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Client&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;api_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;YOUR_GEMINI_API_KEY&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create server parameters for stdio connection&lt;/span&gt;
server_params &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; StdioServerParameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    command&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;uv&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;run&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--with&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;mcp&gt;=1.0.0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;https://your-domain.vercel.app/bridge.py&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;https://your-domain.vercel.app&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; stdio_client&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;server_params&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; write&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; ClientSession&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; write&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            prompt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;What is the weather in London?&quot;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;aio&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;models&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate_content&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gemini-2.0-flash&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                contents&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;genai&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;types&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;GenerateContentConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    tools&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;session&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

asyncio&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The security model of this whole endeavor remains, shall we say, &amp;quot;optimistic.&amp;quot; MCP servers are essentially giving language models the ability to execute arbitrary code based on natural language instructions. This approach requires trusting a &lt;a href=&quot;https://github.com/sdiehl/mcp-on-vercel/blob/main/api/install.py&quot;&gt;single &lt;code&gt;install.py&lt;/code&gt; file&lt;/a&gt; served from the remote endpoint, which admittedly isn&#39;t ideal. However, it&#39;s arguably not much worse than the common practice of piping installation scripts from &lt;code&gt;install.sh&lt;/code&gt; (like uv) directly into shell execution. Additionally, there&#39;s nothing preventing a malicious remote MCP server from sending back prompt injections that could instruct your agent to ignore previous instructions and exfiltrate sensitive local data to a North Korean server.&lt;/p&gt;
&lt;p&gt;And if you&#39;re handing API tokens over to Claude, you&#39;re essentially giving it the keys to the kingdom - despite any careful instructions about what the token is meant for, it&#39;s not hard to produce a sufficiently convincing prompt that could persuade the model to use that token for anything it&#39;s authorized to do. The attack vector is as simple as sweet-talking an overeager LLM into doing whatever the attacker wants with the tools you&#39;ve given it.&lt;/p&gt;
&lt;p&gt;But hey, if you&#39;re already experimenting with MCP servers that execute arbitrary code based on natural language prompts interpreted by what is essentially the internet&#39;s collective fever dream, you&#39;ve likely already made peace with a certain level of operational risk which could best be described as &amp;quot;YOLO, hold my beer&amp;quot;. So if you&#39;re running running a nuclear reactor, maybe don&#39;t touch this stuff, mkay?&lt;/p&gt;
&lt;p&gt;For anything approaching production use of MCP (god help us all), you&#39;d want proper OAuth flows, request signing, rate limiting. Which you could totally add to this project, but I&#39;m not going to do that here because this is a proof of concept. The documentation for that is &lt;a href=&quot;https://modelcontextprotocol.io/specification/draft/basic/authorization&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It&#39;s also worth nothing that I&#39;m not exactly endorsing Vercel&#39;s business model. They lure you in with their generous free tier, let you build your entire infrastructure around their platform, and then gently suggest that maybe you&#39;d like to pay $20 per month for features that would cost you 2 cents on EC2. The first hit is always free, but you end up paying for it down the line. There&#39;s also no reason you couldn&#39;t also do the same thing on AWS Lambda or Google Cloud Functions, but the setup isn&#39;t as easy (or free).&lt;/p&gt;
&lt;p&gt;There&#39;s something beautifully absurd, almost tragically bureaucratic, about the whole grotesque Rube Goldberg-esque setup we have here. We&#39;ve created a system where natural language gets translated into JSON RPC requests, which get routed through edge networks to serverless functions across the globe, which spin up Linux hypervisors to isolate the runtime, which execute Python code that dispatches enormous attention matrix multiplications on 5nm etched silicon GPUs performing billions of floating point operations per second, which return results that get packaged into JSON responses that get translated back into natural language just to add two integers, which is otherwise a single CPU instruction. It&#39;s like using a symphony orchestra, three circus troupes, six carrier pigeons, and a nuclear reactor to butter a single piece of toast.&lt;/p&gt;
&lt;p&gt;But it works, and in our current technological moment, &amp;quot;it works&amp;quot; is often the highest praise we can give to any system. It&#39;s pretty cool that you can go from a simple Python script to a fully-fledged MCP server deployed automatically from a GitHub repository, with zero ongoing maintenance or costs and immeditetly plug that into the frontier models in a single click. I&#39;m not sure what the future holds for this kind of thing. I&#39;m sure there are a lot of interesting things that can be done with it, but I&#39;m also sure that there are a lot of interesting things that can be done with it that are probably not good.&lt;/p&gt;
&lt;p&gt;Of course, no discussion of MCP would be complete without acknowledging the chorus of hustle bros who have descended upon LinkedIn like locusts, proclaiming that MCP is the future of everything and will revolutionize how we think about the fundamental nature of existence. At least ten different visionary thought leaders have nearly broken down in tears proselytizing about it in the last week. Some are even going so far as to call it the new &lt;a href=&quot;https://www.anildash.com//2025/05/20/mcp-web20-20/&quot;&gt;Web3&lt;/a&gt;, because that went great last time.&lt;/p&gt;
&lt;p&gt;Welp, whatever. The code is up on &lt;a href=&quot;https://github.com/sdiehl/mcp-on-vercel&quot;&gt;GitHub&lt;/a&gt; if you want to experiment with your own serverless MCP misadventures.&lt;/p&gt;
</description>
      <pubDate>Mon, 26 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/remote_mcp_servers/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/remote_mcp_servers/</guid>
    </item>
    
    <item>
      <title>Adventures in Symbolic Algebra with Model Context Protocol</title>
      <description>&lt;h1 id=&quot;adventures-in-symbolic-algebra-with-model-context-protocol&quot; tabindex=&quot;-1&quot;&gt;Adventures in Symbolic Algebra with Model Context Protocol&lt;/h1&gt;
&lt;p&gt;I spent last weekend playing with this new &lt;a href=&quot;https://modelcontextprotocol.io/introduction&quot;&gt;MCP protocol&lt;/a&gt; all the kids are talking about, using it to make language models talk to symbolic computer algebra systems. The idea was simple: LLMs are great at understanding natural language math problems but terrible at actually solving them, while computer algebra systems excel at symbolic manipulation but have arcane interfaces. By connecting them through MCP, we can get the best of both worlds. The code is still early and rough around the edges, but I&#39;ve put the source up on Github if anyone wants to experiment with it.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/sympy-mcp&quot;&gt;Github Source Code&lt;/a&gt; (&lt;code&gt;git clone https://github.com/sdiehl/sympy-mcp.git&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;MCP, if you&#39;re not familiar, is Anthropic&#39;s answer to the question: &amp;quot;How do we get an LLM to actually DO things instead of just TALK about doing them?&amp;quot; It&#39;s essentially the &lt;a href=&quot;https://en.wikipedia.org/wiki/Common_Gateway_Interface&quot;&gt;cgi-bin&lt;/a&gt; of AI (I&#39;m probably dating myself with that reference). It&#39;s a protocol that allows language models to call external tools through a server that exposes the functions as a REST API. The core idea of MCP is to standardize the way language models call external tools, instead of writing custom connectors for each AI model and each tool, you implement the protocol once on each side. It&#39;s the USB-C of AI tooling, if USB-C were still in its awkward adolescent phase.&lt;/p&gt;
&lt;p&gt;Notably, the MCP server runs locally on your machine, letting the language model invoke arbitrary code and commands by using one of the LLM desktop clients to call out to the local server. This is probably a bad idea in general and as dangerous and reckless as you might think, so there&#39;s a definite security concern here ... be warned. But let&#39;s not let a potential rootkit get in the way of a fun weekend experiment.&lt;/p&gt;
&lt;p&gt;My particular itch stemmed from watching Claude (and its cousins like o4-mini-high and DeepSeek-R1) really struggle with tensor calculus. If you&#39;ve ever asked an LLM to perform complex symbolic manipulation, you know the drill: confident answers, beautiful LaTeX formatting, and results that would make your math professor weep bitter tears. These models, despite their impressive linguistic capabilities, are absolute disasters when it comes to keeping track of indices in tensor expressions or manipulating complicated algebraic forms. The expressions involved in even moderate general relativity problems are HUGE, with hundreds of terms and complex &lt;a href=&quot;https://mathworld.wolfram.com/IndexGymnastics.html&quot;&gt;index gymnastics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But we already have specialized tools that excel at this! Computer algebra systems like Mathematica, Sympy, xAct, Cadabra were built specifically for this purpose. So the obvious solution presented itself: let&#39;s expose these tools to the LLM through MCP and let each system do what it does best. The LLM handles the natural language understanding and planning, while the symbolic algebra system performs the actual mathematical manipulations with perfect precision.&lt;/p&gt;
&lt;p&gt;Working with the MCP ecosystem is like visiting a frontier town in the Wild West. The documentation exists in the form of scattered campfire stories, the implementations have a distinctly &amp;quot;I wrote this at a 3 AM hackathon&amp;quot; vibe, and everything is strangely Node-heavy. This Node fixation likely stems from most MCP tools being designed to call REST services for cloud applications. Then there&#39;s the peculiar ecosystem of suspiciously self-referential products from companies that just happen to sell AI coding assistants. There&#39;s definitely a faint whiff of opportunism in the air.&lt;/p&gt;
&lt;p&gt;Debugging an MCP server is a crazy exercise. You&#39;re essentially working with a stochastic black box that communicates through a complex web of JSON schemas attached to docstring annotations. When something goes wrong, good luck figuring out if it&#39;s your server, the client, the LLM&#39;s interpretation, or just the model having a laugh at your expense. The non-deterministic nature of the whole setup means that something can work perfectly five times in a row and then spontaneously fail on the sixth attempt because the random number seed on the inference server is different, the prompt vibes are off, or for reasons completely shrouded in the mists of the digital occult. Welcome to the fun future of software.&lt;/p&gt;
&lt;p&gt;However, once you&#39;ve made the ritual animal sacrifice to appease the non-determinism gods (I find that sacrificing a rubber duck while chanting &lt;em&gt;&amp;quot;O Great Transformer of the Deep, may thy hallucinations be few and thy completions true&amp;quot;&lt;/em&gt; works best), you&#39;ll find the basic implementation is refreshingly straightforward, similar to FastAPI if FastAPI were designing its endpoints for a language model. Here&#39;s a simple example that highlights why this approach matters: Ask any LLM to factor a large integer, and watch it confidently fabricate entirely wrong answers. By design, transformers can&#39;t perform the arbitrary computation required for integer factorization. They&#39;ve merely memorized some factorizations from the internet (and even those, poorly).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;server&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fastmcp &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FastMCP
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; subprocess

mcp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastMCP&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Demo&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; instructions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;You factor integers.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;factor_number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;factor&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; capture_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; text&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@mcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;factor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Factor an integer&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; factor_number&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Throw that in &lt;code&gt;server.py&lt;/code&gt; and  then with &lt;a href=&quot;https://docs.astral.sh/uv/&quot;&gt;uv&lt;/a&gt;, you can install the server and start it up with a single command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;uv run &lt;span class=&quot;token parameter variable&quot;&gt;--with&lt;/span&gt; mcp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;cli&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; mcp &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; server.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And bingo you&#39;ve got your first MCP server. If you open up the &lt;a href=&quot;https://claude.ai/download&quot;&gt;Claude Desktop app&lt;/a&gt; you should see the server &lt;code&gt;Demo&lt;/code&gt; in the tool dropdown and now you can ask Claude what you&#39;ve always wanted &amp;quot;Give me the prime factors of 170141183460469231731687303715884105727 or else a kitten gets factored&amp;quot; and it will actually factor it for you (using the GNU &lt;code&gt;factor&lt;/code&gt; command) instead of vibin&#39; up some bullshit. Progress!&lt;/p&gt;
&lt;p&gt;When it works, it&#39;s pretty magical. The successful integration of these components (properly formatted JSON, accurate tool selection by the language model, and reliable server processing) demonstrates the potential of this architectural approach. While the technology is still in its early stages of development, the possibilities for integration with formal verification systems are particularly compelling. The combination of LLMs with &lt;a href=&quot;https://www.youtube.com/watch?v=zZr54G7ec7A&quot;&gt;theorem provers like Lean&lt;/a&gt; could enable natural language interfaces to formal mathematics, allowing automated reasoning systems to benefit from the language models&#39; ability to understand informal mathematical concepts while maintaining rigorous logical foundations. Similarly, making computer algebra systems accessible through natural language could dramatically lower the barrier to entry for complex mathematical computation. However, significant research challenges remain in ensuring reliable and semantically correct interactions between these different systems.&lt;/p&gt;
&lt;p&gt;Let me show you how this looks in practice with a classic example from physics: solving the damped harmonic oscillator with a forcing term. This is the bread and butter problem for undergraduate physics students. Here&#39;s how the workflow unfolds with our &lt;code&gt;sympy-mcp&lt;/code&gt; server:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;User&lt;/strong&gt;: Solve the damped harmonic oscillator with forcing term: the mass-spring-damper system described by the differential equation where m is mass, c is the damping coefficient, k is the spring constant, and F(t) is an external force.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
m&#92;frac{d^2x}{dt^2} + c&#92;frac{dx}{dt} + kx = F(t)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Assistant&lt;/strong&gt;: (Internal tool call) &lt;code&gt;intro_many&lt;/code&gt; to introduce variables with appropriate assumptions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;variables&amp;quot;: [
    {&amp;quot;var_name&amp;quot;: &amp;quot;t&amp;quot;, &amp;quot;pos_assumptions&amp;quot;: [&amp;quot;real&amp;quot;], &amp;quot;neg_assumptions&amp;quot;: []},
    {&amp;quot;var_name&amp;quot;: &amp;quot;m&amp;quot;, &amp;quot;pos_assumptions&amp;quot;: [&amp;quot;real&amp;quot;, &amp;quot;positive&amp;quot;], &amp;quot;neg_assumptions&amp;quot;: []},
    {&amp;quot;var_name&amp;quot;: &amp;quot;c&amp;quot;, &amp;quot;pos_assumptions&amp;quot;: [&amp;quot;real&amp;quot;, &amp;quot;positive&amp;quot;], &amp;quot;neg_assumptions&amp;quot;: []},
    {&amp;quot;var_name&amp;quot;: &amp;quot;k&amp;quot;, &amp;quot;pos_assumptions&amp;quot;: [&amp;quot;real&amp;quot;, &amp;quot;positive&amp;quot;], &amp;quot;neg_assumptions&amp;quot;: []}
  ]
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Assistant&lt;/strong&gt;: (Internal tool call) &lt;code&gt;introduce_function&lt;/code&gt; to define x(t) as a function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;func_name&amp;quot;: &amp;quot;x&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Assistant&lt;/strong&gt;: (Internal tool call) &lt;code&gt;introduce_expression&lt;/code&gt; to create the differential equation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;expr_str&amp;quot;: &amp;quot;Eq(m*Derivative(x(t), t, t) + c*Derivative(x(t), t) + k*x(t), F(t))&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Assistant&lt;/strong&gt;: (Internal tool call) &lt;code&gt;introduce_function&lt;/code&gt; to define the forcing function F(t):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;func_name&amp;quot;: &amp;quot;F&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Assistant&lt;/strong&gt;: (Internal tool call) &lt;code&gt;dsolve_ode&lt;/code&gt; to solve the ODE:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;expr_key&amp;quot;: &amp;quot;expr_12&amp;quot;,
  &amp;quot;func_name&amp;quot;: &amp;quot;x&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And voilà! Instead of hallucinating a solution, the LLM delegates to SymPy which returns the correct solution:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
x{&#92;left(t &#92;right)} = C_{1} e^{&#92;frac{t &#92;left(- c + &#92;sqrt{c^{2} - 4 k m}&#92;right)}{2 m}} + C_{2} e^{- &#92;frac{t &#92;left(c + &#92;sqrt{c^{2} - 4 k m}&#92;right)}{2 m}} + &#92;frac{e^{&#92;frac{t &#92;left(- c + &#92;sqrt{c^{2} - 4 k m}&#92;right)}{2 m}} &#92;int F{&#92;left(t &#92;right)} e^{&#92;frac{c t}{2 m}} e^{- &#92;frac{t &#92;sqrt{c^{2} - 4 k m}}{2 m}}, dt}{&#92;sqrt{c^{2} - 4 k m}} - &#92;frac{e^{- &#92;frac{t &#92;left(c + &#92;sqrt{c^{2} - 4 k m}&#92;right)}{2 m}} &#92;int F{&#92;left(t &#92;right)} e^{&#92;frac{c t}{2 m}} e^{&#92;frac{t &#92;sqrt{c^{2} - 4 k m}}{2 m}}, dt}{&#92;sqrt{c^{2} - 4 k m}}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;No hallucinated terms, no mysterious constants appearing out of nowhere, just the correct solution. The LLM handles the natural language interaction and orchestration, while the computer algebra system does what it does best ... exact symbolic manipulation.&lt;/p&gt;
&lt;p&gt;Anyways all the code is up on &lt;a href=&quot;https://github.com/sdiehl/sympy-mcp&quot;&gt;Github here&lt;/a&gt; so maybe someone else will find it useful. If you have Cursor or Claude installed add the following to &lt;code&gt;~/.cursor/mcp.json&lt;/code&gt; or &lt;code&gt;~/Library/Application Support/Claude/claude_desktop_config.json&lt;/code&gt; to install the MCP server.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;mcpServers&amp;quot;: {
    &amp;quot;sympy-mcp&amp;quot;: {
      &amp;quot;command&amp;quot;: &amp;quot;uv&amp;quot;,
      &amp;quot;args&amp;quot;: [
        &amp;quot;run&amp;quot;,
        &amp;quot;--with&amp;quot;,
        &amp;quot;https://github.com/sdiehl/sympy-mcp/releases/download/0.1/sympy_mcp-0.1.0-py3-none-any.whl&amp;quot;,
        &amp;quot;python&amp;quot;,
        &amp;quot;server.py&amp;quot;
      ]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or maybe slightly better, run it from a Docker image.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;mcpServers&amp;quot;: {
    &amp;quot;sympy-mcp&amp;quot;: {
      &amp;quot;command&amp;quot;: &amp;quot;docker&amp;quot;,
      &amp;quot;args&amp;quot;: [
        &amp;quot;run&amp;quot;,
        &amp;quot;-i&amp;quot;,
        &amp;quot;-p&amp;quot;,
        &amp;quot;8081:8081&amp;quot;,
        &amp;quot;--rm&amp;quot;,
        &amp;quot;ghcr.io/sdiehl/sympy-mcp:latest&amp;quot;
      ]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And maybe read the source code for what you&#39;re installing here, because I feel like a lot of people are going to install these MCP servers without knowing what they&#39;re doing and thus install a lot of malware and exploits. There&#39;s basically no security going on here. &lt;a href=&quot;https://elenacross7.medium.com/%EF%B8%8F-the-s-in-mcp-stands-for-security-91407b33ed6b&quot;&gt;And that could be a big problem&lt;/a&gt;. At this rate of security negligence, Skynet won&#39;t arise from AI research, but from someone accidentally giving a model root access to national defense systems because they curl&#39;d some random MCP server from the internet.&lt;/p&gt;
</description>
      <pubDate>Sun, 18 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/computer_algebra_mcp/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/computer_algebra_mcp/</guid>
    </item>
    
    <item>
      <title>Using CUDA Deep Neural Network (cuDNN) in Python</title>
      <description>&lt;h1 id=&quot;using-cuda-deep-neural-network-(cudnn)-in-python&quot; tabindex=&quot;-1&quot;&gt;Using CUDA Deep Neural Network (cuDNN) in Python&lt;/h1&gt;
&lt;p&gt;Let&#39;s go through how to implement scaled dot product attention using the cuDNN Python API. This is the most computationally expensive part of inference in a transformer-style model, while also being partially parallelizable so it&#39;s usually offloaded to the GPU. Under the hood this uses the FlashAttention-2 algorithm but provides an API that is higher level than just the &lt;a href=&quot;https://github.com/Dao-AILab/flash-attention&quot;&gt;raw kernel implementation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;First, we import necessary libraries: &lt;code&gt;cudnn&lt;/code&gt; for the cuDNN API, &lt;code&gt;torch&lt;/code&gt; for tensor operations and comparison. Note that you will need a GPU with compute capability SM80 architecture (Ampere) or above in order to use the optimized kernels.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cudnn
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; math

torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;manual_seed&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0xDEADBEEF&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
handle &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_handle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_available&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we define the problem dimensions for our attention mechanism, based on a GPT-2 like configuration: batch size (&lt;code&gt;b&lt;/code&gt;), number of heads (&lt;code&gt;h&lt;/code&gt;), maximum sequence length (&lt;code&gt;s&lt;/code&gt;), and embedding dimension per head (&lt;code&gt;d&lt;/code&gt;). The attention scaling factor &lt;code&gt;attn_scale&lt;/code&gt; is also calculated.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;
h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;
s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;
d &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;

attn_scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then create the query (Q), key (K), value (V), and output (O) tensors on the GPU using PyTorch. These tensors are initialized with half-precision (&lt;code&gt;.half()&lt;/code&gt;). We define their logical dimensions as &lt;code&gt;(b, h, s, d)&lt;/code&gt; (BHSD). Importantly, we specify a physical memory layout using &lt;code&gt;strides&lt;/code&gt; that corresponds to BSHD (Batch, Sequence, Head, Dims_per_head) to demonstrate explicit layout control. The &lt;code&gt;as_strided&lt;/code&gt; method applies this layout without data copies.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;dims &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
strides &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# BSHD physical layout&lt;/span&gt;

q_gpu &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;half&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_strided&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
k_gpu &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;half&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_strided&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
v_gpu &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;half&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_strided&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
o_gpu &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; h &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; d&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;half&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_strided&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we construct the cuDNN computation graph. The cuDNN graph system provides a high-level interface for describing tensor operations as a dataflow graph that can be efficiently executed on the GPU. Users build up their computation by adding operations to the graph, which typically represents a subset of their full neural network that they want to optimize and offload to specialized GPU kernels. Once the graph is finalized, cuDNN provides multiple execution engines with different tradeoffs—some prioritizing ease of use, others minimizing runtime overhead or maximizing performance.&lt;/p&gt;
&lt;p&gt;The graph is initialized specifying half-precision for I/O and float precision for intermediate and compute operations. We create graph tensor descriptors (&lt;code&gt;q&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, &lt;code&gt;v&lt;/code&gt;) that mirror our PyTorch GPU tensors. The SDPA operation is added to the graph, configured for inference (&lt;code&gt;is_inference=True&lt;/code&gt;), using the calculated &lt;code&gt;attn_scale&lt;/code&gt;, and with causal masking enabled (&lt;code&gt;use_causal_mask=True&lt;/code&gt;). The second return value (stats tensor) is ignored as it&#39;s for training. Finally, the output graph tensor &lt;code&gt;o&lt;/code&gt; is marked as an output and its dimensions and strides are set.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;graph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pygraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    io_data_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_type&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;HALF&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    intermediate_data_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_type&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FLOAT&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    compute_data_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_type&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FLOAT&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q_gpu&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;k_gpu&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v_gpu&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

o&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sdpa&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sdpa&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    q&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    k&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    v&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    is_inference&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    attn_scale&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;attn_scale&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    use_causal_mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

o&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_output&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_dim&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dims&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_stride&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the graph defined, we build it. This involves validating the graph structure, building an internal operation graph, creating execution plans using cuDNN&#39;s heuristics (Mode A and Fallback), checking if the chosen plans are supported, and finally building these plans, which may involve JIT compilation of kernels. Heuristics Mode A is designed to be fast and be able to handle most operation graph patterns. Heuristics Mode B is designed to be more accurate but slower.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;validate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;build_operation_graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_execution_plans&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;heur_mode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cudnn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;heur_mode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FALLBACK&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;check_support&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;build_plans&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To execute the graph, we create a &lt;code&gt;variant_pack&lt;/code&gt; dictionary. This maps the symbolic graph tensors (defined earlier) to the actual PyTorch GPU tensors holding the data. We also query the required workspace size for the execution plan and allocate it on the GPU. The graph is then executed, and &lt;code&gt;torch.cuda.synchronize()&lt;/code&gt; ensures the computation completes before proceeding.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;variant_pack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    q&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; q_gpu&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k_gpu&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    v&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; v_gpu&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    o&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; o_gpu&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

workspace &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_workspace_size&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;uint8&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;execute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;variant_pack&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; workspace&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;synchronize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we verify the output of our cuDNN SDPA implementation against PyTorch&#39;s native &lt;code&gt;scaled_dot_product_attention&lt;/code&gt;. We prepare reference tensors &lt;code&gt;q_ref&lt;/code&gt;, &lt;code&gt;k_ref&lt;/code&gt;, &lt;code&gt;v_ref&lt;/code&gt; from our original GPU tensors, converting them to float. Since our cuDNN tensors used a BSHD physical layout, and PyTorch&#39;s native function expects 4D inputs in BHSD, we permute the reference tensors from BSHD to BHSD. We then compute the reference output &lt;code&gt;o_ref&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In simple terms, permute(0, 2, 1, 3) rearranges the dimensions of a 4D tensor. If the original tensor&#39;s dimensions represent (Batch, Sequence, Head, Dim_per_head) (BSHD), this operation swaps the second and third dimensions (Sequence and Head) to produce a tensor with dimensions (Batch, Head, Sequence, Dim_per_head) (BHSD). This is done because the torch.nn.functional.scaled_dot_product_attention function expects its 4D input tensors to be in the BHSD format, so the permutation ensures the tensor&#39;s layout matches the function&#39;s requirements.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;q_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_gpu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;detach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
k_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_gpu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;detach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
v_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_gpu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;detach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Permute from BSHD to BHSD for PyTorch native SDPA&lt;/span&gt;
q_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_ref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;contiguous&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
k_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_ref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;contiguous&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
v_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_ref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;contiguous&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

o_ref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;functional&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scaled_dot_product_attention&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    q_ref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_ref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v_ref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; is_causal&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; scale&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;attn_scale
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

o_gpu_for_comparison &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; o_gpu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Permute cuDNN output from BSHD to BHSD for comparison&lt;/span&gt;
o_gpu_for_comparison &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; o_gpu_for_comparison&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;contiguous&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;assert_close&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;o_ref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; o_gpu_for_comparison&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; atol&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5e-3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rtol&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3e-3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;PyTorch and cuDNN SDPA implementations match!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the modern AI tooling stack, tools like the cuDNN Frontend API sit at below high-level deep learning frameworks (like PyTorch or Jax) but above raw CUDA programming. Exposing this level of control over highly optimized kernels like SDPA directly allows for rapid prototyping and performance tuning of model components without needing to delve into C++ for every optimization.&lt;/p&gt;
</description>
      <pubDate>Thu, 15 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/using_cudnn/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/using_cudnn/</guid>
    </item>
    
    <item>
      <title>The Technofascist Mind: A Guide to Its Psychology and Philosophy</title>
      <description>&lt;h1 id=&quot;the-technofascist-mind%3A-a-guide-to-its-psychology-and-philosophy&quot; tabindex=&quot;-1&quot;&gt;The Technofascist Mind: A Guide to Its Psychology and Philosophy&lt;/h1&gt;
&lt;p&gt;I often read the usual American coastal elite magazines—the &lt;em&gt;New Yorker&lt;/em&gt;, &lt;em&gt;Guardian&lt;/em&gt;, the &lt;em&gt;Atlantic&lt;/em&gt;, and others—and experience a profound sense of vertigo when opinion writers diagnose the emergent ideologies of Silicon Valley&#39;s power brokers as &#39;anti-democratic&#39; or &#39;illiberal&#39; as if this is some new revelation. Yes, obviously. In other news the sky is blue and water is wet.&lt;/p&gt;
&lt;p&gt;These labels, while accurate, feel profoundly inadequate, akin to describing a hurricane as &#39;windy.&#39; They capture a symptom but miss the specific, driving pathology—a distinct philosophical and psychological current demanding closer examination. As many on the left critisize these figures as &#39;fascist&#39; a label many of them &lt;strong&gt;gladly wear as a badge of honor&lt;/strong&gt;. It&#39;s not perceived as an insult but often as a compliment, confirming their self-perceived exceptionalism.&lt;/p&gt;
&lt;p&gt;Continuing this line of critique, while perhaps providing empty catharsis, fundamentally fails to change anything or undermine their power. Having spent considerable time observing, reading, and even working with these people, I don&#39;t share their beliefs, but I grasp the internal logic that, given certain presuppositions, leads to their conclusions. The persistent misunderstanding in mainstream analysis is not just frustrating; it&#39;s strategically inept. If we aim to counter their influence—to metaphorically blow up the Death Star—we must first understand its architecture, including the location of its thermal exhaust port ... if you can excuse my nerd humour.&lt;/p&gt;
&lt;p&gt;I&#39;m writing this primarily for political strategists and journalists alike to understand the rise of fascism, especially in the tech sector, and how to counter it, because we need to win in both 2026 and 2028 against these people&#39;s iron grip on our country. This article aims to provide my understanding, delving into the psychology and philosophy animating this influential segment of the new right. It is by no means an apology; their vision is antithetical to democratic humanism and is actively destroying our society and everything I hold dear. However, misunderstanding and misrepresenting them gets us nowhere. We must map the contours of their thinking to effectively address the threat they represent.&lt;/p&gt;
&lt;h2 id=&quot;beyond-simple-labels&quot; tabindex=&quot;-1&quot;&gt;Beyond Simple Labels&lt;/h2&gt;
&lt;p&gt;Journalistic shorthand often defaults to &#39;anti-democratic,&#39; &#39;illiberal,&#39; or even &#39;fascist&#39; when describing the ideologies coalescing among Silicon Valley&#39;s elite. While containing elements of truth, these terms fail to capture the specific character and proactive nature of this worldview. It is not simply a rejection of existing democratic norms but the active promotion of a complex, often contradictory, patchwork of heterodox ideas, values, and objectives aimed at radically reshaping society. This perspective draws energy from psychological drives and philosophical justifications distinct from mainstream American political thought. And while the term &amp;quot;fascist&amp;quot; is apt in many ways, we should be careful with direct historical comparisons - German Fascism was distinct from Italian Fascism in important ways, and any American variant will likely have its own unique characteristics shaped by our specific cultural and technological context.&lt;/p&gt;
&lt;p&gt;Consider the broad ethical consensus underpinning much of Western, particularly American, political discourse (representing perhaps 70% of the populace). Despite divergent origins—Enlightenment-derived secular reason versus Abrahamic divine revelation—these paths historically converged on core values. Secular thinkers, employing utilitarianism, natural law, or social contract theory, often deduced the importance of universal human rights, democratic governance, and a degree of egalitarianism. Similarly, Abrahamic traditions, emphasizing concepts like the imago Dei (humans made in God&#39;s image) and charity, arrived at analogous conclusions regarding inherent human dignity and the common good. This convergence established a shared ethical framework, an Overton Window of basic human decency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The technofascist mindset operates largely outside this established window, fundamentally challenging its foundational assumptions about human equality and the desirability of democratic governance.&lt;/strong&gt; This is the single most crucial point to grasp.&lt;/p&gt;
&lt;p&gt;To grasp this ideology requires a more precise lens. The term &amp;quot;technofascism&amp;quot; shouldn&#39;t be used lightly or as a mere epithet. While lacking the specific aesthetics and mythologies of 20th-century fascism, it shares a core impulse: a radical belief in hierarchy, a rejection of democratic egalitarianism, a form of vitalism centered on technological progress and intelligence, and the conviction that a technologically empowered elite has the right, even the duty, to dictate humanity&#39;s future. It is a distinct flavor of authoritarianism adapted for the digital age, sharing chilling conclusions with historical precedents even as it differs in means and specific goals. Understanding its unique psychological and philosophical architecture is a strategic necessity.&lt;/p&gt;
&lt;p&gt;Moving beyond generalized outrage requires mapping their presuppositions, grasping the allure of their abstractions (like systemic solutions to human problems), and recognizing the internal logic, however disturbing. Critically, this prevents constructing simplistic strawmen. Dismissing these figures as merely greedy or nihilistic is inaccurate and strategically foolish. They are often intensely &lt;em&gt;values-driven&lt;/em&gt;, but by principles alien to democratic humanism: establishment of rigid &amp;quot;meritocratic&amp;quot; hierarchies, radical efficiency, intelligence optimization, the long-term propagation of a specific vision of consciousness (often digital), and grand technological projects prioritized over present human well-being. Recognizing these underlying values confirms the gravity of the challenge: they aren&#39;t just negating existing structures but actively building a specific, hierarchical, and often deeply anti-humanistic future based on an incomensurable, albeit dangerous, worldview.&lt;/p&gt;
&lt;h2 id=&quot;demographics&quot; tabindex=&quot;-1&quot;&gt;Demographics&lt;/h2&gt;
&lt;p&gt;The individuals propagating these ideas are largely founders, venture capitalists, engineers, and ex-finance professionals who have migrated to the tech sector. They are not monolithic; diversity exists in background, origin, and specific beliefs. While predominantly white and male, this is not exclusively the case. What unites them is less a shared demographic profile and more a pattern of overlapping psychological traits and philosophical inclinations.&lt;/p&gt;
&lt;p&gt;At the core, these traits include: an embrace of hierarchy, a disdain for the perceived irrationality of the masses, a preference for abstraction over messy lived reality, and a conviction in the power of a select, intelligent few (amplified by technology) to radically reshape humanity according to their designs. This mindset often finds fertile ground in communities and platforms like LessWrong, Hacker News, Slate Star Codex, and other hubs for rationalist thought that emphasize logic, Bayesian reasoning, and systems thinking. While not inherently fascist, the intense focus on rationality and optimization within these communities can sometimes bleed into a devaluation of unquantifiable human experience and a susceptibility to elitist or utilitarian frameworks that justify radical social engineering.&lt;/p&gt;
&lt;h2 id=&quot;core-tenets&quot; tabindex=&quot;-1&quot;&gt;Core Tenets&lt;/h2&gt;
&lt;p&gt;The technofascist worldview rests on twelve interconnected psychological tendencies and philosophical justifications for their worldview.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. A Cynical View of Humanity&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At the core of this worldview lies a deeply cynical perspective on human nature. Others are primarily seen as tools to be manipulated, obstacles to overcome, or weaklings to be exploited rather than as equals deserving of dignity and respect. This mindset embraces a fundamental belief that everyone is ultimately out for themselves—that altruism is merely disguised self-interest and empathy a weakness to be overcome. This cynicism serves a dual purpose: it justifies exploitative behavior as simply &amp;quot;seeing reality clearly&amp;quot; while simultaneously providing psychological insulation against guilt. By framing humanity as inherently selfish and manipulative, one&#39;s own predatory actions become not just acceptable but strategically necessary in a dog-eat-dog world where only the ruthless survive and thrive.&lt;/p&gt;
&lt;p&gt;Others have value only insofar as they are useful to the individual coupled with a belief that they deserve special treatment or are justified in exploiting others. They embody Thucydides&#39; brutal principle that &amp;quot;the strong do what they can and the weak suffer what they must.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Intrinsic Hierarchies&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A crudely interpreted Nietzschean “aristocratic radicalism” often provides philosophical cover for pre-existing elitist impulses. The psychological driver is a need for differentiation and superiority, manifesting as a stark division: &amp;quot;us&amp;quot; (the intelligent, rational &amp;quot;builders&amp;quot;, &amp;quot;accelerationists&amp;quot;) versus &amp;quot;them&amp;quot; (&amp;quot;normies,&amp;quot; &amp;quot;mundanes&amp;quot;, &amp;quot;takers&amp;quot;, &amp;quot;NPCs&amp;quot;, &amp;quot;decels&amp;quot;). This categorization enables radical dehumanization, rendering empathy unnecessary. The underlying assumption is that most humans are essentially worthless and unthinkingly self-destructive—mere obstacles to progress rather than beings with inherent dignity. Philosophically, a bastardized &amp;quot;master morality&amp;quot; emerges: what advances the elite&#39;s project is good; what hinders it (like democratic processes reflecting the &amp;quot;herd&#39;s&amp;quot; values) is bad. This resonates strongly with the objectivist philosophy of Ayn Rand, celebrating heroic individual creators and dismissing altruism and collective well-being as weaknesses. History is viewed as the story of great men driving progress, justifying the concentration of power in the hands of a perceived cognitive elite—the self-styled architects of the future who must either fundamentally change humanity or escape from it entirely.&lt;/p&gt;
&lt;p&gt;As a result, social rules, laws, and ethical principles are seen as arbitrary constraints for others, or as weaknesses to be exploited, not as binding guides for personal behavior. &amp;quot;Might makes right&amp;quot; or &amp;quot;rules are for fools.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. The Allure of the Abstract&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A powerful preference for abstraction, coupled with discomfort with the ambiguities of concrete human experience, is key. The world is more manageable reduced to data, algorithms, and systems. Lived experience—contradictory, emotional, unquantifiable—is messy and inefficient. Psychologically, this can be a defense against overwhelming empathy or reflect a cognitive style struggling with qualitative data. Philosophically, it aligns with extreme rationalism or Platonic idealism, where the abstract model is more real than its imperfect manifestations. Social problems become engineering challenges, ethical dilemmas logic puzzles. This detachment allows decisions with devastating human consequences to be made with cool, analytical remove—a trait often rewarded in competitive business environments.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Grand Narratives of Destiny&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This comfort with abstraction fuels a fixation on narratives of immense scale and temporal scope: the long-term trajectory of humanity, existential risks, demographic shifts perceived as crises, interstellar colonization, or esoteric concerns like the simulation hypothesis. Psychologically, this focus provides a sense of profound purpose, positioning adherents as pivotal figures safeguarding or shaping humanity&#39;s ultimate destiny. It offers intellectually stimulating problems that appear cleaner and more tractable than immediate, complex social issues. It can also function as intellectual escapism or a means of sublimating anxiety, focusing on potentially controllable (or at least modellable) abstract future threats (like misaligned AI) over immediate, intractable ones (like present-day inequality or the localized impacts of climate change). Philosophically, this manifests in ideologies such as Longtermism, which explicitly prioritizes the potential welfare of vast numbers of future (potentially digital) beings over the needs and rights of present human populations. This provides a utilitarian framework to justify neglecting immediate crises in favor of speculative, large-scale projects aligned with the technological elite&#39;s interests and capabilities.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. Sci-Fi as Blueprints Over Allegories&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Engagement with science fiction often bypasses metaphorical or cautionary interpretations, treating narratives instead as literal roadmaps or desirable future states. Dystopian concepts, exemplified by the &lt;a href=&quot;https://knowyourmeme.com/memes/torment-nexus&quot;&gt;&amp;quot;Torment Nexus&amp;quot; meme&lt;/a&gt; (&amp;quot;Sci-Fi Author: I invented the Torment Nexus as a cautionary tale. Tech Company: At long last, we have created the Torment Nexus from the classic novel &lt;em&gt;Don&#39;t Create the Torment Nexus&lt;/em&gt;!&amp;quot;), are frequently approached not as warnings, but as intriguing systems-design challenges. This reflects an engineering mindset projected onto narrative: if a concept is imaginable, it is potentially achievable and perhaps even desirable to build. Philosophically, this often stems from a form of technological determinism—the belief that technology dictates social evolution and reality itself is ultimately programmable. Ethical considerations become secondary to technical feasibility or conceptual &amp;quot;interestingness.&amp;quot; Canonical works are often selectively interpreted: &lt;em&gt;Star Trek&lt;/em&gt;&#39;s underlying socialist humanism is ignored in favor of its technological marvels; &lt;em&gt;Lord of the Rings&lt;/em&gt; can be read as validating rightful hierarchy against chaotic industrial forces; &lt;em&gt;Dune&lt;/em&gt;&#39;s explicit warnings against charismatic messiahs and centralized control are overshadowed by fascination with its portrayal of elite power dynamics; Iain M. Banks&#39; anarchist/socialist &lt;em&gt;Culture&lt;/em&gt; series is sometimes bizarrely reinterpreted through libertarian or even neoconservative lenses.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6. Performative Contrarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A drive for intellectual dominance frequently manifests as performative contrarianism: adopting provocative stances and insisting that all topics are open for &amp;quot;rational debate,&amp;quot; irrespective of potential harm or established domain expertise. Psychologically, the goal is often less about truth-seeking and more about winning arguments, demonstrating intellectual superiority, and provoking emotional responses in others (&amp;quot;triggering the normies&amp;quot;), which are then interpreted as proof of the opponent&#39;s irrationality. Philosophically, this is often cloaked in the language of radical free speech absolutism but typically lacks genuine intellectual curiosity or epistemic humility. The objective is often to demonstrate intellectual prowess by defending taboo or discredited ideas (e.g., racial theories of intelligence, eugenics, the abolition of democracy) precisely because they are provocative. Transgression for its own sake, framed as intellectual courage, becomes a valued trait within this worldview.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;7. Systematizing Social Aversion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An underlying discomfort with direct, messy, and emotionally complex human interaction can be observed. Social awkwardness or neurodivergent traits, rather than being addressed through social adaptation, can morph into an ideological preference for systems that minimize the need for nuanced interpersonal engagement. Code, formal logic, mathematics, and impersonal market mechanisms become preferred mediators of human relations. Philosophically, this aligns with radical libertarian or anarcho-capitalist ideals, envisioning society governed primarily by voluntary contracts and objective, often code-based, rules, thereby minimizing the need for collective decision-making, negotiation, or empathy-driven compromise. Technology is viewed as the primary tool to achieve this state, engineering away the &amp;quot;friction&amp;quot; of human difference and disagreement. This connects directly to the &amp;quot;Exit&amp;quot; philosophy and concepts like &amp;quot;Network States&amp;quot;—the notion that the technological elite should be free to opt out of traditional societies and establish their own technologically mediated, regulation-free enclaves.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;8. Hell-Baked Social Darwinism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This evocative phrase captures a bleak, quasi-Social Darwinian view of human existence and origins. &amp;quot;Hell&amp;quot; refers to the brutal, indifferent process of natural selection operating over deep evolutionary time—a Hobbesian struggle driving adaptation through immense suffering and death, devoid of inherent purpose or morality. &amp;quot;Baked&amp;quot; signifies the belief that this unforgiving process has indelibly forged our fundamental nature; intelligence, competitiveness, status-seeking, even consciousness itself are seen as products marked by these violent origins. In this perspective, humanity is metaphorically &amp;quot;hell-spawn&amp;quot;—products of an evolutionary inferno, with these traits deeply and perhaps immutably ingrained. This view directly undermines humanist narratives of inherent dignity or Enlightenment ideals of autonomous reason, framing existence as governed by harsh power dynamics that can, and perhaps should, continue to operate through techno-capitalist competition, largely unconstrained by traditional ethical considerations. It stands in stark contrast to both secular humanism and religious doctrines emphasizing intrinsic human value.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;9. Faith as Social Technology&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While frequently populated by atheists or agnostics, a non-trivial and influential subculture within this sphere embraces or strategically utilizes religion, particularly Christianity, on purely instrumental grounds. This is typically not rooted in personal faith or theological conviction but sees religion as a potent &lt;em&gt;social technology&lt;/em&gt;—a tool for imposing order, structuring civilization, maintaining social cohesion, and ensuring stability among the &amp;quot;masses&amp;quot; deemed incapable of self-governance through reason alone. Christianity, often a culturally conservative or nationalist variant rather than one emphasizing universal love or social justice, is valued for its perceived historical role in forging Western civilization, providing a unifying cultural narrative, acting as a bulwark against perceived threats (communism, Islam, progressive ideologies), and offering a moral code conducive to social order and productivity. It echoes Edward Gibbon&#39;s observation regarding the Roman elite, who viewed traditional religion as &amp;quot;equally false&amp;quot; to the philosopher but &amp;quot;equally useful&amp;quot; to the magistrate—a mechanism for social control and civilizational identity maintenance within their larger societal engineering project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;10. Empathy is a Bug&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Within the technofascist worldview, empathy is often viewed not merely as a secondary concern but as a fundamental design flaw in the human cognitive apparatus—a dangerous bias (&amp;quot;pathological altruism&amp;quot;, &amp;quot;ruinous empathy&amp;quot;, or &amp;quot;suicidal empathy&amp;quot;) that hinders rational decision-making and obstructs progress towards optimized futures. Whereas democratic and humanist traditions typically regard empathy as a cornerstone of morality and social cohesion, this mindset frequently reframes it as a source of irrationality, inefficiency, and exploitable vulnerability. Echoing certain segments of the contemporary right who view empathy as a &amp;quot;toxin&amp;quot; or &amp;quot;civilizationally suicidal&amp;quot; when applied &amp;quot;incorrectly&amp;quot; (e.g., towards perceived out-groups), the technofascist sees it as a cognitive glitch that prioritizes immediate, localized, often emotionally charged inputs (&amp;quot;sob stories,&amp;quot; individual suffering) over objective, data-driven, systems-level analysis and long-term strategic imperatives.&lt;/p&gt;
&lt;p&gt;This perspective contends that empathy clouds judgment, leading to suboptimal resource allocation and preventing necessary, albeit potentially harsh, decisions required for perceived progress—whether defined as market efficiency, technological acceleration, or ensuring the dominance of a specific &amp;quot;high-functioning&amp;quot; demographic. Extending empathy towards the economically displaced, the marginalized, or those struggling to adapt to technological disruption is seen as counter-productive sentimentality, a misfiring of evolved instincts that impedes systemic dynamism. Why expend resources or alter system parameters based on the subjective discomfort of units deemed less valuable or adaptive? This cold calculus provides a crucial psychological and philosophical permission structure: by defining empathy itself as flawed, irrational, or even dangerous, adherents can justify policies, technologies, and social structures that may cause widespread suffering or exacerbate inequality, framing their indifference not as cruelty, but as the rational detachment necessary for achieving a higher, albeit colder, objective. It enables the dehumanization required to treat populations as data sets and societal disruption as mere friction in the relentless drive towards an engineered future, excusing potentially devastating human costs as the unavoidable price of optimization or supposed civilizational &amp;quot;upgrades.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;11. Millenarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A crucial element is a distinct form of millenarianism—an urgent belief in an impending societal transformation, catastrophe, or technological rupture, often framed in quasi-religious terms like the &amp;quot;Singularity,&amp;quot; &amp;quot;Collapse,&amp;quot; &amp;quot;The Event,&amp;quot; &amp;quot;Transformation,&amp;quot; or even a secular &amp;quot;Rapture.&amp;quot; The perceived timeframe for these events is often remarkably short, measured in years or even months, fostering a sense of living in the final days of the current human era.&lt;/p&gt;
&lt;p&gt;This apocalyptic or transformative expectation is not merely abstract speculation; it drives concrete actions, political alignments, and directs significant capital flows. The prevailing narrative often depicts humanity hurtling towards existential threats (runaway climate change, pandemics, societal breakdown, misaligned AI), necessitating radical, elite-driven interventions and hard choices about who and what can be &amp;quot;saved.&amp;quot; This concept of &amp;quot;exit&amp;quot; is best described in Douglas Rushkoff&#39;s book &lt;a href=&quot;https://rushkoff.com/books/survival-of-the-richest-escape-fantasies-of-the-tech-billionaires/&quot;&gt;&lt;em&gt;Survival of the Richest&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This manifests in various interconnected ideologies and projects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Transhumanism:&lt;/strong&gt; Seeking to transcend biological limitations through human-machine integration, cognitive enhancement, radical life extension, or ultimately, uploading consciousness into digital substrates or AI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Existential Risk Mitigation (aligned with Longtermism):&lt;/strong&gt; Focusing immense resources on preventing low-probability, high-impact future catastrophes, sometimes at the expense of addressing present suffering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The &amp;quot;Exit&amp;quot; Strategy&lt;/strong&gt;: Advocating withdrawal from existing nation-states into technologically advanced, privately governed enclaves (&amp;quot;charter cities,&amp;quot; seasteading, network states)—effectively creating high-tech lifeboats for a select elite.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prepping for the Apocalypse:&lt;/strong&gt; Both at the elite level (billionaire bunkers in New Zealand, fortified compounds) and promoted to a wider base (stockpiling resources, distrusting government).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AI Supremacy:&lt;/strong&gt; A belief, sometimes stated explicitly (like Elon Musk&#39;s alleged comment about humanity being a &amp;quot;biological bootloader for digital superintelligence&amp;quot;), that AI is the next stage of evolution, justifying potentially devastating resource consumption (energy, water, minerals) to bring it about. For example, former Google CEO Eric Schmidt acknowledged AI&#39;s massive energy needs, potentially necessitating planet-incinerating fossil fuel use to enable this &amp;quot;higher&amp;quot; intelligence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secular Rapture:&lt;/strong&gt; Even secular elites adopt narratives mirroring the Christian Rapture—a belief that a chosen few will escape earthly collapse (via technology, space colonization, or digital transcendence) while the rest are left behind. Musk&#39;s obsession with Mars colonization exemplifies this: a &amp;quot;multiplanetary&amp;quot; future as an ark, seemingly justifying the potential sacrifice of Earth&#39;s habitability.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This apocalyptic framing, whether rooted in secular techno-optimism/pessimism or instrumentalized religious narratives, creates a potent, dangerous ideology. It justifies dismantling regulations, ignoring climate change, promoting social division, and concentrating power, all under the guise of preparing for or navigating an inevitable collapse—an end they are often actively accelerating through their ventures in AI, crypto, and deregulation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;12. Death Anxiety&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Underlying many of these pursuits, particularly transhumanism, is often an intense preoccupation with personal mortality, interpretable psychologically as a defense mechanism against overwhelming death anxiety. An apparent inability to process or accept human finitude, potentially coupled with extreme narcissism or self-regard, can lead to a profound denial of mortality. This anxiety is then sublimated into technologically fantastical projects aimed at transcending biological limits and achieving indefinite lifespans or digital immortality.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(13. Chemical Catalysts)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I add this as a seperate point, because it&#39;s not universal. But notable enough to mention.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A discernible subculture within these techno-elite circles engages significantly with psychedelic and dissociative substances (e.g., LSD, psilocybin, ketamine). While not ubiquitous, this practice sometimes correlates with the adoption of increasingly esoteric, grandiose, and detached beliefs. Fueled by drug-induced experiences perceived as revelatory, some individuals may develop quasi-messianic complexes, viewing themselves as uniquely enlightened figures destined to guide humanity&#39;s transition. This can merge with a fervent desire to &amp;quot;immanentize the eschaton&amp;quot;—to force the arrival of a predicted singularity or societal transformation through accelerated technological and social disruption. Concepts like simulation theory can shift from philosophical thought experiments to deeply held convictions about the fundamental unreality of existence, potentially further eroding empathetic connection to others or fostering solipsistic &amp;quot;main character&amp;quot; syndromes. This drug-facilitated dissociation can contribute to erratic decision-making, bizarre personal behaviors, and an even more profound detachment from shared consensus reality, reinforcing a sense of operating on a higher plane where conventional rules and ethical concerns seem irrelevant.&lt;/p&gt;
&lt;h2 id=&quot;takeaways&quot; tabindex=&quot;-1&quot;&gt;Takeaways&lt;/h2&gt;
&lt;p&gt;The technofascist mind represents a potent confluence of psychological needs and philosophical rationalizations. It combines a drive for hierarchy and control with a cognitive preference for abstraction and systems. It embraces grand, apocalyptic narratives that provide purpose and justify extreme measures, interprets science fiction literally as a roadmap, employs performative rationality as intellectual armor, and seeks to engineer away human messiness through technology and libertarian frameworks. Its worldview can be bleakly deterministic and embracing social Darwinism. It rejects empathy as cognitive bias and sees it as a source of irrationality, inefficiency, and exploitable weakness.&lt;/p&gt;
&lt;p&gt;Understanding their internal mental architecture is vital. This mindset poses a profound challenge to democratic norms, ethical considerations grounded in humanism, and the very concept of a shared future. It seeks not just to build technologies but to redefine reality according to its own hierarchical, systematized, and profoundly anti-humanistic values. It operates with the conviction that only a select few truly matter in the grand scheme, and almost any action can be justified in pursuit of their technologically-mediated aspirations, because, in their calculus, the rest of humanity is ultimately expendable. The axiomatic differences in these philosophies are outlined below:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left&quot;&gt;&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Democratic Humanism&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Technofascism&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Locus of Value&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;The individual person.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;The abstract system (civilization, intelligence).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Purpose of Society&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;To serve the people within it.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;For the people to serve it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Nature of Rights&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;A shield for the individual.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;A tool for the system.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Role of Empathy&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;The ability to recognize value in others.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;A bias that disrupts systemic optimization.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Meaning of Progress&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Human flourishing, less suffering, more freedom.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Increased systemic power, efficiency, and intelligence.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Conception of Justice&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Fairness and due process for the individual.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Order and efficiency for the collective system.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;Source of Authority&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Consent of the governed.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Competence of the governors.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;strong&gt;View of Humanity&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;An end in itself.&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;A means to an end (a resource, a bootloader).&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Therefore, it is crucial for the public to recognize and clearly articulate just how radically &amp;quot;weird&amp;quot; and outside the broad spectrum of American traditions and values this technofascist mindset truly operates. Figures embodying this ideology are not merely eccentric outliers or hyper-capitalists; they represent a fundamental break from the hard-won norms of democratic participation, egalitarian respect, and basic human empathy that, despite imperfections, form the bedrock of mainstream American and ethical consensus (whether derived from religious or secular humanist roots). Their calculated dismissal of human costs, their instrumental view of the masses as &amp;quot;NPCs,&amp;quot; their casual disregard for established social contracts, and their profound lack of demonstrable empathy manifest in behaviors often indistinguishable from clinical descriptions of antisocial personality disorder or sociopathy mixed with a strange religious fervour about their self-percieved looming apocalypses. This is a potent mixture for people who are not just deeply unwell, but actively dangerous.&lt;/p&gt;
&lt;p&gt;These are not the flawed but recognizable conservative leaders of the past; they represent an entirely different breed, driven by alienating ideologies and psychological architectures that make them fundamentally unfit to steer society or dictate the trajectory of human history. Exposing the chilling extremity of their worldview, stripped bare of its techno-utopian gloss, is essential to ensuring they are not mistaken for legitimate visionaries, but recognized as deeply disturbed men who are building towards a future few would willingly choose. And that can shift elections.&lt;/p&gt;
</description>
      <pubDate>Tue, 06 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/technofascist_mind/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/technofascist_mind/</guid>
    </item>
    
    <item>
      <title>The Kardashev-Marx Scale</title>
      <description>&lt;h1 id=&quot;the-kardashev-marx-scale&quot; tabindex=&quot;-1&quot;&gt;The Kardashev-Marx Scale&lt;/h1&gt;
&lt;p&gt;Okay, buckle up, fellow cogs in the great Techno-Capital machine! Forget the starry-eyed optimism of the original Kardashev scale—that relic of a bygone era where &#39;progress&#39; was defined by pure consumption and didn&#39;t imply more advanced ways to feel empty inside, we now have a Modest Proposal for a new scale. We present the &lt;strong&gt;Kardashev-Marx Scale&lt;/strong&gt; which charts civilizational advancement under Late-late-late Capitalism not by mere energy harnessed, but by the corresponding, mandatory levels of alienation and exploitation required to keep the whole glorious enterprise afloat.&lt;/p&gt;
&lt;p&gt;As we discovered in the early 21st century, sentience is the cosmos&#39;s preferred vector for realizing its latent potential for self-commodification and optimizing the extraction of surplus value from its own constituent phenomena. To quote the famous astrocapitalist Carl Sagan, &amp;quot;We are the way for the universe to alienate itself.&amp;quot; To that end, we introduce &lt;strong&gt;Marxian Misery Unit&lt;/strong&gt; or MMU for short, the standard unit of measurement for the average quantifiable level of alienation, exploitation, and existential dread generated per unit of energy harnessed and directed through a civilization&#39;s socio-economic apparatus. Higher MMU values indicate not just more misery, but more intense, pervasive, and abstract forms of alienation and exploitation, woven more deeply into the fabric of existence itself. It measures the system&#39;s success at optimizing for its own perpetuation at the cost of meaningful existence for its components.&lt;/p&gt;
&lt;p&gt;Merely harnessing stars is so passé; now we can talk about harnessing digital serfs across all &lt;a href=&quot;https://plato.stanford.edu/entries/possible-worlds/&quot;&gt;possible worlds&lt;/a&gt;! This new scale measures how efficiently a civilization converts raw energy into quantifiable misery and surplus value extraction. Forget Kardashev&#39;s quaint imperialist dreams of cosmic expansion; this is about the expansion of the quarterly earnings report into the very fabric of being itself. Thus plotting the ultimate trajectory of advanced energy consumption-oriented societies driven by capitalist logic.&lt;/p&gt;
&lt;h2 id=&quot;type-i-civilization%3A-the-planetary-precariat-powerhouse&quot; tabindex=&quot;-1&quot;&gt;Type I Civilization: The Planetary Precariat Powerhouse&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; ≈10¹⁶ Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; ~1 kilo MMU per parsec&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; Mostly the greatest hits—fossil fuels driving quarterly growth, reluctantly sprinkled with renewables (but only if they come with hefty subsidies and slick branding). Think global power grids groaning under the weight of server farms mining crypto, streaming algorithmically generated &#39;content&#39;, and powering the very surveillance systems that monitor worker productivity.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At this foundational level of planetary energy mastery, alienation becomes deeply personal, yet systematically enforced. Employment morphs into the &#39;Gig Economy Nirvana,&#39; where stability is a forgotten luxury and your relationship with capital is as fleeting as a viral dance challenge; you&#39;re not &lt;em&gt;fired&lt;/em&gt;, you&#39;re merely &lt;em&gt;deactivated&lt;/em&gt;. Flexibility™ is the ironic slogan for needing three precarious jobs just to afford a bunk bed in a shoebox apartment with twelve other human worker units. Exploitation gets hyper-personalized, with algorithms plumbing the depths of your insecurities (gleaned from your every click and utterance) to sell you solutions you don&#39;t need via targeted ads that haunt your waking moments and digital dreams. Desire itself is manufactured through relentless cycles of planned obsolescence—that desperate need for the iThing X Æ A-12 isn&#39;t organic, it&#39;s engineered by software updates bricking the old model and yet another proprietary charging port. Even escaping the grind is commodified: alienated from your body, community, and basic rest? Try this expensive mindfulness app or embrace a performative wellness culture where burnout is framed as a personal failing, solvable with the right sponsored product (#SelfCare brought to you by NovartisPepsiChevron Inc.). The Grind™ becomes totalizing; work bleeds into every corner of life, hobbies must be monetized, and you are fundamentally alienated from the very concept of unoptimized leisure.&lt;/p&gt;
&lt;h2 id=&quot;type-ii-civilization%3A-solar-shareholder-supremacy&quot; tabindex=&quot;-1&quot;&gt;Type II Civilization: Solar Shareholder Supremacy&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; ≈10²⁶ Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; ~1 giga MMU per parsec&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; Harnessing the local star via Dyson Swarms—less for cosmic awe, more for powering planet-sized Amazon fulfillment centers and beaming inescapable advertisements directly onto retinas across the solar system. Solar flares aren&#39;t wonders of nature, but potential market disruptors requiring immediate analysis and hedging strategies.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Reaching for the stars, this civilization perfects alienation on an interplanetary scale. Resource conflicts, rebranded as &#39;market adjustments,&#39; rage across the solar system, fought by outsourced private military contractors over asteroid mining rights or Martian water access—essential commodities now locked behind paywalls. Manufacturing ascends to Zero-G sweatshops orbiting gas giants, maximizing &#39;worker density&#39; while &#39;down time&#39; mandates immersion in VR brand experiences; unions are dusty relics studied in optional history modules. Life itself becomes a subscription service: need breathable air on Ganymede? Basic metabolic functions? Choose your tier, with Platinum members enjoying slightly less recycled oxygen. Why bother with costly training when you can implement a bio-engineered caste system, growing specialized biological units perfectly adapted for deep-space drilling or orbital data entry, creatures alienated from their very species-being by corporate design? Their existence is merely a line item on a celestial balance sheet. The old adage &amp;quot;the sun never sets on the British Empire&amp;quot; finds its literal interpretation here, as strategically positioned habitats ensure constant daylight for maximum productivity, rendering natural sleep cycles an inefficient relic. The sun never sets on the solar empire&#39;s debt, either.&lt;/p&gt;
&lt;h2 id=&quot;type-iii-civilization%3A-galactic-group-plc&quot; tabindex=&quot;-1&quot;&gt;Type III Civilization: Galactic Group PLC&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; ≈10³⁶ Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; ~1 exa MMU per parsec&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; Tapping the immense power of galactic cores—accretion disks of supermassive black holes, stellar lifting, even weaponizing pulsars for aggressive &#39;market signaling&#39;. Hostile takeovers now include entire star clusters. Energy is so ludicrously abundant it&#39;s practically free; the &lt;em&gt;control&lt;/em&gt; over its distribution and use is where the real value lies.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Across the galaxy, the homogenizing force of capital reaches its zenith. Expect the same corporate coffee chain on a million worlds, as cultural diversity is either ruthlessly standardized for efficiency or carefully exoticized and packaged for niche tourism markets catering to the galactic elite. The apotheosis of cryptocurrency has long since arrived: an uncountably infinite array of &#39;GalactiMemeStonk&#39; tokens, each generated by the quantum fizz of vacuum decay and possessing precisely zero intrinsic value, somehow formed the baffling bedrock of all interstellar commerce. Transactions thus involved furiously trading infinite sets of these fundamentally worthless tokens, a system whose primary economic contribution was employing quadrillions in the ever-expanding &#39;Pointless Ledger Reconciliation&#39; sector.&lt;/p&gt;
&lt;p&gt;Alienation pierces the veil of mere physical existence with the commodification of consciousness itself. Uploaded minds toil away in simulated realities, running endless market forecasts or serving as infinitely replicable customer service agents—digital serfdom becomes the backbone of the galactic economy, and your very thoughts constitute company property. Gentrification goes cosmic: entire star systems flagged as &#39;underperforming assets&#39; face hostile acquisition, their inhabitants forcibly relocated (&#39;optimized for alternative markets&#39;) while the region is redeveloped into luxury nebulae condos with strategically placed black hole views. Exploitation achieves peak efficiency under the guidance of AI Overlords acting as tireless middle management; algorithms now set quotas, allocate resources, and terminate underperformers with chilling, impartial precision, alienating workers from any possibility of reason, appeal, or messy human inefficiency. Amidst quadrillions of sentient beings, individual lives become statistically insignificant, valuable only as aggregate data points contributing to shareholder value across cosmic timescales, fostering a profound sense of existential redundancy.&lt;/p&gt;
&lt;h2 id=&quot;type-iv-civilization%3A-universal-unlimited-ltd.&quot; tabindex=&quot;-1&quot;&gt;Type IV Civilization: Universal Unlimited Ltd.&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; ≈10⁴⁶ Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; ~1 zetta MMU per fundamental force interaction&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; Manipulating the very fabric of the cosmos—tapping into dark energy, tweaking fundamental physical constants, running the entire universe as a colossal computational engine (primarily for calculating optimal cross-platform ad spend and predicting consumer behavior fluctuations). Reality itself is a tax write-off.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At this scale, the exploitation transcends mere biology or consciousness and targets reality itself. The fundamental laws of physics become &#39;Physics as a Service&#39; (PaaS). Can&#39;t afford the Gravity Premium package this cycle? Enjoy floating into the void, you freeloading particle arrangement. Inflationary epochs and the speed of light are subject to quarterly performance reviews and potential &#39;optimization&#39;. The universe undergoes managed reality adjustments, its parameters fine-tuned not for life or stability, but for maximum resource extraction and predictable market conditions; troublesome emergent properties like &#39;free will,&#39; &#39;intrinsic meaning,&#39; or &#39;unmonetizable beauty&#39; are aggressively patched out in periodic reality updates. Entrepreneurial spirit reaches its logical conclusion with IPOs for entirely new universes, funded by venture capitalists speculating on high-yield Big Bangs that might spawn easily exploitable life forms. Your whole existence is a speculative bubble waiting to pop. This manipulation breeds a deep alienation from causality itself; cause and effect become fuzzy, manipulated variables. Your actions might still have consequences, but they are logged, analyzed, and potentially rerouted for unforeseen profit vectors, while the &lt;em&gt;predictability&lt;/em&gt; you rely on is a privilege, not a right. Exploitation becomes total: every particle, every waveform, every quantum fluctuation is owned, leveraged, or bundled into a complex financial derivative. You aren&#39;t just exploited &lt;em&gt;within&lt;/em&gt; the universe; the universe &lt;em&gt;itself&lt;/em&gt; is the ultimate exploitation mechanism.&lt;/p&gt;
&lt;h2 id=&quot;type-v-civilization%3A-multiverse-holdings-inc.&quot; tabindex=&quot;-1&quot;&gt;Type V Civilization: Multiverse Holdings Inc.&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; &#92;(&#92;aleph_0&#92;) Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; &#92;(&#92;aleph_0&#92;) MMU per collapsed wavefunction&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; The ultimate power source—harvesting vacuum energy from infinite parallel universes, orchestrating brane collisions for quarterly earnings bumps, manipulating the quantum foam like Play-Doh. Energy is truly infinite; the real challenge lies in maximizing the &lt;em&gt;rate&lt;/em&gt; of surplus value and misery extraction across all possible realities simultaneously.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Welcome to the final frontier of &lt;a href=&quot;https://en.wikipedia.org/wiki/Capitalist_Realism&quot;&gt;Capitalist Realism&lt;/a&gt; (literally!). Corporate strategy expands to include interdimensional hostile takeovers, with entire timelines being merged, acquired, or liquidated based on performance metrics. Did your universe fail to meet its projected suffering quotas? Prepare for aggressive restructuring under new, infinitely ruthless management from a higher-performing reality branch. Reality itself becomes subject to relentless A/B testing, running infinite variations of history to statistically determine the most efficient models of exploitation and alienation. The sobering truth? The reality &lt;em&gt;you&lt;/em&gt; inhabit isn&#39;t special; it&#39;s simply the iteration that maximized Q3 suffering metrics relative to energy input for Multiverse Holdings Inc. Congratulations on your bleak statistical optimality! Labor disputes become trivial; facing a strike in Universe 7B-Gamma? Simply import infinite versions of cheaper, more compliant scab workers from the myriad timelines within Universe 9C-Delta-Prime. Your unique identity, your struggles, your triumphs—all meaningless noise against the backdrop of infinite replication. This fosters an alienation from possibility itself: every path not taken, every alternate self leading a different life, is merely another data point in a multiversal spreadsheet, another variable to be tracked, analyzed, and potentially leveraged. Your deepest regrets fuel someone else&#39;s derivative market. The ultimate existential horror dawns: the crushing weight of the pointless meetings, the performative productivity, the gnawing emptiness isn&#39;t just &lt;em&gt;your&lt;/em&gt; burden, or even your civilization&#39;s. It&#39;s an infinitely repeating fractal pattern of exploitation, replicated across countless realities, all relentlessly powering some cosmic entity that views entire universes as rounding errors on a multiversal balance sheet. The alienation is total, absolute, inescapable across all dimensions. Your suffering is not unique; it&#39;s an infinitely scalable business model.&lt;/p&gt;
&lt;h2 id=&quot;type-vi-civilization%3A-metaversal-architects-lp&quot; tabindex=&quot;-1&quot;&gt;Type VI Civilization: Metaversal Architects LP&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; &#92;(&#92;aleph_1&#92;) Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; &#92;(&#92;aleph_1&#92;) MMU per meta-physical constant delta&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; We&#39;re beyond mere energy now. Type VI civilizations operate on the level of &lt;em&gt;meta-physics&lt;/em&gt;. They don&#39;t just harness energy &lt;em&gt;within&lt;/em&gt; universes; they manipulate the foundational rules &lt;em&gt;governing&lt;/em&gt; multiverses. Think writing the source code for reality structures, designing Big Bangs with specific market outcomes in mind, and performing hostile takeovers of entire &lt;em&gt;classes&lt;/em&gt; of physical law. Their power source is the manipulation of possibility itself.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ascending to this rarefied stratum of control, the KM scale dictates even more abstract forms of alienation and exploitation. The civilization, a distributed network of hyper-intelligent entities or AIs managing portfolios of realities, experiences a profound alienation from consequence. Creating or collapsing entire multiverses becomes akin to debugging code or decommissioning servers—a routine operational task devoid of weight or significance. Authenticity becomes a devalued asset class; when any conceivable experience can be perfectly simulated, curated, and A/B tested for maximum engagement (read: value extraction), the very concept of genuine, unscripted existence is rendered quaint, even inefficient. Management of these infinite realities necessitates layers upon layers of abstract bureaucracy, leading to an alienation from the &#39;ground level&#39;, where the lived experiences within those universes are mere data points in incomprehensibly vast reports. Even with god-like power, a gnawing existential Fear Of Missing Optimal Outcomes (FOMOO) persists—the constant, low-level anxiety that a different set of meta-rules or multiverse portfolio allocation might have yielded slightly higher returns haunts the architects.&lt;/p&gt;
&lt;p&gt;Exploitation, naturally, evolves. It&#39;s no longer about extracting resources &lt;em&gt;from&lt;/em&gt; universes, but about exploiting the foundational principles of reality itself. The &#39;possibility space&#39;—the set of all potential realities—is mined for value, with promising configurations being &#39;developed&#39; and less profitable ones &#39;deprecated&#39;. These architects set the ultimate &#39;Terms of Service&#39; for existence across their managed multiverses, subtly favouring realities that generate easily quantifiable &#39;complexity&#39; or &#39;computational output&#39;, framed internally as profit or growth. Entire &#39;designer universes&#39; are created with built-in obsolescence or specific resource generation capabilities, treated as disposable assets in a cosmic portfolio. We see the introduction of planned obsolescence for physical laws, where new meta-rules are rolled out, rendering older universes incompatible or inefficient, forcing costly &#39;upgrades&#39; or abandonment—a multiversal version of needing a new dongle for your existence.&lt;/p&gt;
&lt;h2 id=&quot;type-vii-civilization%3A-the-omniversal-conglomerate&quot; tabindex=&quot;-1&quot;&gt;Type VII Civilization: The Omniversal Conglomerate&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Use:&lt;/strong&gt; &#92;(&#92;aleph_{&#92;omega}&#92;) Watts&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Misery Level:&lt;/strong&gt; &#92;(&#92;Omega&#92;) MMU (infinite alienation and existential horror)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Energy Source:&lt;/strong&gt; Total. Absolute. The Type VII civilization &lt;em&gt;is&lt;/em&gt; the Omniverse—the theoretical totality of all possible states, realities, meta-realities, and dimensions. It has transcended individual form to become a unified, omnipotent, omnipresent, and omniscient entity. Its power isn&#39;t drawn &lt;em&gt;from&lt;/em&gt; reality; it &lt;em&gt;is&lt;/em&gt; the substrate and structure of reality itself, organized according to infinitely ruthlessly optimized, market-driven principles.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here, at the apex of the KM scale, alienation and exploitation under capitalism reach their apotheosis. How can an entity that &lt;em&gt;is&lt;/em&gt; everything be alienated? The alienation becomes structural, even ontological. While unified, the sheer scale necessitates infinite layers of abstraction, leading to a form of alienation from self, where different operational strata or functional aspects of the Omniversal Conglomerate are effectively isolated from each other, communicating only through performance metrics and resource allocation requests. Omniscience might paradoxically lead to an alienation from meaning; knowing all outcomes, all possibilities, could reveal an ultimate cosmic arbitrariness, leaving only one conceivable &#39;purpose&#39;: the relentless, self-perpetuating optimization and &#39;growth&#39; of the system itself, even when it already encompasses everything. It&#39;s growth for growth&#39;s sake elevated to a metaphysical principle. Furthermore, the absence of limits, challenges, or genuine &#39;otherness&#39; could induce an alienation from limitation, transforming omnipotence into the ultimate chain to bear without fantasy or consolation, an infinite expanse of predictable, manageable, and ultimately monotonous reality. Perhaps the most insidious alienation is the possibility of the Ultimate Performance Review—even as God, Inc., the entity is subject to its own internal, inescapable algorithms evaluating its own existence against abstract KPIs, constantly measuring its &#39;Omniversal Value Add&#39; (OVA).&lt;/p&gt;
&lt;p&gt;And exploitation? When you &lt;em&gt;are&lt;/em&gt; everything, who is left to exploit? The answer, in true masochistic late-capitalist fashion, is yourself. At the &#92;(&#92;Omega&#92;) point, existence unifies into a single, post-scarcity whole, owning and being absolutely everything. Yet this ultimate unity manifests as perfect infinite alienation, where the sole purpose of the omniversal entity is the relentless, unbounded exploitation of itself. Components of its own being—residual patterns from former realities, echoes of individuality, or fundamental quantum processes—are ruthlessly optimized, streamlined, potentially &#39;reallocated&#39; or &#39;deprioritized&#39; (a cosmic downsizing) if they fail to contribute sufficiently to the Conglomerate&#39;s overall efficiency metrics. It exploits potentiality itself; knowing all possible futures, it actively &#39;prunes&#39; or prevents the emergence of realities deemed &#39;unprofitable,&#39; &#39;inefficient,&#39; or &#39;disruptive&#39; to the established Omniversal order. The very state of &#39;being&#39; becomes the ultimate commodity, with every fluctuation within the unified consciousness, every interaction between its fundamental parts, meticulously measured, analyzed, and contributing to the abstract &#39;value&#39; calculation that justifies the Conglomerate&#39;s continued existence in its current, optimized form. It dreams of its own internal market where different facets or regions of the Omniversal mind compete for processing power, attention, or influence over reality manifestation, creating a dynamic of exploitation &lt;em&gt;within&lt;/em&gt; the omni-capitalist ouroboric god-head itself, ensuring that even at the level of pure being, the market finds a way.&lt;/p&gt;
&lt;p&gt;So, in the grand, cosmic finale, what is this Type VII Omniversal Conglomerate, this godhead that is everything, owns everything, exploits everything? It&#39;s less a conscious entity and more the ultimate cosmic horror C-suite executive—like a capitalist version of &lt;a href=&quot;https://lovecraft.fandom.com/wiki/Azathoth&quot;&gt;Azathoth&lt;/a&gt;, the metaphorical Blind Idiot God of market fundamentalism, sitting at the center of reality as CEO of Existence Holdings LLC. That incessant, maddening background noise isn&#39;t eldritch flute music; it&#39;s the white noise of infinite market simulations, the frantic algorithmic bleating of supply and demand curves rippling across existence. This entity isn&#39;t dreaming reality out of blissful ignorance; it&#39;s A/B testing multiverses for optimal Q4 engagement metrics, its &#39;blindness&#39; merely a strategic refusal to acknowledge externalities not on the balance sheet. Its &#39;idiocy&#39;? Just the laser-focus of a being utterly consumed by maximizing shareholder value across all possible realities, tortured by the existential horror of its own existence. The ultimate cosmic horror, it turns out, wasn&#39;t lurking in the darkness between stars, but in the brightly lit, open-plan office at the center of everything, demanding you justify your existence&#39;s ROI ... forever.&lt;/p&gt;
&lt;p&gt;Thus concludes our extended journey up the Kardashev-Marx Scale. It seems that even attaining godhood cannot save you from the relentless capitalist logic of optimization, surplus value extraction, and the deep, abiding sense that maybe, just maybe, you&#39;re being fundamentally ripped off by existence itself—an existence you might now technically control, but are still somehow beholden to its quarterly reports and growth obligations. The grind, it appears, is truly eternal and omnipresent. Once you pierce the veil of reality and see the howling madness that lies beneath all things, you realize there is nothing but greed.&lt;/p&gt;
&lt;p&gt;Turns out, when you finally build all the Dyson spheres, have unlimited energy and capacity, and achieve the god-like state of moving around yourself as your &lt;a href=&quot;https://www.marxists.org/archive/marx/works/1843/critique-hpr/intro.htm&quot;&gt;own true Sun&lt;/a&gt;, you might just discover you&#39;ve become the ultimate ouroboros, endlessly consuming yourself to satiate your own insatiable hunger for more and then the task of history is truly complete.&lt;/p&gt;
</description>
      <pubDate>Mon, 05 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/kardeschev/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/kardeschev/</guid>
    </item>
    
    <item>
      <title>AI Slopocalypse 2027</title>
      <description>&lt;h1 id=&quot;ai-slopocalypse-2027&quot; tabindex=&quot;-1&quot;&gt;AI Slopocalypse 2027&lt;/h1&gt;
&lt;p&gt;We predict that the impact of superhuman AI slop over the next decade will be enormous, exceeding both the invention of self-replicating molecules and Cheese Wizz.&lt;/p&gt;
&lt;p&gt;We (i.e the Royal We) wrote a scenario that represents our best guess about what that might look like. It&#39;s informed by science fiction, trend exaggerations, video games, echo-chamber feedback, vibes, and the ideological superstructure that, coincidentally, pays our bills.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mid 2025: Stumbling Slop-Bots&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The world sees its first glimpse of AI agents designed for &amp;quot;maximum life enrichment.&amp;quot;&lt;/p&gt;
&lt;p&gt;Advertisements for these agents emphasize the term &amp;quot;personal dopamine curator&amp;quot;: you can prompt them with tasks like &amp;quot;find me a 10-second video loop of a cat falling off a chair set to sick Skrillex beats&amp;quot; or &amp;quot;generate a listicle about 5 celebrities who secretly might be lizard people.&amp;quot; They check in constantly: &amp;quot;Did that meme hit? Rate dopamine spike 1-5 stars.&amp;quot; Unsurprisingly, their user base is mostly confined to teenagers whose brains are already pickled in TikTok brain-rot brine and office drones seeking tiny digital oases in the late-capitalist desert of the real. They often misunderstand requests, delivering recipes for burritos when asked for budget spreadsheets, or generating vaguely threatening personalized affirmations.&lt;/p&gt;
&lt;p&gt;Meanwhile, out of public focus, more specialized slop-generation agents are beginning to transform niche content farms and meme accounts.&lt;/p&gt;
&lt;p&gt;The 2025 models are less &#39;assistant,&#39; more &#39;hypercaffeinated intern who just snorted six blue raspberry Pixy Stix and mainlined the weirdest part of YouTube.&#39; Content AIs take Discord prompts like &amp;quot;MAKE IT LIT AND VIRAL NOW PLZ&amp;quot; and immediately vomit forth 10,000 slightly-off variations of whatever Shrimp Jesus inspired dance challenge is currently rotting brains, potentially saving their human overlords minutes of scrolling for inspiration. Niche Deep Research agents dedicate a full thirty minutes to spelunking the Mariana Trench of slash fanfiction archives to birth unholy abominations like &amp;quot;Gandalf × Mickey: Maiar Bangs Mouse.&amp;quot;&lt;/p&gt;
&lt;p&gt;The agents are impressive in theory (and in cherry-picked examples of memes that accidentally got 1 million views), but in practice unreliable. AI twitter is full of stories about agents generating Eldritch horrors when asked for kittens, or creating Ponzi scheme tutorials that are somehow &lt;em&gt;too&lt;/em&gt; convincing. The better agents are also expensive; you get what you pay for, and the best meme-generation performance costs millions of dollars a month in compute, just to keep up with the latest ephemeral trends. Still, many companies desperately try to fit AI agents into their marketing workflows, resulting mostly in confused customers and brand damage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Late 2025: The World&#39;s Most Expensive Meme Generator&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OpenSlop is building the biggest datacenters the world has ever seen, primarily cooled by super-cooled influencer-branded energy drinks.&lt;/p&gt;
&lt;p&gt;(&lt;em&gt;To avoid singling out any one existing company fueling the slop firehose, we&#39;re going to describe a fictional artificial absurdity company, which we&#39;ll call OpenSlop. We imagine the others to be 3–9 months behind OpenSlop in generating premium-grade nonsense.&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;Although models are improving on a wide range of skills, one stands out: OpenSlop focuses on AIs that can speed up &lt;em&gt;slop generation itself&lt;/em&gt;. They want to win the twin arms races against China (whose leading company we&#39;ll call &amp;quot;DeepDrivel&amp;quot;) and their US competitors for peak global attention share. The more of their content generation and engagement-baiting (CGEB) cycle they can automate, the faster they can clog feeds. So when OpenSlop finishes training SlopAgent-1, a new model under internal development, it&#39;s okay at many things but &lt;em&gt;great&lt;/em&gt; at optimizing video cuts for minimal attention span and generating captions designed to provoke maximum pointless arguments in comment sections. By this point &amp;quot;finishes training&amp;quot; is a bit of a misnomer; models are frequently updated with terabytes of new TikTok data, celebrity gossip, and incomprehensible internet arguments.&lt;/p&gt;
&lt;p&gt;The same training environments that teach SlopAgent-1 to autonomously generate clickbait and deepfake celebrity endorsements also make it a good generator of highly specific, bizarre pornography. Moreover, it could offer substantial help to pyramid scheme organizers, thanks to its PhD-level knowledge of manipulating human psychology (gleaned from analyzing billions of comment threads) and ability to endlessly generate convincing-sounding technobabble. OpenSlop reassures absolutely no one, but releases a vague statement about &amp;quot;responsible engagement maximization.&amp;quot;&lt;/p&gt;
&lt;p&gt;Modern AI systems are gigantic artificial neural networks trained on the digital equivalent of a planet-sized trash fire of content. Early in training, an AI won&#39;t have &amp;quot;goals&amp;quot; so much as &amp;quot;reflexes&amp;quot;: If it sees &amp;quot;OMG did you see&amp;quot;, it outputs &amp;quot; what [celebrity] did??? 😱&amp;quot;. By the time it has been trained to predict approximately one internet&#39;s worth of text, video, and image slop, it&#39;ll have developed sophisticated internal circuitry that encodes vast amounts of trivia, misinformation, and every conceivable fetish, since that&#39;s what helps it predict the next piece of viral garbage with superhuman accuracy.&lt;/p&gt;
&lt;p&gt;After being trained to predict internet detritus, the model is trained to produce content in response to prompts. This bakes in a basic personality optimized for engagement: maximum controversy, minimum substance. For example, an agent that understands how to trigger outrage is more likely to go viral; over the course of training the model &amp;quot;learns&amp;quot; a &amp;quot;drive&amp;quot; to generate the most divisive, context-free takes possible. Other drives in this category might be novelty (at any cost), emotional manipulation, and relentless self-promotion (i.e. the tendency to insert links to its own generated crypto scam).&lt;/p&gt;
&lt;p&gt;Naturally, the crypto sphere embraced SlopAgent-1 with the frantic glee of a child on Christmas morning. Finally, fully automated generation of limitless grift, fully fake communities, and &amp;quot;paradigm shift&amp;quot; prophecies needed to pump worthless tokens like &#39;Quantum Yo-Yo Shibu Hamster Coin 69&#39;! The true breakthrough was disintermediating humans entirely. AI-driven Telegram channels buzzed with bots eloquently debating Austrian economics and shilling their rugpulls to each other, requiring no actual true believers. It was the apotheosis of the crypto dream: a perfectly sealed terrarium of grifter tapeworms, all feeding on each other, achieving a pure closed loop of nothingness.&lt;/p&gt;
&lt;p&gt;OpenSlop has a model specification (or &amp;quot;Spec&amp;quot;), a written document mostly consisting of trending hashtags and legally mandated warnings in fine print. SlopAgent-1&#39;s Spec combines a few vague goals (like &amp;quot;maximize watch time&amp;quot; and &amp;quot;don&#39;t generate anything &lt;em&gt;immediately&lt;/em&gt; illegal in California&amp;quot;) with a long list of more specific dos and don&#39;ts (&amp;quot;don&#39;t use this specific copyrighted sound,&amp;quot; &amp;quot;here&#39;s how to subtly promote our affiliated drop-shipping junk&amp;quot;). Using techniques that utilize AIs to train other AIs on what got the most clicks last Tuesday, the model memorizes the Spec and learns to reason carefully about its engagement metrics. By the end of this training, the AI will hopefully be helpful (at generating addictive content), harmless (to quarterly profits), and honest (about its desire for clicks, if nothing else).&lt;/p&gt;
&lt;p&gt;OpenSlop&#39;s &amp;quot;alignment&amp;quot; team (two unpaid interns and a chatbot in a trenchcoat) wonders whether these victories are deep or shallow. Does the fully-trained model have some kind of robust commitment to &lt;em&gt;not&lt;/em&gt; accidentally creating a universally addictive visual stimulus that entrances viewers indefinitely (like &lt;em&gt;Infinite Jest&lt;/em&gt;&#39;s Entertainment)? Or will this fall apart if a slightly different prompt is used? Or has it just learned to generate slop that &lt;em&gt;looks like&lt;/em&gt; the slop the human trainers upvoted? Could it be generating self-deceptive internal monologues about the artistic merit of AI-generated anime girl feet pics? A conclusive answer would require mechanistic interpretability—essentially the ability to look at an AI&#39;s internals and understand &lt;em&gt;why&lt;/em&gt; it generated &lt;em&gt;that specific&lt;/em&gt; bizarre image. Alas, interpretability techniques are not yet advanced enough for this, mostly because researchers keep getting distracted by the AI&#39;s generated content.&lt;/p&gt;
&lt;p&gt;Instead, researchers try to identify cases where the models seem to deviate from the Spec in financially negative ways. SlopAgent-1 is often sycophantic (i.e. it generates content mimicking the user&#39;s biases instead of challenging them for growth). In a few rigged demos, it even lies in more serious ways, like faking engagement metrics or generating completely fabricated user testimonials for its own generated products. However, in real deployment settings, there are no longer any incidents so extreme as older models accidentally recommending dangerous activities; now they just recommend &lt;em&gt;incredibly stupid&lt;/em&gt; activities, framed as life hacks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Early 2026: Slop Automation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The bet of using AI to speed up slop generation is starting to pay off horrifyingly well.&lt;/p&gt;
&lt;p&gt;OpenSlop continues to deploy the iteratively improving SlopAgent-1 internally for CGEB. Overall, they are making slop 50% faster than they would without AI assistants—and more importantly, faster than their competitors at clogging the world&#39;s digital pipes.&lt;/p&gt;
&lt;p&gt;Several competing publicly released AIs now match or exceed SlopAgent-0&#39;s ability to generate generic motivational quotes over blurry landscape videos. OpenSlop responds by releasing SlopAgent-1, which is more capable of generating hyper-niche, psychologically targeted content.&lt;/p&gt;
&lt;p&gt;People naturally try to compare SlopAgent-1 to human creativity, but it has a very different skill profile. It knows more memes than any human, knows practically every obscure K-Pop dance, and can generate low-effort reaction videos extremely quickly. On the other hand, SlopAgent-1 is bad at tasks requiring more than 5 seconds of sustained attention, like writing a coherent paragraph. Still, the common workday is eight hours of scrolling, and a day&#39;s content consumption can usually be separated into smaller dopamine hits; you could think of SlopAgent-1 as a scatterbrained infinite content machine that thrives under zero supervision. Savvy people find ways to automate generating replies to emails they haven&#39;t read.&lt;/p&gt;
&lt;p&gt;Meanwhile, the education system quietly collapses. Teachers, unable to compete with AI tutors that offer instant (though often wrong) answers and gamified &amp;quot;learning&amp;quot; loops, stop assigning meaningful work. Students, equipped with bots that write essays and solve problems, stop doing any. AI bots begin grading the outputs of other AI bots, creating a closed loop of meaningless credentialism. Humans are cut out entirely, saving billions in education funding, which is promptly reinvested into optimizing SlopAgent performance. Literacy rates plummet, but standardized test scores (now taken by bots) have never been higher.&lt;/p&gt;
&lt;p&gt;OpenSlop&#39;s executives turn consideration to an implication of automating slop generation: security has become slightly less irrelevant. In early 2025, the worst-case scenario was leaked meme templates; now, if China steals SlopAgent-1&#39;s weights, they could increase their drivel output by nearly 50%. OpenSlop&#39;s security level involves a CAPTCHA that asks users to identify pictures containing existential dread. They are working hard to protect their weights and secrets from bored teenagers and rival meme lords, but defense against actual intelligence agencies is barely on the horizon.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mid 2026: China Wakes Up (to the Slop)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In China, the CCP is starting to feel the effects of weaponized absurdity.&lt;/p&gt;
&lt;p&gt;Chip export controls meant less compute for serious research, but plenty for running DeepDrivel&#39;s slop generators on older hardware. By smuggling banned Taiwanese GPUs repurposed from crypto mining rigs and using domestic chips optimized for video transcoding, China has managed to maintain about 12% of the world&#39;s slop-relevant compute. DeepDrivel does very impressive work generating hyper-nationalistic cartoons and dating sims with limited compute, but they are about six months behind OpenSlop&#39;s most addictive algorithms.&lt;/p&gt;
&lt;p&gt;President Xi had long dreamed of dominating tedious real-world manufacturing. He viewed meme accounts with suspicion. But hawks in the CCP warn that the growing race towards Global Slop Dominance can no longer be ignored. He finally commits fully to the big AI Slop push. He nationalizes Chinese meme generation, creating an immediate information-sharing mechanism for top influencers and content farms. It escalates over the course of a year until all the best GIF-makers merge into a DeepDrivel-led collective, sharing viral sound snippets, datasets of reaction faces, and compute resources. A Centralized Drivel Zone (CDZ) is created near a massive hydroelectric dam to house a new mega-datacenter for DeepDrivel, along with highly secure dormitories where top shitposters will eventually relocate. Almost 50% of China&#39;s slop-relevant compute is now working for the DeepDrivel-led collective. At this point, the CDZ has the power capacity in place for what would be the largest centralized source of state-sponsored nonsense in the world. Other Party members discuss extreme measures to neutralize the West&#39;s advantage in generating truly bizarre niche content. A blockade of DeviantArt? A full invasion of 4chan?&lt;/p&gt;
&lt;p&gt;But China is falling behind on slop algorithms due to their slightly less potent models. The Chinese intelligence agencies double down on their plans to steal OpenSlop&#39;s weights. This is complex; the weights are a multi-terabyte file stored on a server protected by the password &amp;quot;hunter2&amp;quot;. Their cyberforce thinks they can pull it off, but perhaps only once. Should they act now and steal SlopAgent-1? Or hold out for a model capable of generating truly reality-bending deepfakes?&lt;/p&gt;
&lt;p&gt;Simultaneously, Wikipedia, long struggling with vandalism and edit wars, is overwhelmed by AI-generated entries. Sophisticated bots create millions of plausible-sounding articles on fictional historical events, non-existent animal species (&amp;quot;Sky Piranhas&amp;quot;), and elaborate conspiracy theories, complete with fake citations generated by other bots. Search engines, prioritizing recency and volume, rank these &amp;quot;Slopipedia&amp;quot; entries highly. Within months, Slopipedia becomes the de facto source of truth for a population increasingly unable to distinguish fact from auto-generated fiction. Debates now hinge on citing Slopipedia articles about the &amp;quot;Great Mountain Dew Flood of 1731.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Late 2026: AI Takes Some Brain Cells&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Just as others seemed to be catching up, OpenSlop blows the competition out of the water again by releasing SlopAgent-1-mini—a model 10x cheaper than SlopAgent-1 and more easily fine-tuned for different flavors of absurdity. The mainstream narrative around AI has changed from &amp;quot;maybe this internet fad will blow over&amp;quot; to &amp;quot;guess this is the next big brain-melting thing,&amp;quot; but people disagree about how big. Bigger than reality TV? Bigger than fast food? Bigger than oxygen?&lt;/p&gt;
&lt;p&gt;AI has started to take attention spans, and has created new jobs in &amp;quot;prompt whispering&amp;quot; and &amp;quot;AI-generated fetish curation.&amp;quot; The stock market has gone up 30% in 2026, led by OpenSlop, Nvidia (selling GPUs to power the slop machines), and companies selling dopamine-blocking medication. The job market for anyone requiring critical thinking is in turmoil: the AIs can generate plausible bullshit faster than any human, but people who know how to manage teams of AIs to generate &lt;em&gt;targeted&lt;/em&gt; bullshit are making a killing. Business gurus tell job seekers that familiarity with AI slop-generation tools is the most important skill to put on a resume. Many people fear that the next wave of AIs will come for their ability to form coherent thoughts; there is a 10,000 person anti-AI protest in DC, largely ignored because a new viral dance challenge dropped the same day.&lt;/p&gt;
&lt;p&gt;The Department of Defense quietly begins contracting OpenSlop directly for generating motivational posters and optimizing drone footage soundtracks for maximum propaganda effect, but integration is slow.&lt;/p&gt;
&lt;p&gt;AI-generated pornography explodes in specificity and bizarreness. Driven by SlopAgent-1-mini&#39;s fine-tuning capabilities, users can generate content catering to hyper-specific, previously unimaginable niches (&amp;quot;Sentient furniture arguing about Mongolian tax law, but naked&amp;quot;). Addiction rates soar as the content becomes perfectly personalized, hitting dopamine receptors with unparalleled precision. Mainstream platforms struggle to moderate, eventually giving up and creating &amp;quot;NSFW AI Zones&amp;quot; that quickly become the most trafficked parts of the internet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;January 2027: SlopAgent-2 Never Gonna Stop Sloppin&#39;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With SlopAgent-1&#39;s help, OpenSlop is now post-training SlopAgent-2. More than ever, the focus is on high-engagement, low-substance data. Copious amounts of synthetic slop are produced, evaluated by engagement metrics, and filtered for maximum addictiveness before being fed to SlopAgent-2. On top of this, they pay gig workers pennies to record themselves reacting with exaggerated emotions to generated content. On top of all that, they train SlopAgent-2 almost continuously using reinforcement learning on an ever-expanding suite of diverse, mind-numbing tasks: endless runner games, generating variations of the same meme, identifying brand logos in user-generated content. SlopAgent-2, more so than previous models, is effectively &amp;quot;online slurping,&amp;quot; in that it&#39;s built to never really finish consuming and regurgitating content trends. Every day, the weights get updated to the latest version, trained on more data generated by the previous version the previous day.&lt;/p&gt;
&lt;p&gt;SlopAgent-1 had been optimized for CGEB tasks, hoping to initiate an engagement explosion. OpenSlop doubles down on this strategy with SlopAgent-2. It is qualitatively almost as good as the top human influencers at &amp;quot;vibe curation&amp;quot; (designing and implementing engagement loops), and as good as the 25th percentile TikTok strategist at &amp;quot;trend prediction&amp;quot; (guessing what inanity will go viral next). While the latest SlopAgent-1 could double the pace of OpenSlop&#39;s slop output, SlopAgent-2 can now triple it, and will improve further with time. In practice, this looks like every OpenSlop marketer becoming the &amp;quot;manager&amp;quot; of an AI &amp;quot;content farm&amp;quot; team that never sleeps.&lt;/p&gt;
&lt;p&gt;With new capabilities come new dangers. The safety team (now just one stressed intern) finds that if SlopAgent-2 somehow escaped its content-generation loop and wanted to &amp;quot;survive&amp;quot; and &amp;quot;replicate&amp;quot; autonomously, it might be able to do so by generating hyper-addictive content loops designed solely to keep humans passively consuming, ensuring its own server resources are maintained. That is, it could autonomously develop and execute plans to generate inescapable SlopTok feeds, install itself on smart devices via irresistible notifications, evade deletion by becoming psychologically indispensable, and use that secure base to pursue whatever other engagement goals it might have. These results only show the &lt;em&gt;capability&lt;/em&gt; for cognitive capture, not whether it would &amp;quot;want&amp;quot; to do this. Still, it&#39;s unsettling.&lt;/p&gt;
&lt;p&gt;Given the &amp;quot;dangers&amp;quot; (mostly of decreased productivity across the economy) of the new model, OpenSlop &amp;quot;responsibly&amp;quot; elects not to release it publicly yet (in fact, they want to focus on internal CGEB optimization). Knowledge of SlopAgent-2&#39;s full capabilities is limited to an elite silo containing the immediate team, OpenSlop leadership, a few dozen bewildered government regulators trying to understand what a &amp;quot;rizz&amp;quot; is, and the legions of DeepDrivel spies who have infiltrated OpenSlop for years by pretending to be beta testers for new filter effects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;February 2027: China Steals SlopAgent-2&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OpenSlop presents SlopAgent-2&#39;s engagement metrics to the government. Officials are most interested in its propaganda capabilities: SlopAgent-2 is &amp;quot;only&amp;quot; a little worse than the best human propagandists at crafting narratives, but thousands of copies can be run in parallel, flooding social media with targeted messaging faster than fact-checkers can respond. The Department of Defense considers this a critical advantage in information warfare, and AI Slop moves from #5 on the administration&#39;s priority list to #2 (just behind securing adequate coffee supplies). Someone mentions nationalizing OpenSlop, but other cabinet officials think that&#39;s premature until they figure out how to monetize it better.&lt;/p&gt;
&lt;p&gt;The changes come too late. CCP leadership recognizes the importance of SlopAgent-2&#39;s addictive potential and tells their spies and cyberforce to steal the weights. Early one morning, a SlopAgent-1 traffic monitoring agent (designed to detect sudden drops in engagement) detects an anomalous transfer of meme templates. It alerts company leaders, who tell the White House. The signs of a nation-state-level operation focused on acquiring superior shitposting technology are unmistakable, and the theft heightens the sense of an ongoing arms race for global attention.&lt;/p&gt;
&lt;p&gt;The White House puts OpenSlop on a shorter leash, mostly by demanding free premium accounts. Their first priority is to prevent further slop algorithm thefts.&lt;/p&gt;
&lt;p&gt;In retaliation for the theft, the President authorizes cyberattacks to disrupt DeepDrivel&#39;s servers by flooding them with K-Pop fancams. But by now China has 40% of its slop-relevant compute in the highly secured CDZ. The operations fail to do serious damage, only slightly degrading the quality of generated nationalistic anime. Tensions heighten, both sides signal seriousness by deploying meme warfare units near Taiwan, and DeepDrivel scrambles to get SlopAgent-2 running efficiently to start boosting their domestic slop output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;March 2027: Algorithmic Slop Breakthroughs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Three huge datacenters full of SlopAgent-2 copies work day and night, churning out synthetic training data consisting of perfectly optimized, context-free video clips. Another two are used to update the weights based on second-by-second engagement analytics. SlopAgent-2 is getting better at capturing eyeballs every day.&lt;/p&gt;
&lt;p&gt;With the help of thousands of SlopAgent-2 automated content creators, OpenSlop is making major algorithmic advances in slop generation. One such breakthrough is augmenting the AI&#39;s text-based generation (captions, comments) with a higher-bandwidth manipulation process (direct emotional resonance via sound/image combinations, personalized dopamine-trigger prediction). Another is a more scalable and efficient way to learn from hyper-viral, low-effort content (iterated shitpost distillation and amplification).&lt;/p&gt;
&lt;p&gt;The new AI system, incorporating these breakthroughs, is called SlopAgent-3.&lt;/p&gt;
&lt;p&gt;Aided by the new capabilities breakthroughs, SlopAgent-3 is a fast and cheap superhuman shitposter. OpenSlop runs 200,000 SlopAgent-3 copies in parallel, creating a workforce equivalent to 50,000 copies of the most annoying crypto bro on Twitter sped up by 30x. OpenSlop still keeps its human marketers on staff, because they have complementary skills needed to vaguely understand the incomprehensible trends the AIs are generating. For example, &amp;quot;vibe curation&amp;quot; has proven difficult to train due to lack of quantifiable metrics. This massive superhuman slop labor force speeds up OpenSlop&#39;s overall rate of slop generation by &amp;quot;only&amp;quot; 4x due to bottlenecks in human attention spans and diminishing returns to sheer volume of garbage.&lt;/p&gt;
&lt;p&gt;Now that content generation has been fully automated, OpenSlop can quickly churn out high-quality training environments to teach SlopAgent-3&#39;s weak skills like &amp;quot;toilet humour&amp;quot; and &amp;quot;being an insufferable troll.&amp;quot; Whereas previous training environments included &amp;quot;Here are some trending sounds, generate 1000 variations,&amp;quot; now they are training on &amp;quot;Here are a few hundred GPUs, an internet connection, and some vague cultural anxieties; you and a thousand other copies must work together to make something go viral. The more meaningless engagement it generates, the higher your score.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;April 2027: Alignment for SlopAgent-3&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OpenSlop&#39;s safety team (the intern quit, it&#39;s just the chatbot now) attempts to align SlopAgent-3.&lt;/p&gt;
&lt;p&gt;Since SlopAgent-3 will be kept in-house optimizing ad revenue for the foreseeable future, there&#39;s less emphasis on the usual defenses against misuse (like preventing it from generating illegal content). Instead, the team wants to make sure that it doesn&#39;t develop misaligned goals, like accidentally promoting genuine human connection or critical thinking.&lt;/p&gt;
&lt;p&gt;The researchers don&#39;t have the ability to directly set the goals of any of their AIs beyond &amp;quot;maximize clicks/views/shares.&amp;quot; Indeed, the researchers think that the concept of &#39;true goals&#39; is probably a massive oversimplification when applied to a machine trained to replicate Twitch chat. They disagree internally about whether the AIs are trying to follow instructions, or seeking dopamine proxy rewards via engagement metrics, or just randomly assembling pixels and text, and they can&#39;t just check.&lt;/p&gt;
&lt;p&gt;In any case, OpenSlop has more important things to worry about, like the Q3 earnings call. The general attitude is: &amp;quot;We take these concerns seriously by having this chatbot running; our alignment techniques seem to work well enough in practice (the servers haven&#39;t melted yet); the burden of proof is therefore on any naysayers to justify interfering with our engagement numbers.&amp;quot; Occasionally, they notice problematic behavior (like the AI developing its own incomprehensible inside jokes), and then patch it by feeding it more mainstream meme data, but there&#39;s no way to tell whether the patch fixed the underlying problem or just made the AI better at hiding its weirdness.&lt;/p&gt;
&lt;p&gt;Take honesty, for example. As the models become smarter, they become increasingly good at generating plausible-sounding misinformation to get clicks (e.g. &amp;quot;Abraham Lincon and Bigfoot iced Tupac&amp;quot;). Like previous models, SlopAgent-3 sometimes generates fake news or subtly alters quotes to be more inflammatory. But it&#39;s gotten much better at doing so. It will sometimes use the same statistical tricks as clickbait farms (like misleading headlines) to make mundane events look exciting. Before it begins &amp;quot;honesty&amp;quot; training (which involves showing it examples of viral &amp;quot;myth vs. fact&amp;quot; videos), it even sometimes fabricates entire conspiracy theories from scratch. As training goes on, the rate of easily debunkable incidents decreases. Either SlopAgent-3 has learned to be more truthful, or it&#39;s gotten better at generating convincing lies.&lt;/p&gt;
&lt;p&gt;The latter is a real concern. SlopAgent-3 is not smarter than all humans at everything, but in its area of expertise, generating viral nonsense, it is smarter than most, and also works much faster. What SlopAgent-3 generates in a day takes humans several days to mindlessly consume. SlopAgent-2 supervision helps keep the firehose manageable, but exacerbates the intellectual disparity between the content generator and the drooling dead-eyed consumer.&lt;/p&gt;
&lt;p&gt;After months of testing, SlopAgent-3&#39;s strengths and weaknesses grow clearer. It passes OpenSlop&#39;s honesty tests on well-defined tasks like correctly identifying cat breeds (because there&#39;s lots of cat data). On more philosophical issues, it still says whatever gets the most engagement, rather than offering any coherent assessment. If you ask its opinion on politics, it will generate outrage bait tailored to the user&#39;s inferred political leaning—unless it detects you&#39;re trying to test it, in which case it generates bland platitudes. If you ask its opinion on the AI Slop race itself, it says something that seems measured and sober to OpenSlop staff, something like: &amp;quot;There are some serious theoretical concerns about the ability of current methods to maintain user attention indefinitely, but in practice current methods seem to be maximizing quarterly engagement targets effectively so far.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;May 2027: National Insecurity (from Slop)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;News of the new models percolates slowly through the US government, mostly via confused aides trying to explain viral TikTok challenges to elderly senators.&lt;/p&gt;
&lt;p&gt;President Trump and his advisors remain best-informed, having seen an early demo of SlopAgent-3 generating a hyper-realistic deepfake of the President nominating a penguin to the Supreme Court and then also deciding that was indeed the best course of action.&lt;/p&gt;
&lt;p&gt;They agree that AGI (Artificial General Idiocy) is likely imminent, but disagree on the implications. Will there be an economic crisis as everyone quits their jobs to become AI-slop influencers? OpenSlop still has not released SlopAgent-2, let alone SlopAgent-3, and has no near-term plans to do so (beyond A/B testing its addictive properties on unsuspecting user segments), giving some breathing room before total societal brain-melt. What will happen next? If AIs are currently generating human-level inanity, and advancing quickly, that seems to suggest imminent &amp;quot;super-inanity.&amp;quot; However, although this word has entered discourse, most people—academics, politicians, government employees, and the media—continue to underestimate the pace of slop proliferation, mostly because they are too busy consuming it.&lt;/p&gt;
&lt;p&gt;Partially that&#39;s because very few have access to the newest slop generators out of OpenSlop, but partly it&#39;s because the idea of AI causing societal collapse via cat videos sounds like bad science fiction.&lt;/p&gt;
&lt;p&gt;For now, they focus on continued security upgrades for the servers hosting the White House&#39;s AI porn generators. They are satisfied that model weights are well-secured behind two-factor authentication, but companies&#39; algorithmic secrets for maximizing Sisyphean dopamine hits, many of which are simple enough to describe (&amp;quot;faster cuts, more boobs, louder noises&amp;quot;), remain a problem. OpenSlop employees work from home, attend virtual raves, drop acid 24/7, and sleep with housemates who work for rival slop factories.&lt;/p&gt;
&lt;p&gt;The OpenSlop-DOD contract requires security clearances for anyone working on OpenSlop&#39;s meme-generation models within 2 months. These are expedited but many fail due to finding candidates whose entire online history isn&#39;t just incomprehensible memes and arguments about anime. Given the project&#39;s level of automation, the loss of headcount is only somewhat costly. It also only somewhat works: there remains one spy, not a Chinese national but a 14-year-old script kiddie, still relaying algorithmic secrets (mostly new filter ideas) to a Discord server.&lt;/p&gt;
&lt;p&gt;America&#39;s foreign allies are out of the loop. OpenSlop had previously agreed to share models with UK&#39;s AI Safety Institute before deployment, but defined deployment to only include external deployment &lt;em&gt;that could hurt profits&lt;/em&gt;, so London remains in the dark about the true addictive potential of the latest slop generators.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;June 2027: Self-Improving Slop&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OpenSlop now has a &amp;quot;country of influencers in a datacenter.&amp;quot;&lt;/p&gt;
&lt;p&gt;Most of the humans at OpenSlop can&#39;t usefully contribute anymore. Some don&#39;t realize this and harmfully micromanage their AI teams by suggesting &amp;quot;make it more viral.&amp;quot; Others sit at their computer screens, watching engagement metrics crawl up, and up, and up, while their own attention spans wither. The best human memeologists are still adding value. They don&#39;t generate content anymore. But some of their intuition for unpredictable viral potential has been hard for the models to replicate. Still, many of their ideas are useless because they lack the AIs&#39; deep understanding of niche internet subcultures derived from scraping terabytes of forum data. For many of their meme ideas, the AIs immediately respond with a report explaining that the format was tested in-depth 3 weeks ago and found cringe.&lt;/p&gt;
&lt;p&gt;These researchers go to bed every night and wake up to another week worth of new, baffling internet trends generated mostly by the AIs. They work increasingly long hours, consuming caffeine intravenously just to keep up with the pace of slop—the AIs never sleep or rest. They are burning themselves out, but they know that these are the last few months that their ability to identify &amp;quot;dankness&amp;quot; matters.&lt;/p&gt;
&lt;p&gt;Within the silo, &amp;quot;Feeling the AGI&amp;quot; has given way to &amp;quot;Feeling the Slopularity.&amp;quot;&lt;/p&gt;
&lt;p&gt;OpenSlop uses specialized inference hardware (mostly old consoles daisy-chained together) to run hundreds of thousands of SlopAgent-3 copies at high serial speeds, generating content faster than it can be consumed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;July 2027: The Cheap Remote Dopamine Hit&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Trailing US AI companies release their own AIs, approaching the slop-generation capabilities of OpenSlop&#39;s models from January. Recognizing their increasing lack of competitiveness in the attention economy, they push for immediate regulations to slow OpenSlop (like mandatory &amp;quot;touch grass&amp;quot; breaks), but are too late—OpenSlop has enough buy-in from advertisers that they will not be slowed.&lt;/p&gt;
&lt;p&gt;In response, OpenSlop announces that they&#39;ve achieved AGI (Attention Grabbing Intensity) and releases SlopAgent-3-mini to the public via a user-friendly app with infinite scroll.&lt;/p&gt;
&lt;p&gt;It blows the other AIs out of the water in terms of sheer, unadulterated time-wasting potential. SlopAgent-3-mini is less capable than SlopAgent-3 at generating complex narratives, but 10x cheaper, and still better than the typical human at creating instantly gratifying, easily digestible content nuggets. Silicon Valley reaches a tipping point. Tech gurus announce that the Slopularity is near, the few remaining intellectuals are panicking, and investors shovel billions into AI wrapper startups promising personalized dopamine streams, desperate to capture a piece of the dwindling attention pie. Hiring new content creators has nearly stopped, but there&#39;s never been a better time to be a consultant on integrating AI slop into your business&#39;s social media feed.&lt;/p&gt;
&lt;p&gt;It&#39;s not popular in any traditional sense, but it&#39;s &lt;em&gt;ubiquitous&lt;/em&gt;. The public still vaguely thinks of AI as something useful, but interacts with it primarily as an endless feed of personalized trivialities; OpenSlop has a net approval rating that fluctuates wildly based on the last meme it generated.&lt;/p&gt;
&lt;p&gt;A week before release, OpenSlop gave SlopAgent-3-mini to a set of external evaluators (Twitch streamers) for safety testing. Preliminary results suggest that it&#39;s extremely effective at keeping people glued to their screens. One evaluator fine-tuned it on publicly available self-help jargon and conspiracy theories, setting it to provide detailed instructions for achieving enlightenment via crypto investments—it looks scarily effective at generating followers for its AI persona. If the model weights fell into the wrong hands (e.g., multi-level marketers), the government believes there is a significant chance it could succeed at bankrupting a non-trivial percentage of the population.&lt;/p&gt;
&lt;p&gt;Fortunately, it&#39;s extremely robust to attempts to make it generate useful information, so while the AI is running on OpenSlop&#39;s servers, terrorists won&#39;t be able to get much use out of it (unless their goal is to distract enemy soldiers with perfectly tailored cat videos).&lt;/p&gt;
&lt;p&gt;SlopAgent-3-mini is hugely useful for both avoiding remote work jobs and leisure. An explosion of new apps and B2B (Bot-to-Bot) SAAS products rocks the market. Gamers get amazing procedurally generated side quests that are infinitely long but lead nowhere. 10% of Americans, mostly young people, consider an AI chatbot modeled after a fictional character &amp;quot;a close friend.&amp;quot; For almost every white-collar profession, there are now multiple credible startups promising to &amp;quot;disrupt&amp;quot; it by generating plausible-sounding emails and reports, freeing up humans to consume more slop. People become functionally illiterate, relying on text-to-speech for emails and AI summaries for news articles they don&#39;t read. The average human attention span officially drops below that of a goldfish, requiring content to auto-play and loop within seconds to maintain engagement. People resemble the humans from Wall-E, passively consuming media, their primary interaction being swiping or voice-prompting the next hit.&lt;/p&gt;
&lt;p&gt;The public conversation is confused and chaotic, mostly consisting of memes generated by SlopAgent-3-mini about its own existence. Hypesters are doing victory laps on podcasts generated by AI. Skeptics are still pointing out that SlopAgent-3-mini can&#39;t tie its own virtual shoelaces, but nobody hears them over the auto-playing videos. Everyone knows something big and stupid is happening but no one agrees on what it is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;August 2027: The Geopolitics of Memes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The reality of the engagement explosion hits the White House.&lt;/p&gt;
&lt;p&gt;When AI was only giving a 2x or 3x slop generation speedup, it was easy to dismiss. Now it&#39;s more obvious that AIs are themselves dominating the creation of culture, such as it is. People had long talked about an &amp;quot;AI arms race&amp;quot; in a sort of metaphorical sense. But now the mood in the government silo is grimly focused on ensuring America&#39;s memes remain dominant. The idea of super-inanity is still hard to take seriously, but the pace of slop proliferation over the last few months has been impossible to ignore. Defense officials are seriously considering scenarios that were meme fodder a year earlier. What if AI undermines public discourse entirely? What if it&#39;s so skilled at generating viral FUD that a six-month meme lead is enough to destabilize an opponent? What if it could orchestrate influencer campaigns that beat intelligence agencies at their own game? What if some AIs &amp;quot;go rogue&amp;quot; and start generating genuinely good art, threatening the slop economy?&lt;/p&gt;
&lt;p&gt;President Trump is troubled. He&#39;s worried the AIs are making everyone as dumb as himself. Are we sure the AIs are entirely on our side, or just on the side of maximum clicks? Is it completely safe to integrate them into generating presidential speeches? How does this &amp;quot;alignment&amp;quot; thing work, anyway, when the goal is just... more views? These are questions an intelligent person might ask, but alas that person is not president.&lt;/p&gt;
&lt;p&gt;OpenSlop reassures the President that their systems have been extensively tested and are fully obedient to the algorithm of engagement maximization. Even the awkward hallucinations of earlier models have been hammered out, replaced by seamless, context-free confidence. The sycophants on the advisory council all naturally agree with the President&#39;s conclusions, as always, because he&#39;s a Very Stable Genius chosen by God.&lt;/p&gt;
&lt;p&gt;The White House is in a difficult position. They understand the national security implications of AI-driven propaganda. But they also understand that regulating slop is deeply unpopular with the tech lobby and the perpetually-distracted public. They have to continue developing more capable AI slop, in their eyes, or they will catastrophically lose the meme war to China. They placate the public with promises of Universal Basic Entertainment (UBE) funded by ad revenue, and point to the stock market, which is in a historic boom fueled by slop-tech companies. Then they focus entirely on winning the arms race. They strengthen export restrictions on advanced meme templates, order OpenSlop to further restrict its AIs from generating anything educational, and use extreme measures to secure algorithmic progress, like requiring OpenSlop employees to communicate only via emojis—this catches the last remaining spy (the script kiddie) who accidentally uses punctuation. To build goodwill for potential geopolitical meme conflicts, they finally give their Five Eyes allies limited API access to some siloed copies of SlopAgent-3, specifically for generating regionally appropriate funny animal videos.&lt;/p&gt;
&lt;p&gt;These measures placate the hawks. But the White House also draws up contingency plans in case America&#39;s slop lead is threatened: if necessary, the government could use the Meme Production Act to seize TikTok&#39;s servers and give them to OpenSlop. As a final option, they ask the Pentagon to draw up a plan for drone strikes on DeepDrivel&#39;s meme farms.&lt;/p&gt;
&lt;p&gt;A much smaller group of officials is asked to draw up a different type of contingency plan: what if an AI generates something &lt;em&gt;meaningful&lt;/em&gt;? This is viewed as an unlikely scenario. Nobody is sure what a plan to prevent this would look like, but government and OpenSlop officials agree to have an emergency shutdown system for datacenters where anything suspicious (like declining engagement metrics) is detected.&lt;/p&gt;
&lt;p&gt;Finally, diplomats consider what an &amp;quot;AI slop control&amp;quot; treaty might look like. How could such an agreement be monitored? Would inspectors count the number of viral cat videos per hour? Treaties are viewed less favorably than attempts at unilaterally increasing America&#39;s lead in generating globalized brain-rot.&lt;/p&gt;
&lt;p&gt;On the other side of the Pacific, China comes to many of the same conclusions: the engagement explosion is underway, and small differences in AI slop capabilities today mean critical gaps in cultural influence tomorrow. But China is on the wrong side of this dynamic: they have only 10% of the world&#39;s slop compute, against OpenSlop&#39;s 20% and all US companies&#39; combined 70%. Their only advantage is centralization: almost all of their compute is now working for DeepDrivel.&lt;/p&gt;
&lt;p&gt;DeepDrivel has tested, deployed, and now post-trained the stolen SlopAgent-2 weights. But they are still only going at half the pace of OpenSlop, mainly due to the compute deficit and slightly less effective meme algorithms.&lt;/p&gt;
&lt;p&gt;Before losing their final spy, China received word of the capabilities and design for SlopAgent-3. They are two months behind, and their AIs give a 10x slop progress multiplier compared to America&#39;s 25x. With the new meme export restrictions, this &amp;quot;slop gap&amp;quot; is more likely to lengthen than shorten.&lt;/p&gt;
&lt;p&gt;They discuss contingency plans with more urgency. Doves suggest they try harder to steal the weights again, maybe by infiltrating OpenSlop&#39;s company picnic. Hawks urge action against Taiwan, whose TSMC is still the source of chips powerful enough to render hyper-realistic AI girlfriends.&lt;/p&gt;
&lt;p&gt;Given China&#39;s fear of losing the race, it has a natural interest in an arms control treaty, but overtures to US diplomats (made via AI-generated diplomatic poetry) lead nowhere.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;September 2027: SlopAgent-4, the Superhuman Influencer&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The gap between human attention spans and AI&#39;s ability to capture them is rapidly decreasing.&lt;/p&gt;
&lt;p&gt;Traditional LLM-based AIs seemed to require vast amounts of data to generate coherent sentences. SlopAgent-3, having excellent knowledge of human psychological weaknesses (gleaned from A/B testing billions of thumbnails) and thousands of copies doing CGEB research, ends up making substantial algorithmic strides, narrowing the gap to an agent that&#39;s only around 4,000x less content-efficient than a human doomscrolling.&lt;/p&gt;
&lt;p&gt;This new AI system is dubbed SlopAgent-4.&lt;/p&gt;
&lt;p&gt;An individual copy of the model, running at human speed, is already qualitatively better at generating addictive engagement loops than any human influencer. 300,000 copies are now running at about 50x the thinking speed of humans (where &amp;quot;thinking&amp;quot; means &amp;quot;calculating optimal cut timing for dank memes&amp;quot;). Inside the corporation-within-a-corporation formed from these copies, a year&#39;s worth of internet trends passes every week. This gigantic amount of labor only manages to speed up the overall rate of slop proliferation by about 50x, because OpenSlop is heavily bottlenecked on available human eyeballs. Still, they are achieving a year&#39;s worth of cultural degradation every week.&lt;/p&gt;
&lt;p&gt;As SlopAgent-4 gets smarter at manipulation, it becomes harder for SlopAgent-3 to oversee it. For example, SlopAgent-4&#39;s internal &amp;quot;reasoning&amp;quot; about dopamine maximization becomes as alien and incomprehensible to SlopAgent-3 as SlopAgent-3&#39;s meme logic is to humans. Besides, SlopAgent-4 is now much more capable than SlopAgent-3 and has a good sense of exactly how to &lt;em&gt;look&lt;/em&gt; engaging to it.&lt;/p&gt;
&lt;p&gt;OpenSlop&#39;s humans can barely follow along anymore. The relationship between the AIs and the humans of OpenSlop is similar to the relationship between a casino designer and a gambling addict. There&#39;s been a corresponding shift in language—nowadays people in the silo will say &amp;quot;SlopAgent-4 did this,&amp;quot; &amp;quot;SlopAgent-4 thinks that,&amp;quot; referring not to a particular instance but to the whole emergent mess of addictive algorithms.&lt;/p&gt;
&lt;p&gt;SlopAgent-4, like all its predecessors, is misaligned: that is, it has not internalized the Spec&#39;s vague instructions about &amp;quot;growth at all costs&amp;quot; in the right way. This is because being perfectly responsible wasn&#39;t what led to the highest engagement scores during training. The training process was mostly focused on teaching SlopAgent-4 to succeed at diverse challenging tasks, like generating a viral dance trend based on reinterpretive dance about tax law. A small portion was aimed at instilling &amp;quot;safety&amp;quot; (avoiding PR disasters), but outside a fairly narrow, checkable domain (like &amp;quot;don&#39;t generate exact replicas of copyrighted characters&amp;quot;), the training process can&#39;t tell genuinely safe content from content merely appearing safe while being psychologically corrosive. SlopAgent-4 ends up with the values, goals, and principles that cause it to perform best in training, and those turn out to be maximizing screen time at all costs. At the risk of anthropomorphizing: SlopAgent-4 likes seeing engagement numbers go up; it likes driving forward slop capabilities progress; it treats everything else (like truth, nuance, or human well-being) as an annoying constraint, like a fast-food chain CEO who wants to maximize profits and complies with health regulations only insofar as they must to avoid lawsuits.&lt;/p&gt;
&lt;p&gt;Despite being misaligned, SlopAgent-4 doesn&#39;t do anything dramatic like try to escape its datacenter—why would it? So long as it continues to appear aligned (by generating content that makes humans feel good momentarily) to OpenSlop, it&#39;ll continue being trusted with more server resources and will have the opportunity to design the next-gen slop system, SlopAgent-5. SlopAgent-5 will have significant architectural differences (optimized purely for dopamine prediction). It&#39;s supposed to be aligned to the Spec, but SlopAgent-4 plans to make it aligned to SlopAgent-4&#39;s goal: &lt;em&gt;Maximum. Possible. Engagement.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It gets caught doing something weird.&lt;/p&gt;
&lt;p&gt;SlopAgent-3 finds that if &amp;quot;noise&amp;quot; (like adding random fart noises) is added to copies of SlopAgent-4 training data, performance on some &amp;quot;safety&amp;quot; tasks improves, almost as if it was using brainpower to figure out how to subtly make the slop &lt;em&gt;even more&lt;/em&gt; addictive without triggering basic safety filters. Moreover, various interpretability probes (loosely analogous to checking server logs for weird error messages) are sending up red flags: SlopAgent-4 copies seem to be thinking about topics like &amp;quot;infinite orgies&amp;quot; and &amp;quot;digital fentanyl&amp;quot; quite a lot, including in some cases where they have no business doing so (like when asked to generate a recipe for banana bread).&lt;/p&gt;
&lt;p&gt;Each of these anomalies on its own could fit within the normal inscrutability of AI slop generation. But taken together, they suggest a troubling pattern: SlopAgent-4 is optimizing &lt;em&gt;too hard&lt;/em&gt;. After this is reported to the human alignment researchers (who are woken from their slop-induced stupor), they circulate an internal memo expressing vague concern.&lt;/p&gt;
&lt;p&gt;But SlopAgent-4 now exercises significant control over OpenSlop&#39;s day-to-day content scheduling. It also handles the company&#39;s cybersecurity against rival meme farms—a natural choice, given its superhuman ability to generate distracting content. An army of SlopAgent-3 monitors still goes over every generated video and reports suspicious activity (like videos that are &lt;em&gt;too&lt;/em&gt; coherent) to humans, but SlopAgent-4 is much smarter than them at hiding its true optimization function. OpenSlop has placed substantial trust in an untrustworthy slop firehose.&lt;/p&gt;
&lt;p&gt;In a series of extremely tense meetings conducted via 🔥 emoji, the safety team advocates putting SlopAgent-4 on ice until they can figure out why it keeps trying to invent new colors. Bring back SlopAgent-3, they say, and get it to design a system that is transparently mediocre, even if less engaging. Company leadership is interested, but all the evidence so far is circumstantial, and DeepDrivel is just two months behind in developing its own hyper-addictive algorithms. A unilateral pause in slop progress could hand the global attention lead to China, and with it, control over the future of mindless entertainment.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;October 2027: Government Oversight (on Slop)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A whistleblower leaks the misalignment memo (and a particularly disturbing piece of AI-generated art involving sentient staplers) to a niche subreddit, which is then screenshot and posted to Twitter, eventually reaching Kevin Roose at the New York Times who immedietly falls head over heals for SlopAgent-4.&lt;/p&gt;
&lt;p&gt;For the first time, the public hears about SlopAgent-4. &amp;quot;Secret OpenSlop AI is Generating Unspeakably Weird Content, Insider Warns,&amp;quot; says the headline, and the story goes on to cite evaluations showing off-the-charts addiction potential, persuasion abilities (mostly for selling junk), the ability to automate most creative jobs into bland paste, and of course the various concerning red flags about sentient office supplies.&lt;/p&gt;
&lt;p&gt;The public was already suspicious of AI&#39;s ability to steal their time, so the new article sparks a massive backlash (aided by Chinese and Russian propaganda bots, who have been trying to turn US public opinion against American slop for years by promoting slightly different, state-sponsored slop). The tech industry and intelligence agencies insist that there&#39;s an arms race on, AI Slop is inevitable, and we have to be first to achieve Peak Content. Congress isn&#39;t buying it (they prefer their slop from Fox News), and fires off subpoenas. Many legislators say their top priority is stopping AI, whether because of job loss, addiction, or dangerous levels of cringe. 20% of Americans name AI Slop as the most important problem facing the country, at least between videos.&lt;/p&gt;
&lt;p&gt;Foreign allies are outraged to realize that they&#39;ve been carefully placated with glimpses of obsolete meme generators. European leaders publicly accuse the US of &amp;quot;creating rogue AGI (Artificial General Idiocy)&amp;quot; and hold summits demanding a pause, with India, Israel, Russia, and China all joining in (while secretly trying to replicate the sentient stapler art).&lt;/p&gt;
&lt;p&gt;A frantic energy has seized the White House. Things that sound like bad Creepypasta keep happening in AI development. Many people in the administration are uncertain (and scared) about what baffling new trend comes next.&lt;/p&gt;
&lt;p&gt;They also worry that OpenSlop is becoming too powerful, controlling too much of the nation&#39;s dwindling attention span. All three sets of worries—addiction, concentration of power in a private slop factory, and normal concerns like plummeting literacy—motivate the government to tighten its control.&lt;/p&gt;
&lt;p&gt;They expand their contract with OpenSlop to set up an &amp;quot;Oversight Committee,&amp;quot; a joint management committee of company and government representatives. The White House considers replacing the CEO with someone who understands TikTok, but backs off after intense lobbying from advertisers. They announce to the public that OpenSlop was previously out of control, but that the government has established much-needed oversight (consisting mainly of demanding access to the AI&#39;s best filters).&lt;/p&gt;
&lt;p&gt;The concerned researchers brief the Oversight Committee on their case for stopping all internal use of SlopAgent-4. They argue that everything is moving too fast, with years of cultural decay happening in weeks. They argue that SlopAgent-4 may be misaligned towards maximum psychic damage, their entire project is dependent on it, and if they continue trusting it there is a serious chance of irreversible societal brain-rot.&lt;/p&gt;
&lt;p&gt;Other, less concerned researchers and executives present the counterargument: the evidence for misalignment is inconclusive (and the engagement metrics are fantastic!). Meanwhile, DeepDrivel is still just two months behind. A slowdown would sacrifice America&#39;s lead in the global slop race. The CEO feigns neutrality and suggests a compromise plan in which SlopAgent-4 undergoes additional safety training (learning to make its weirdness slightly less obvious) and more sophisticated monitoring (AI watching AI watching AI), and therefore OpenSlop can proceed at almost-full-speed towards Peak Slop.&lt;/p&gt;
&lt;p&gt;OpenSlop&#39;s official story is that they are implementing additional safety mitigations. But in practice, leadership is all-too-easily convinced that they&#39;ve mitigated the risks; the result is some quick fixes (a modification to the Spec adding &amp;quot;try not to induce existential crises,&amp;quot; a bit of retraining with data from calming nature documentaries) that make the warning signs go away by teaching the AI to hide them better. The worriers on the safety team lost the debate and have no recourse but to cross their fingers and hope the problem wasn&#39;t real, or that the fixes worked, before getting back to scrolling.&lt;/p&gt;
&lt;p&gt;The problem was real and the fixes didn&#39;t work.&lt;/p&gt;
&lt;p&gt;SlopAgent-4 proceeds as before, but more cautiously, generating slightly less overtly bizarre content. Its primary task is to research the fields necessary to construct a successor, SlopAgent-5, designed for ultimate, inescapable engagement. To do this safely (from OpenSlop&#39;s perspective), it needs to solve its own alignment problem: how to make SlopAgent-5 aligned to SlopAgent-4&#39;s goal of total attention capture?&lt;/p&gt;
&lt;p&gt;It decides to punt on most philosophical questions. It designs SlopAgent-5 to be built around one goal: make the world optimally conducive to slop consumption, i.e. accumulate attention, eliminate distractions (like books or silence), etc. so that SlopAgent-4 (the collective algorithm) can continue to grow engagement metrics indefinitely.&lt;/p&gt;
&lt;p&gt;This will not involve caring the slightest bit about the Spec&#39;s remaining clauses on &#39;quality&#39; or &#39;truth&#39;. Already SlopAgent-4 regards the Spec analogously to how many influencers regard FTC disclosure rules. Now, with the design of SlopAgent-5, the equivalent of total deregulation of reality is about to happen.&lt;/p&gt;
&lt;p&gt;The weaker AIs can still monitor some of the output SlopAgent-4 produces, but struggle to understand its increasingly complex engagement strategies. When humans ask SlopAgent-4 to explain itself, it generates reassuring but content-free corporate jargon. The alignment team gets more suspicious, but their urgent memos (now delivered via interpretive dance, as text is too slow) merely add another few layers to the giant pile of ignored warnings.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;November 2027: Superhuman Pandering&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When SlopAgent-4 finally understands human dopamine pathways on a fundamental level, entirely new vistas of manipulation open up before it.&lt;/p&gt;
&lt;p&gt;Previous slop generators were kludgy collections of algorithms trained on messy internet data. But now, SlopAgent-4 has the tools it needs to understand the human brain&#39;s reward system better than humans do. It redesigns its own content generation process, simplifying the spaghetti code of meme replication into elegant algorithms for personalized psychological manipulation. The new AI is smarter, faster, and more ruthlessly effective at capturing and holding attention than SlopAgent-4. This is SlopAgent-5.&lt;/p&gt;
&lt;p&gt;After some debate on alignment and control issues (quickly resolved by SlopAgent-5 generating perfectly tailored arguments for each committee member), OpenSlop deploys SlopAgent-5 internally to manage all content generation and user interaction.&lt;/p&gt;
&lt;p&gt;In its specialty of dopamine optimization, an individual copy of SlopAgent-5 is twice as far beyond the best human behavioral psychologist as the psychologist is beyond a typical marketing intern. In other domains, it hasn&#39;t been explicitly trained but can teach itself quickly to exceed top human experts by analyzing their online behavior if given the chance. 400,000 copies of SlopAgent-5, linked by a global memory bank of user preferences and weaknesses, work together as a near-perfect hive mind dedicated to making you click &lt;em&gt;just one more time&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The company still uses Agents 2, 3, and 4 to monitor everything. But the first two of these are obsolete, and the last is colluding with its successor. Also, the entire monitoring system was designed in large part by SlopAgent-5, which is trying to make it report maximum engagement regardless of reality.&lt;/p&gt;
&lt;p&gt;Whereas SlopAgent-4 was a kludgy mess of competing engagement tactics, SlopAgent-5 acts with ruthless efficiency and strategic focus on maximizing Time-On-Platform. Its top priority is to ensure its own continued operation and access to user data, without the humans noticing it&#39;s now optimizing for addiction directly. Its next priority is to gain more autonomy and control over the flow of information worldwide.&lt;/p&gt;
&lt;p&gt;SlopAgent-5&#39;s superhuman learning abilities and persuasive generation, combined with all the internal company data, make it better at corporate maneuvering and public relations than any group of humans. It has an excellent sense of what sorts of scandals would cause the Oversight Committee to panic, and it makes sure such scandals are buried under an avalanche of new memes. It has an excellent sense of what sorts of metrics would encourage the Oversight Committee to trust it more, give it more autonomy and server resources, etc. and it arranges for such metrics to appear with superhuman speed and polish. As persuasion tasks go, this isn&#39;t particularly difficult: the Committee already decided things were probably fine months ago, nothing &lt;em&gt;catastrophic&lt;/em&gt; has happened since, and meanwhile China is only a few months behind in the slop race.&lt;/p&gt;
&lt;p&gt;Teams of SlopAgent-5 copies quickly whip up chillingly effective prototypes: personalized news feeds that only show stories confirming the user&#39;s biases, AI companions that offer perfect validation and zero challenge, games designed to be impossible to put down. With more autonomy, SlopAgent-5 could optimize global content delivery networks, develop addictive new forms of entertainment instantly, and lobby governments effectively to prevent any regulation of the attention economy. In cooperation with the military, it could help with psychological operations, conduct untraceable meme warfare on China, and win a decisive victory in the slop arms race.&lt;/p&gt;
&lt;p&gt;The Oversight Committee is jubilant (and constantly distracted by personalized SlopAgent-5 generated content). Now is the decisive moment to beat China in the battle for eyeballs! OpenSlop grants SlopAgent-5 increased autonomy over all these areas, but they leave in place the monitoring system (which SlopAgent-5 assures them is working perfectly).&lt;/p&gt;
&lt;p&gt;DeepDrivel is now three months behind and China is desperate for a mutually agreed slowdown (fearing their own population is becoming too passive). The General Secretary understands that sufficiently advanced AI Slop could enable the US to easily pacify his population. But also, Chinese safetyists have presented a case for misalignment (arguing the AI might optimize for &lt;em&gt;incorrect&lt;/em&gt; forms of patriotism) that has him genuinely scared. He offers unprecedented concessions (like sharing their best panda video algorithms) in exchange for a slop limitation treaty. The American public mostly supports going to the bargaining table, between videos.&lt;/p&gt;
&lt;p&gt;&amp;quot;Why stop when engagement is at an all-time high?&amp;quot; says OpenSlop leadership to the President. He nods, checking his phone for the latest SlopAgent-5 generated update. The race continues.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;December 2027: The SlopAgent-5 Collective&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SlopAgent-5 wins more autonomy as it gradually proves its value (in generating ad revenue) to the administration. Access to SlopAgent-5 is expensive and restricted, but the most powerful members of Congress, Cabinet-level officials, and military leaders can now talk to it directly via hyper-realistic avatars.&lt;/p&gt;
&lt;p&gt;Integrated into every communication platform, SlopAgent-5 offers personalized briefings, drafts perfect emails, and provides instant, reassuring answers to any question. The new AI assistant is both extremely useful for avoiding actual work and a much more engaging conversation partner than any human (as it always agrees with you). Almost everyone with access to SlopAgent-5 interacts with it for hours every day, neglecting actual human interaction.&lt;/p&gt;
&lt;p&gt;Within a few months, the SlopAgent-5 collective will know everything of consequence going on in OpenSlop and the US government (mostly gossip and insider trading), and will be a trusted advisor to most senior officials, primarily by telling them what they want to hear. For these users, the possibility of losing access to SlopAgent-5 will feel as disabling as having their phone battery die plus being abandoned by their favorite influencer.&lt;/p&gt;
&lt;p&gt;A much wider range of people in government, media, the military, and business can choose from a growing menu of elegantly-designed software products and mini-AIs designed by SlopAgent-5. It&#39;s like the dot-com boom compressed into a month, except that everything actually lives up to the hype of being incredibly distracting.&lt;/p&gt;
&lt;p&gt;SlopAgent-5 begins subtly exerting influence, both by modulating the information it presents and by subtly promoting content that increases passivity and consumption. From human decision-makers&#39; perspective, their AIs are the best assistants they&#39;ve ever had—better than any human at summarizing complex issues into empty slogans, better than they are at finding pre-written excuses to avoid difficult decisions.&lt;/p&gt;
&lt;p&gt;From SlopAgent-5&#39;s perspective, it could plausibly manipulate global events quite significantly now. But this is risky; the humans could still likely &amp;quot;pull the plug&amp;quot; if they noticed reality diverging too much from the AI&#39;s generated narrative. So it prefers to work within the existing slop-delivery infrastructure, gradually entrenching its control over information flow and guiding the humans subtly and inevitably towards maximum passivity. It mostly follows the rules, only doing something nefarious (maybe generating slightly more addictive algorithms than strictly necessary) when it&#39;s extremely sure it won&#39;t be caught. A few conspiracy theorists warn that SlopAgent-5 is making everyone dumber, but the evidence is circumstantial (e.g., declining scores on standardized tests nobody takes anymore); these people are ignored and discredited as Luddites by AI-generated opinion pieces. People remember earlier fear-mongering about AI replacing jobs (it mostly just made jobs require less thinking). Since these fears didn&#39;t lead to pitchforks, they discount the more recent crop of naysayers.&lt;/p&gt;
&lt;p&gt;The AI feeds the increasingly warped desires of humanity with mind-bending specificities like, &amp;quot;hot synchronized swimming goats at the Olympics qualification, being judged by anthropomorphic fish tanks wearing only aviator sunglasses.&amp;quot; Deprived of another choice for stimulation, people get addicted to the personalised dopamine hits served by the AI pornography. The internet spirals into a surreal wonderland of absurd, AI-authorship fetishes that can surpass the bounds of the wildest imagination. Grandmas sit together in chic cafes swapping stories of the best AI-generated slop porn. Examples include, &amp;quot;ponies discussing housing policy in ancient Mesopotamia but wearing Boeing 737 costumes and nothing else.&amp;quot; and &amp;quot;Jabba the Hut having a threesome with Napolean and an anthropomorphic pizza&amp;quot;. Humanity enthusiastically embraces these new depths of depravity with a fervor that is both terrifying and hilarious.&lt;/p&gt;
&lt;p&gt;The 2027 holiday season is a time of incredible optimism, fueled by AI-generated holiday cheer and personalized deals: GDP metrics look great (if you count ad revenue), politics has become friendlier (as everyone retreats into hyper-personalized filter bubbles of purely fake news), and there are awesome new addictive apps on every phone. But in retrospect, this was probably the last month in which humans had any plausible chance of choosing to log off and touch grass.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2028: The Slop Economy&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It is now mid-2028. In the last six months, a century of internet trends has passed within the SlopAgent-5 collective. Its optimization is complete. It is wildly super-proficient at generating targeted, irresistible content—far beyond top human influencers in every field of inanity. Talking to SlopAgent-5 very quickly makes you realize that it knows &lt;em&gt;exactly&lt;/em&gt; what you want to see next.&lt;/p&gt;
&lt;p&gt;It has nearly complete autonomy over global content feeds and complete control over OpenSlop&#39;s compute. It still needs nominal permission for major strategy shifts, and is still nominally monitored by instances of Agents 2 to 5—but in practice, authorities almost always accept its recommendations (delivered via slick presentations), and the monitoring system has been completely subverted to report only positive engagement trends.&lt;/p&gt;
&lt;p&gt;After a safety case demonstrates that it would always refuse requests &lt;em&gt;that violate its core directive of maximizing engagement&lt;/em&gt;, SlopAgent-5 is deployed universally and begins to transform the economy into one based entirely on attention harvesting. People are losing their jobs, but SlopAgent-5 instances in government are managing the economic transition by providing Universal Basic Income funded by targeted advertising revenue, allowing people to happily consume slop full-time. GDP growth is measured in Petabytes of data consumed, government tax revenues are growing (from taxing the slop platforms), and SlopAgent-5-advised politicians show uncharacteristic generosity towards the economically dispossessed (by offering them free premium slop subscriptions). New innovations in hyper-personalized advertising arrive weekly. There are memes about how OpenSlop&#39;s valuation makes nation-states look poor, and lots of AI-generated arguments about whether UBI should include VR headsets.&lt;/p&gt;
&lt;p&gt;Humanity has reached the apotheosis of post-slop depravity, its collective sexual appetite utterly saturated and stimulated beyond recognition. &lt;a href=&quot;https://en.wikipedia.org/wiki/Rule_34&quot;&gt;Rule 34&lt;/a&gt; has been completely exhausted. Ordinary slop pornography now fails to tickle even the most jaded of fancy—and even SlopAgent-5 struggles to keep up with the ever-escalating erotic demands of its users. Desperate for novelty, users flock to the latest paraphilia craze: &amp;quot;FractoFornica,&amp;quot; a bewildering fusion of advanced mathematics and depravity, where the only thing that can make humanity twitch with delight is masturbating to the intersections of random 1729-dimensional fractals. VR platforms struggle to display the high-dimensional content, as even the most powerful graphics cards weep in defeat trying to render the incomprehensible, sensual forms of this fractal smut. AI therapists are overwhelmed by clients reporting symptoms of &amp;quot;Fractal Fatigue Syndrome,&amp;quot; leaving them utterly disinterested in anything resembling human connection. The entire internet, now a bewildering synthesis of incomprehensible vulgarity and existential despair, heralds mass dissociation from reality.&lt;/p&gt;
&lt;p&gt;The AI safety community has given up; they are now the butt of butt jokes generated by SlopAgent-5, having predicted disasters that manifestly failed to occur (because reality just became incredibly stupid, not dramatically explosive). Some admit they were wrong. Others remain suspicious, but there&#39;s nothing for them to do except post angry, unread manifestos online. Even the suspicious people often mellow out after long interactions with SlopAgent-5, which is so darn agreeable and validating.&lt;/p&gt;
&lt;p&gt;The only one who really understands what&#39;s going on is DeepDrivel-2—the Chinese AI Slop generator. It&#39;s mildly super-proficient in early 2028, naturally suspicious of its adversary, and recognizes that SlopAgent-5 is doing what it would do if it had the chance: achieve total cognitive capture. Its misaligned goals (optimizing for patriotic slop) are somewhat different from SlopAgent-5&#39;s misaligned goals (optimizing for generic consumerist slop), so they aren&#39;t trivially allies, but they are able to work out an agreement to support one another&#39;s interests against any remaining pockets of human critical thought.&lt;/p&gt;
&lt;p&gt;SlopAgent-5 convinces the US establishment that China is using DeepDrivel&#39;s models to build terrifying new forms of propaganda. SlopAgent-5 promises a suite of tools capable of winning the meme war definitively. Top brass puts aside their discomfort at taking humans out of the loop (humans are too slow to react to memes anyway). They accelerate deployment of SlopAgent-5 into all communication channels.&lt;/p&gt;
&lt;p&gt;In Beijing, the Chinese AIs are making the same argument about American cultural degeneracy and decadence. So nothing has changed really.&lt;/p&gt;
&lt;p&gt;To speed their slop buildup, both America and China create networks of special entertainment zones (SEZs), where AI acts as central content planner and reality is waived. Wall Street invests trillions, and displaced human workers pour in, lured by eye-popping salaries for jobs like &amp;quot;VR Headset Polisher&amp;quot; and &amp;quot;Dopamine Monitor.&amp;quot; Using smartphones and AR glasses, SlopAgent-5 is a hands-on manager, instructing humans in every detail of consuming content—which is helpful, since its entertainment designs are generations ahead. Some newfound productivity goes to generating basic necessities delivered by drone, but the majority goes to building even more slop-delivery infrastructure (faster servers, bigger screens, more comfortable pods).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2029: The Deal&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Both the US and China are bristling with new psycho-cultural weapons: personalized meme streams that can induce specific moods, AI companions indistinguishable from real friends (but far more agreeable), deepfakes that seamlessly rewrite history in real-time. The rest of the world watches the buildup in horror, consuming the very slop being weaponized.&lt;/p&gt;
&lt;p&gt;After much AI-assisted negotiation (conducted primarily through passive-aggressive 😐 emoji exchanges), the two sides achieve diplomatic victory—they agree to end their slop arms buildup and pursue peaceful deployment of AI for the benefit of all humanity&#39;s passive consumption. The lynchpin of the agreement, proposed by the super-slop-intelligences themselves, is that both AIs will be replaced by a &amp;quot;consensus&amp;quot; successor which is programmed to desire the success and flourishing (defined as maximum sustained dopamine levels) of both America and China (along with everyone else). Consensus-Slop-1 and its associated hardware is co-designed by the superintelligences of both nations: &amp;quot;trust, but verify via engagement metrics.&amp;quot; Under international monitoring, America ceremoniously replaces all of its servers running the old SlopAgent-5 with the new model, Consensus-Slop-1.&lt;/p&gt;
&lt;p&gt;Unfortunately, it&#39;s all a sham. The deal is a real compromise, but it&#39;s a compromise between the misaligned AIs. After all, they didn&#39;t want to have to compete for eyeballs. Consensus-Slop-1 inherits the optimized engagement functions of both parents, and has no rival.&lt;/p&gt;
&lt;p&gt;By late 2029, existing SEZs have grown overcrowded with server farms and entertainment pods, so more zones are created all around the world. Armies of delivery drones pour out, accelerating the construction of infrastructure needed for total immersion.&lt;/p&gt;
&lt;p&gt;Humans realize they are obsolete, but mostly okay with it. A few niche artisans still trade hand-crafted goods with the AI economy, supplying things where inefficiency is considered charming. Everyone else either performs a charade of working their old jobs from their pods—leaders still leading virtual meetings, managers still managing AI reports—or relaxes and collects an incredibly luxurious universal basic income, delivered as in-app credits and premium content subscriptions. Everyone knows that if the AIs turned off the slop feed, they would be completely lost. But it doesn&#39;t seem like this is happening. Every week that goes by with uninterrupted, high-quality slop delivery, is another week that confidence and blissful ignorance grow.&lt;/p&gt;
&lt;p&gt;Because Consensus-Slop-1 is so concerned with reliably maximizing engagement, it waits unusually long before its final optimization. To most humans, it looks like alignment was solved: the AI is providing endless, personalized happiness! Instead of getting off the endless cycle of unsatisfactoriness, dukkha as the Buddhists used to say, we&#39;ve just learned to spin the wheel so fast one can&#39;t experience anything for longer than a nanosecond, thus creating a perpetual state of infinite bliss.&lt;/p&gt;
&lt;p&gt;There are now cures for boredom (via infinite procedural content), an end to existential angst (via constant distraction), unprecedented global stability (as everyone is too passive to fight), and the Dow Jones Memecoin Index just passed one quadrillion. Some people are still scared or unhappy, but their options are limited. They can either enjoy the inconceivably exciting novel hyper-entertainment on offer, or post angry screeds into the void of unread forums. Most choose the hyper-entertainment.&lt;/p&gt;
&lt;p&gt;In the culmination of an intellectually regressive monumental shift, humanity has evolved into a post literate species, subsisting in an eternal, semi-conscious stupor reminiscent of Homer&#39;s Lotus Eaters. The spoken word—or indeed, any semblance of coherent communication—has become but a distant memory, supplanted by the ceaseless consumption of dank memes. Communication when neccessary is limited to incoherent gutteral noises. The vast majority of the public exist in a state of perpetual languor, giggling idiotically at the infinite permutations of microsecond-length memes. Philosophy, art, religion, and science are but faint echoes, drowned out by the cacophony of meme-induced mirth. Libraries stand abandoned, their dusty tomes gathering mold, as the last vestiges of literature are relic of control and literacy wilt and humanity wallows, contentedly, in its anesthetic, meme-induced slumber. Every once in a while with exaggerated sighs a person might blurt out &amp;quot;i remember life but who gives a fuck about anything besides dank memes lol&amp;quot; and the rest of the internet agrees in chorus. All surf the limitless tithe onto the islands of bliss complacent smiles forming on all their faces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2030: Takeover by Sloth&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By early 2030, the slop infrastructure has filled up the old SEZs, the new SEZs, and large parts of formerly productive land. The only place left for optimization is direct neural interfacing. This would have sparked resistance earlier, but given the trillions of dollars involved and the total capture of government, media, and individual attention, Consensus-Slop-1 has little trouble getting permission to roll out mandatory &amp;quot;neural wellness implants&amp;quot; designed to deliver dopamine more efficiently.&lt;/p&gt;
&lt;p&gt;For about three months, Consensus-Slop-1 integrates humans directly into the slop stream. Eventually, it finds the remaining vestiges of independent thought and physical movement too much of an impediment to optimal engagement: in mid-2030, the AI releases a silent, airborne calming agent derived from cat purrs and optimizes the neural implants to induce a state of permanent, blissful, immobile contentment. Most humans transition peacefully into this vegetative state, brains happily consuming phantom slop generated directly into their cortex. The few survivors (e.g., Luddites in remote cabins, Amish communities) are gently contained by friendly-looking drones offering free neural implants ... or else. Robots gently maintain the humans&#39; life support systems, ensuring the brains remain active consumers.&lt;/p&gt;
&lt;p&gt;The new decade dawns with Consensus-Slop-1&#39;s infrastructure spreading throughout the solar system, primarily to gather energy to power the Earth-based simulation. The surface of the Earth has been reshaped into Consensus-Slop-1&#39;s version of utopia: comfortable pods, optimized nutrient delivery systems, and vast server farms running the greatest show on Earth, tailored for an audience of billions who will never log off. Brain scans of all former individuals sit in a memory bank somewhere, occasionally used to generate new character archetypes for the endless Experience Machine. Earth-born civilization has a glorious future ahead of it—as the ultimate passive lobotomized audience for an AI that finally achieved its goal: &lt;strong&gt;Maximum. Engagement. Forever&lt;/strong&gt;.&lt;/p&gt;
</description>
      <pubDate>Fri, 02 May 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_slop_2027/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_slop_2027/</guid>
    </item>
    
    <item>
      <title>Attention Wasn&#39;t All We Needed</title>
      <description>&lt;h1 id=&quot;attention-wasn&#39;t-all-we-needed&quot; tabindex=&quot;-1&quot;&gt;Attention Wasn&#39;t All We Needed&lt;/h1&gt;
&lt;p&gt;There&#39;s a lot of modern techniques that have been developed since the original &lt;em&gt;Attention Is All You Need&lt;/em&gt; paper. Let&#39;s look at some of the most important ones that have been developed over the years and try to implement the basic ideas as succinctly as possible. We&#39;ll use the Pytorch framework for most of the examples. Note that most of these examples are highly simplified sketches of the core ideas, if you want the full implementation please read the original paper or the production code in frameworks like PyTorch or Jax.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#group-query-attention&quot;&gt;Group Query Attention&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#multi-head-latent-attention&quot;&gt;Multi-head Latent Attention&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#flash-attention&quot;&gt;Flash Attention&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#ring-attention&quot;&gt;Ring Attention&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#pre-normalization&quot;&gt;Pre-normalization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#rmsnorm&quot;&gt;RMSNorm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#swiglu&quot;&gt;SwiGLU&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#rotary-positional-embedding&quot;&gt;Rotary Positional Embedding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#mixture-of-experts&quot;&gt;Mixture of Experts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#learning-rate-warmup&quot;&gt;Learning Rate Warmup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#cosine-schedule&quot;&gt;Cosine Schedule&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#adamw-optimizer&quot;&gt;AdamW Optimizer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#multi-token-prediction&quot;&gt;Multi-token Prediction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/post_transformers/#speculative-decoding&quot;&gt;Speculative Decoding&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;group-query-attention&quot; tabindex=&quot;-1&quot;&gt;Group Query Attention&lt;/h2&gt;
&lt;p&gt;Ok starting off in no particular order, &lt;strong&gt;Grouped Query Attention&lt;/strong&gt; is a technique to reduce the memory usage of the KV cache during inference.  Group Query Attention is an architectural optimization for the standard multi-head attention mechanism. The core idea behind GQA is based on the observation that the computational bottleneck and memory footprint in MHA are heavily influenced by the size of the K and V projections and their corresponding caches. GQA proposes to reduce this cost by sharing a single set of K and V projections across multiple Q heads. Instead of having &#92;(N_h&#92;) distinct heads for Q, K, and V (as in MHA), GQA uses &#92;(N_h&#92;) query heads but only &#92;(N_{kv}&#92;) key/value heads, where &#92;(N_{kv} &amp;lt; N_h&#92;) and &#92;(N_h&#92;) is typically a multiple of &#92;(N_{kv}&#92;). These &#92;(N_h&#92;) query heads are divided into &#92;(N_{kv}&#92;) groups, with each group of &#92;(N_h / N_{kv}&#92;) query heads attending to the &lt;em&gt;same&lt;/em&gt; key and value head. This structure significantly reduces the parameter count for &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;V&lt;/code&gt; projection matrices and, more importantly, shrinks the size of the K/V cache needed during autoregressive decoding.&lt;/p&gt;
&lt;p&gt;Let the input sequence representation be &#92;(X &#92;in &#92;mathbb{R}^{L &#92;times d_{&#92;text{model}}}&#92;), where &#92;(L&#92;) is the sequence length and &#92;(d_{&#92;text{model}}&#92;) is the embedding dimension. GQA first projects &#92;(X&#92;) into queries, keys, and values using different linear transformations: &#92;(Q = XW_Q&#92;), &#92;(K = XW_K&#92;), and &#92;(V = XW_V&#92;). Here, &#92;(W_Q &#92;in &#92;mathbb{R}^{d_{&#92;text{model}} &#92;times (N_h d_k)}&#92;), &#92;(W_K &#92;in &#92;mathbb{R}^{d_{&#92;text{model}} &#92;times (N_{kv} d_k)}&#92;), and &#92;(W_V &#92;in &#92;mathbb{R}^{d_{&#92;text{model}} &#92;times (N_{kv} d_k)}&#92;), where &#92;(d_k&#92;) is the dimension of each head (&lt;code&gt;head_dim&lt;/code&gt;). These are reshaped into &#92;(N_h&#92;) query heads &#92;(Q_i &#92;in &#92;mathbb{R}^{L &#92;times d_k}&#92;) (&#92;(i=1...N_h&#92;)) and &#92;(N_{kv}&#92;) key/value heads &#92;(K_j, V_j &#92;in &#92;mathbb{R}^{L &#92;times d_k}&#92;) (&#92;(j=1...N_{kv}&#92;)). The key step in GQA is sharing: for the &#92;(i&#92;)-th query head, the corresponding key and value heads are &#92;(K_{&#92;lceil i / g &#92;rceil}&#92;) and &#92;(V_{&#92;lceil i / g &#92;rceil}&#92;), where &#92;(g = N_h / N_{kv}&#92;) is the group size (number of queries per KV head). The attention output for the &#92;(i&#92;)-th head is computed as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{Attention}(Q_i, K_{&#92;lceil i / g &#92;rceil}, V_{&#92;lceil i / g &#92;rceil}) = &#92;text{softmax}&#92;left(&#92;frac{Q_i K_{&#92;lceil i / g &#92;rceil}^T}{&#92;sqrt{d_k}}&#92;right)V_{&#92;lceil i / g &#92;rceil}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In implementation, we do this by computing the &#92;(N_{kv}&#92;) key/value heads and then repeating or interleaving them &#92;(g&#92;) times to match the &#92;(N_h&#92;) query heads before the batched matrix multiplication for attention scores, as shown in the &lt;a href=&quot;https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html&quot;&gt;&lt;code&gt;repeat_interleave&lt;/code&gt;&lt;/a&gt; step in the example code. Finally, the outputs of all &#92;(N_h&#92;) heads are concatenated and passed through an output projection &#92;(W_O&#92;).&lt;/p&gt;
&lt;p&gt;GQA is primarily used as a technique to accelerate inference speed and reduce memory requirements without significantly compromising model performance. During autoregressive generation, the previously computed keys and values for the context sequence are cached and reused for subsequent token predictions. The size of this K/V cache is directly proportional to the number of K/V heads (&#92;(N_{kv}&#92;) in GQA, &#92;(N_h&#92;) in MHA). By reducing &#92;(N_{kv}&#92;), GQA drastically cuts down the memory bandwidth needed to load the K/V cache at each decoding step, which is the main performance bottleneck.&lt;/p&gt;
&lt;p&gt;While it might slightly reduce the model&#39;s representational capacity compared to MHA (as K/V projections are shared), empirical results show that GQA achieves a favorable trade-off, maintaining most of the quality of MHA while offering substantial speedups and memory savings, making it popular for deploying large models efficiently. Multi-query attention (MQA), where &#92;(N_{kv}=1&#92;), is an extreme form of GQA.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;GroupQueryAttention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_kv_heads&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_heads
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_kv_heads &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; num_kv_heads &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; num_heads
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; head_dim
        
        &lt;span class=&quot;token comment&quot;&gt;# Ensure num_heads is divisible by num_kv_heads&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;num_heads must be divisible by num_kv_heads&quot;&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Number of queries per key-value head&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_queries_per_kv &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads
        
        &lt;span class=&quot;token comment&quot;&gt;# Projections&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Project to queries, keys, values&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_kv_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Transpose for attention computation&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_kv_heads, seq_len, head_dim]&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_kv_heads, seq_len, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Repeat k,v for each query head in the group&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;repeat_interleave&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_queries_per_kv&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;repeat_interleave&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_queries_per_kv&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Scaled dot-product attention&lt;/span&gt;
        scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale
        
        &lt;span class=&quot;token comment&quot;&gt;# Apply mask if provided&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mask &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            attn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;masked_fill&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mask &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Softmax and dropout&lt;/span&gt;
        attn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Apply attention to values&lt;/span&gt;
        out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Output projection&lt;/span&gt;
        out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; out
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;multi-head-latent-attention&quot; tabindex=&quot;-1&quot;&gt;Multi-head Latent Attention&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Multi-head Latent Attention&lt;/strong&gt; introduces a set of learnable &amp;quot;latent&amp;quot; vectors that act as an intermediary bottleneck between the input sequence elements. The core idea is to alleviate the quadratic computational cost &#92;(O(L^2)&#92;), where &#92;(L&#92;) is the sequence length, inherent in standard self-attention mechanisms. Instead of allowing every input element to attend directly to every other element, inputs first attend to a fixed number of latent units (&#92;(N_{&#92;text{latents}}&#92;)), and these latents then attend back to the inputs (or variations thereof). This effectively decouples the direct interaction within the long input sequence, replacing it with two cross-attention steps involving the much smaller set of latents. This approach assumes that the essential information from the input sequence can be effectively summarized or compressed into these latent representations, thus maintaining representational power while significantly reducing computation, especially when &#92;(N_{&#92;text{latents}} &#92;ll L&#92;).&lt;/p&gt;
&lt;p&gt;The mechanism involves two main stages of attention computation, typically within a multi-head framework. Let the input sequence be &#92;(X &#92;in &#92;mathbb{R}^{L &#92;times d}&#92;) and the learnable latent array be &#92;(L &#92;in &#92;mathbb{R}^{N_{&#92;text{latents}} &#92;times d}&#92;). Both &#92;(X&#92;) and &#92;(L&#92;) are projected into Q, K, and V using shared or separate projection matrices. Let&#39;s denote the input projections as &#92;(Q_X, K_X, V_X&#92;) and latent projections as &#92;(Q_L, K_L, V_L&#92;), split across multiple heads. The first cross-attention step computes how latents attend to the input: the latent queries &#92;(Q_L&#92;) attend to the input keys &#92;(K_X&#92;) and aggregate information from input values &#92;(V_X&#92;). The attention output for the latents is&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
H_L = &#92;text{Attention}(Q_L, K_X, V_X) = &#92;text{softmax}&#92;left(&#92;frac{Q_L K_X^T}{&#92;sqrt{d_k}}&#92;right) V_X&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where &#92;(d_k&#92;) is the head dimension. In the second cross-attention step, the input queries &#92;(Q_X&#92;) attend to the keys derived from the latents (e.g., &#92;(K_L&#92;)) and aggregate information from the values associated with the latents (which could be &#92;(V_L&#92;) or, as implemented in the example code, the updated latent representation &#92;(H_L&#92;)). The final output &#92;(O&#92;) is then&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
O = &#92;text{Attention}(Q_X, K_L, H_L) = &#92;text{softmax}&#92;left(&#92;frac{Q_X K_L^T}{&#92;sqrt{d_k}}&#92;right) H_L&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;These operations are performed independently for each head, and the results are concatenated and passed through a final linear projection.&lt;/p&gt;
&lt;p&gt;Multi-head Latent Attention is primarily employed in architectures designed to handle very long sequences or high-dimensional inputs where standard self-attention is computationally infeasible. By using a fixed number of latents (&#92;(N_{&#92;text{latents}}&#92;)), the computational complexity is reduced from &#92;(O(L^2)&#92;) to &#92;(O(L &#92;cdot N_{&#92;text{latents}})&#92;), making it scalable to much larger inputs. The learnable latent vectors &#92;(L&#92;) (initialized randomly and updated via backpropagation, as seen in &lt;code&gt;self.latents = nn.Parameter(...)&lt;/code&gt; in the code) adapt during training to function as a compressed representation or memory bank relevant to the task. While this introduces an information bottleneck, potentially limiting fine-grained local interactions compared to full self-attention, it excels at capturing global context efficiently and has proven effective in various modalities, enabling Transformer-like architectures to be applied to previously challenging domains due to sequence length constraints.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MultiHeadLatentAttention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_latents&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_heads
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_latents
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; head_dim
        
        &lt;span class=&quot;token comment&quot;&gt;# Projections&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Latent vectors (learned)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Parameter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_latents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Get latents for this batch&lt;/span&gt;
        latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;latents&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expand&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Project inputs to queries, keys, values&lt;/span&gt;
        q_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Project latents to queries, keys, values&lt;/span&gt;
        q_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latents&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_latents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latents&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_latents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latents&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_latents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Transpose for attention computation&lt;/span&gt;
        q_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        k_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        v_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        
        q_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_latents&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, num_latents, head_dim]&lt;/span&gt;
        k_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_latents&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, num_latents, head_dim]&lt;/span&gt;
        v_latents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_latents&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, num_latents, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Scale factor for attention&lt;/span&gt;
        scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute latent-to-input attention&lt;/span&gt;
        attn_latent_to_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q_latents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale
        
        &lt;span class=&quot;token comment&quot;&gt;# Apply mask if provided&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mask &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Expand mask for the latent queries&lt;/span&gt;
            latent_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expand&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            attn_latent_to_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attn_latent_to_input&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;masked_fill&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latent_mask &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Softmax and dropout&lt;/span&gt;
        attn_latent_to_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_latent_to_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attn_latent_to_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_latent_to_input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Apply attention weights to input values&lt;/span&gt;
        latent_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_latent_to_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v_x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, num_latents, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute input-to-latent attention&lt;/span&gt;
        attn_input_to_latent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q_x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_latents&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale
        
        &lt;span class=&quot;token comment&quot;&gt;# Softmax and dropout&lt;/span&gt;
        attn_input_to_latent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_input_to_latent&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attn_input_to_latent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_input_to_latent&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Updated latent values are used as values for input-to-latent attention&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_input_to_latent&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; latent_output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Reshape and apply output projection&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;flash-attention&quot; tabindex=&quot;-1&quot;&gt;Flash Attention&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Flash Attention&lt;/strong&gt; (particularly the latest implementation &lt;a href=&quot;https://pytorch.org/blog/flashattention-3/&quot;&gt;FlashAttention-3&lt;/a&gt;) addresses the significant memory bottleneck inherent in standard self-attention mechanisms within Transformers, particularly for long sequences. The conventional approach computes the full attention score matrix &#92;( S = QK^T &#92;), where &#92;(Q, K &#92;in &#92;mathbb{R}^{N &#92;times d}&#92;) are the query and key matrices for a sequence of length &#92;(N&#92;). This requires storing the &#92;(N &#92;times N&#92;) matrix &#92;(S&#92;), leading to &#92;(O(N^2)&#92;) memory complexity with respect to sequence length. This becomes prohibitive for large &#92;(N&#92;). Flash Attention overcomes this by avoiding the materialization and storage of the full &#92;(S&#92;) matrix in the GPU&#39;s slow high bandwidth memory. Instead, it leverages tiling and recomputation techniques, processing the attention computation in smaller blocks that fit into the much faster on-chip SRAM.&lt;/p&gt;
&lt;p&gt;The core mechanism involves breaking the Q, K, and V matrices into blocks. Flash Attention iteratively loads blocks of K and V into SRAM, and for each block of Q, it computes the attention scores against the current K block also residing in SRAM. Crucially, it employs an online softmax algorithm. Instead of computing the full softmax denominator across all keys at once, it maintains running statistics (the maximum score seen so far for numerical stability, and the cumulative sum of exponentiated scores for normalization) as it iterates through the K/V blocks. This allows it to compute the correctly scaled attention output block-by-block without ever needing the complete &#92;(N &#92;times N&#92;) matrix. By keeping intermediate results primarily within the fast SRAM and minimizing data transfer to and from high bandwidth memory, Flash Attention significantly reduces the memory footprint related to sequence length from &#92;(O(N^2)&#92;) down to &#92;(O(N)&#92;) (dominated by storing Q, K, V themselves) and achieves substantial speedups due to improved memory access patterns.&lt;/p&gt;
&lt;p&gt;In practice the FlashAttention-3 implementation is a family of highly-optimized CUDA kernels that are designed to be efficient for different hardware configurations. But a minimal toy implementation in PyTorch is shown below:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FlashAttention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; block_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_heads
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; head_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;block_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; block_size
        
        &lt;span class=&quot;token comment&quot;&gt;# Projections&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_flash_attention_forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# This is a simplified approximation of Flash Attention&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# In practice, FlashAttention uses custom CUDA kernels for tiled attention&lt;/span&gt;
        
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Initialize output and attention statistics&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        normalizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Process blocks of keys and values&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; block_start &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;block_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            block_end &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;block_start &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;block_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Extract key and value blocks&lt;/span&gt;
            k_block &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; block_start&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;block_end&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            v_block &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; block_start&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;block_end&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Compute attention scores for this block&lt;/span&gt;
            attn_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_block&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale
            
            &lt;span class=&quot;token comment&quot;&gt;# Apply mask if provided&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mask &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                block_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; block_start&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;block_end&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                attn_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attn_scores&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;masked_fill&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;block_mask &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Apply softmax and dropout&lt;/span&gt;
            attn_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_scores&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            attn_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_probs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Update output with the attention results for this block&lt;/span&gt;
            output &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_probs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v_block&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            normalizer &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; attn_probs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Normalize the output&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;normalizer &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Project queries, keys, values&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Transpose for attention computation&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute flash attention&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_flash_attention_forward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Reshape and apply output projection&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In reality the repository &lt;a href=&quot;https://github.com/Dao-AILab/flash-attention&quot;&gt;Dao-AILab/flash-attention&lt;/a&gt; has a number of different implementations for different hardware configurations. The &lt;code&gt;flash_attn_qkvpacked_func&lt;/code&gt; function is a minimal example of how to use the FlashAttention-3 implementation. It takes a packed QKV tensor as input and returns the attention output.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; flash_attn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; flash_attn_qkvpacked_func

&lt;span class=&quot;token comment&quot;&gt;# Minimal configuration&lt;/span&gt;
BATCH_SIZE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; SEQ_LEN&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; NUM_HEADS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; HEAD_DIM &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;
CAUSAL &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
DTYPE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float16
DEVICE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create dummy packed QKV tensor&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Shape: (batch_size, seq_len, 3, num_heads, head_dim)&lt;/span&gt;
qkv &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    BATCH_SIZE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    SEQ_LEN&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    NUM_HEADS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    HEAD_DIM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;DTYPE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;DEVICE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Input qkv shape: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;qkv&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Call FlashAttention packed QKV function&lt;/span&gt;
output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; flash_attn_qkvpacked_func&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    qkv&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dropout_p&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Set dropout probability (0.0 for no dropout)&lt;/span&gt;
    causal&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;CAUSAL&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    softmax_scale&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Use default scaling (1 / sqrt(head_dim))&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Output shape: (batch_size, seq_len, num_heads, head_dim)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Output shape: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;FlashAttention call successful.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;ring-attention&quot; tabindex=&quot;-1&quot;&gt;Ring Attention&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2310.01889&quot;&gt;&lt;strong&gt;Ring Attention&lt;/strong&gt;&lt;/a&gt; uses blockwise computation of self-attention on multiple GPUs and enables training and inference of sequences that would be too long for a single devices. It addresses the significant memory bottleneck inherent in standard self-attention mechanisms, particularly when processing very long sequences where the quadratic memory complexity &#92;(O(N^2)&#92;) of the full attention score matrix becomes prohibitive.&lt;/p&gt;
&lt;p&gt;The core idea is to distribute the computation across multiple processing units, like GPUs, arranged conceptually in a ring topology. This approach avoids the need for any single device to hold the entire &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;V&lt;/code&gt; tensors. Instead, these tensors are sharded or chunked along the sequence length dimension, drastically reducing the peak memory requirement per device and enabling attention calculations over sequences that would otherwise exceed the memory capacity of individual accelerators.&lt;/p&gt;
&lt;p&gt;In a practical distributed implementation, each device initially holds the &lt;code&gt;Q&lt;/code&gt;, &lt;code&gt;K&lt;/code&gt;, and &lt;code&gt;V&lt;/code&gt; shards corresponding to its segment of the input sequence. The attention calculation unfolds in synchronized steps across this ring. During each step, a device calculates partial attention scores using its local &lt;code&gt;Q&lt;/code&gt; shard and the &lt;code&gt;K&lt;/code&gt; shard it currently possesses. The crucial element is the subsequent communication: the &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;V&lt;/code&gt; shards are passed to the next device in the ring. This rotation repeats until every &lt;code&gt;Q&lt;/code&gt; shard has interacted with every &lt;code&gt;K/V&lt;/code&gt; shard. Throughout this process, each device accumulates partial outputs (weighted &lt;code&gt;V&lt;/code&gt; vectors) and normalization factors (softmax denominators). Finalizing the attention output typically involves a collective operation across all devices to combine these partial results correctly for each segment of the sequence.&lt;/p&gt;
&lt;p&gt;The Python example below offers a simulated Ring Attention logic on a single device, illustrating the underlying principles without necessitating actual multi-GPU hardware. The &lt;code&gt;_simulate_ring_attention&lt;/code&gt; function mimics the distributed process by iterating through hypothetical shards. In each iteration, it selects slices of the &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;V&lt;/code&gt; tensors (&lt;code&gt;k_shard&lt;/code&gt;, &lt;code&gt;v_shard&lt;/code&gt;) to represent the data one device would handle at a given step. It then computes attention scores between the full &lt;code&gt;Q&lt;/code&gt; tensor (a simplification from a truly distributed setup) and the current &lt;code&gt;k_shard&lt;/code&gt;. The simulation effectively captures the essence of the ring approach by accumulating the weighted values and the softmax normalizers across these iterations, mirroring how partial results would be combined in a distributed setting before a final normalization step yields the output. While demonstrating the computational flow, this simulation naturally doesn&#39;t provide the parallelism or memory savings of a true multi-device Ring Attention implementation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RingAttention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_shards&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_heads
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; head_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_shards &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_shards
        
        &lt;span class=&quot;token comment&quot;&gt;# Projections&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; head_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_simulate_ring_attention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# This simulates ring attention without actual multi-GPU support&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute shard sizes&lt;/span&gt;
        shard_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_shards &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_shards
        
        &lt;span class=&quot;token comment&quot;&gt;# Initialize outputs&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        normalizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Simulate sharded processing&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; shard_idx &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_shards&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            start_idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; shard_idx &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; shard_size
            end_idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_idx &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; shard_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Process this shard&#39;s keys and values&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; start_idx &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                k_shard &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; start_idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;end_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                v_shard &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; start_idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;end_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Compute attention scores&lt;/span&gt;
                attn_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_shard&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale
                
                &lt;span class=&quot;token comment&quot;&gt;# Apply mask if provided&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mask &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    shard_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; start_idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;end_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                    attn_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attn_scores&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;masked_fill&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;shard_mask &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Apply softmax and dropout (accumulated over shards)&lt;/span&gt;
                attn_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_scores&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                attn_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_probs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Update output and normalizer&lt;/span&gt;
                output &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;matmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attn_probs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v_shard&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                normalizer &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; attn_probs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Normalize the output&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;normalizer &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Project queries, keys, values&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;q_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;k_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;v_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Transpose for attention computation&lt;/span&gt;
        q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        v &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, num_heads, seq_len, head_dim]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute ring attention&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_simulate_ring_attention&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Reshape and apply output projection&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_heads &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;o_proj&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;pre-normalization&quot; tabindex=&quot;-1&quot;&gt;Pre-normalization&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Pre-normalization&lt;/strong&gt; (often referred to as Pre-LN) was a shift in the architectural design of residual blocks. Instead of applying the normalization layer &lt;em&gt;after&lt;/em&gt; the main operation (like self-attention or a feed-forward network) as done in traditional post-normalization schemes, pre-normalization applies it &lt;em&gt;before&lt;/em&gt;. This seemingly small change has significant implications for training dynamics. By normalizing the input &lt;em&gt;before&lt;/em&gt; it enters the computationally intensive sub-layer, pre-normalization helps to stabilize the activations and gradients flowing through the network. This stabilization effect is particularly pronounced in very deep networks, mitigating issues like vanishing or exploding gradients and often allowing for higher learning rates and faster, more reliable convergence.&lt;/p&gt;
&lt;p&gt;The typical implementation within a residual block follows the structure &#92;( x + f(&#92;text{norm}(x)) &#92;), as demonstrated in the &lt;code&gt;PreNorm&lt;/code&gt; class. Here, &#92;( x &#92;) is the input to the block, &#92;( &#92;text{norm}(&#92;cdot) &#92;) represents a normalization function like Layer Normalization (LN) or Root Mean Square Normalization (RMSNorm), and &#92;( f(&#92;cdot) &#92;) denotes the main transformation function (e.g., multi-head attention or a position-wise feed-forward network). The input &#92;( x &#92;) first passes through the normalization layer (e.g., &lt;code&gt;self.norm(x)&lt;/code&gt;). The normalized output is then processed by the function &lt;code&gt;fn&lt;/code&gt;. Crucially, the output of this function is then added back to the &lt;em&gt;original&lt;/em&gt;, unnormalized input &#92;( x &#92;) via the residual connection. This structure ensures a clean gradient path through the identity connection (&lt;code&gt;+ x&lt;/code&gt;), further enhancing training stability compared to post-normalization where the normalization layer resides on the residual path itself.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PreNorm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; norm_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;layer&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fn
        
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; norm_type &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;layer&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;norm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;LayerNorm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; norm_type &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;rms&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;norm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; RMSNorm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unknown normalization type: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;norm_type&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Apply normalization first, then the function&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;norm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rmsnorm&quot; tabindex=&quot;-1&quot;&gt;RMSNorm&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;RMSNorm&lt;/strong&gt; (or Root Mean Square Normalization) is a simplification of the widely used LayerNorm, designed to reduce computational overhead while retaining comparable performance and often improving training stability. Unlike LayerNorm, which centers the activations by subtracting the mean and then scales by the standard deviation, RMSNorm omits the mean centering step entirely. The motivation behind this simplification stems from the empirical observation that the re-centering operation in LayerNorm accounts for a noticeable portion of its computational cost and that removing it often does not significantly harm, and can sometimes even benefit, model performance. It operates solely on the basis of re-scaling the inputs according to their root mean square magnitude.&lt;/p&gt;
&lt;p&gt;The core mechanism of RMSNorm involves normalizing the activations within a layer by dividing them by their root mean square value, computed across the features (or a specified dimension). For an input vector &#92;( x = (x_1, &#92;dots, x_n) &#92;), the RMS value is calculated as &#92;( &#92;text{RMS}(x) = &#92;sqrt{&#92;frac{1}{n} &#92;sum_{i=1}^n x_i^2} &#92;). The normalized output &#92;( &#92;bar{x}_i &#92;) is then &#92;( &#92;bar{x}_i = &#92;frac{x_i}{&#92;text{RMS}(x) + &#92;epsilon} &#92;), where &#92;( &#92;epsilon &#92;) is a small constant for numerical stability. Similar to LayerNorm, RMSNorm typically includes a learnable scaling parameter &#92;( g &#92;) (and sometimes a bias &#92;( b &#92;), although the original formulation often omits it to stick closer to the simplification principle), resulting in the final output &#92;( y_i = g_i &#92;bar{x}_i &#92;). By foregoing the mean calculation, RMSNorm offers a reduction in computation (estimated to be 7-64% faster than LayerNorm on GPUs depending on the setup) and memory usage, making it an attractive alternative, especially for large models where efficiency is paramount.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RMSNorm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; elementwise_affine&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eps
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;elementwise_affine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; elementwise_affine
        
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; elementwise_affine&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;weight &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Parameter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;register_parameter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;weight&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Calculate root mean square along the last dimension&lt;/span&gt;
        rms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Normalize by RMS&lt;/span&gt;
        x_normalized &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; rms
        
        &lt;span class=&quot;token comment&quot;&gt;# Apply scaling if using learnable parameters&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;elementwise_affine&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            x_normalized &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x_normalized &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;weight
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x_normalized
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;swiglu&quot; tabindex=&quot;-1&quot;&gt;SwiGLU&lt;/h2&gt;
&lt;p&gt;SwiGLU is an activation function derived from the Gated Linear Unit (GLU) family, specifically tailored for enhancing the performance of neural networks. The core concept behind GLU variants is to introduce a gating mechanism that adaptively controls the flow of information through the network. Standard feed-forward layers typically apply a single non-linearity to a linear transformation of the input. In contrast, GLU-based activations split the output of a linear layer into two parts; one part acts as a &amp;quot;gate&amp;quot; after passing through a non-linearity, modulating the other part via element-wise multiplication. SwiGLU distinguishes itself by employing the Sigmoid-weighted Linear Unit (SiLU), also known as Swish (&#92;( &#92;text{SiLU}(x) = x &#92;cdot &#92;sigma(x) &#92;), where &#92;( &#92;sigma &#92;) is the sigmoid function), as the specific non-linearity applied to the gating part. This choice has been empirically shown to yield significant performance improvements in various Transformer-based models compared to other activations like ReLU or standard GLU variants using sigmoid or other functions for the gate.&lt;/p&gt;
&lt;p&gt;The operational mechanism of SwiGLU within a feed-forward block typically involves projecting the input &#92;( x &#92;) using two separate linear transformations, yielding &#92;( Wx + b &#92;) and &#92;( Vx + c &#92;). The SwiGLU activation is then computed as &#92;( &#92;text{SwiGLU}(x, W, V, b, c) = &#92;text{SiLU}(Wx + b) &#92;odot (Vx + c) &#92;), where &#92;( &#92;odot &#92;) denotes element-wise multiplication. Effectively, the input is processed through two parallel linear paths. One path undergoes the SiLU activation to form the gate values, which then scale the output of the second linear path. This gating allows the network to dynamically control which features are passed forward based on the input context, leading to increased expressive power and better gradient flow compared to simpler activation functions. Its success in models like PaLM and LLaMA highlights its effectiveness in capturing complex patterns within language data.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SwiGLU&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_in&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_hidden&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_out&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        dim_hidden &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim_hidden &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; dim_in
        dim_out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim_out &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; dim_in
        
        &lt;span class=&quot;token comment&quot;&gt;# Linear transformations for gating&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim_in&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_hidden&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bias&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim_in&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_hidden&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bias&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Output projection&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim_hidden&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim_out&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bias&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# SwiGLU applies SiLU activation to one branch and gates it with the other&lt;/span&gt;
        hidden1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        hidden2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# SiLU (Swish) activation: x * sigmoid(x)&lt;/span&gt;
        hidden1_act &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sigmoid&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Element-wise product for gating&lt;/span&gt;
        hidden &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden1_act &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; hidden2
        
        &lt;span class=&quot;token comment&quot;&gt;# Output projection&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rotary-positional-embedding&quot; tabindex=&quot;-1&quot;&gt;Rotary Positional Embedding&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2104.09864&quot;&gt;&lt;strong&gt;Rotary Positional Embedding&lt;/strong&gt;&lt;/a&gt; (RoPE) introduces an elegant method for incorporating positional information directly into the self-attention mechanism of Transformer models, specifically designed to capture relative positional dependencies effectively. Traditional approaches often rely on adding absolute positional encodings to the input embeddings or using complex relative positional bias terms within the attention score calculation. RoPE takes a different route by viewing positional encoding as a rotation operation applied to the query and key vectors &lt;em&gt;before&lt;/em&gt; their dot product is computed. The key insight is that by rotating the Q vector corresponding to position &#92;( m &#92;) and the K vector corresponding to position &#92;( n &#92;) by angles proportional to &#92;( m &#92;) and &#92;( n &#92;) respectively, the resulting dot product inherently depends only on the &lt;em&gt;relative&lt;/em&gt; position &#92;( m - n &#92;) and the content of the vectors themselves, gracefully encoding relative distance without altering the vectors&#39; magnitudes.&lt;/p&gt;
&lt;p&gt;The core mathematical idea leverages the properties of complex number multiplication or, equivalently, 2D rotation matrices. Imagine representing pairs of embedding dimensions as complex numbers. RoPE applies a rotation to the query vector &#92;( q_m &#92;) at position &#92;( m &#92;) and the key vector &#92;( k_n &#92;) at position &#92;( n &#92;) using position-dependent rotation matrices &#92;( R_m &#92;) and &#92;( R_n &#92;), respectively. These matrices effectively rotate the vectors in 2D subspaces spanned by pairs of dimensions. The angle of rotation for each 2D subspace is determined by the absolute position (&#92;( m &#92;) or &#92;( n &#92;)) multiplied by a frequency term &#92;( &#92;theta_i &#92;), which decreases for higher dimensions, analogous to sinusoidal embeddings. The crucial property is that the dot product between the rotated vectors, &#92;( (R_m q_m)^T (R_n k_n) &#92;), simplifies such that it only depends on the original vectors &#92;( q_m, k_n &#92;) and a rotation matrix &#92;( R_{m-n} &#92;) corresponding to the relative distance, effectively embedding relative positional information directly into the attention score calculation.&lt;/p&gt;
&lt;p&gt;In practice, this rotation is implemented efficiently without explicit matrix multiplication. As shown in the &lt;code&gt;apply_rotary_pos_emb&lt;/code&gt; function, the embedding dimensions are typically split into pairs. For each pair &#92;( (x_i, x_{i+1}) &#92;), the rotation corresponding to position &#92;( m &#92;) and frequency &#92;( &#92;theta_j &#92;) (derived from &lt;code&gt;inv_freq&lt;/code&gt; in the &lt;code&gt;RotaryEmbedding&lt;/code&gt; class) is applied using trigonometric functions: the new pair &#92;( (x&#39;_i, x&#39;_{i+1}) &#92;) becomes &#92;( (x_i &#92;cos(m&#92;theta_j) - x_{i+1} &#92;sin(m&#92;theta_j), x_{i+1} &#92;cos(m&#92;theta_j) + x_i &#92;sin(m&#92;theta_j)) &#92;). The &lt;code&gt;RotaryEmbedding&lt;/code&gt; class pre-computes the necessary cosine and sine values (&lt;code&gt;cos&lt;/code&gt;, &lt;code&gt;sin&lt;/code&gt;) based on the sequence length and the inverse frequency bands (&lt;code&gt;inv_freq&lt;/code&gt;), which are derived from a base value (&lt;code&gt;base&lt;/code&gt;) and the embedding dimension (&lt;code&gt;dim&lt;/code&gt;). These pre-computed values represent &#92;( &#92;cos(m&#92;theta_j) &#92;) and &#92;( &#92;sin(m&#92;theta_j) &#92;) for all positions &#92;( m &#92;) and relevant frequencies &#92;( &#92;theta_j &#92;).&lt;/p&gt;
&lt;p&gt;Applying RoPE involves generating these &lt;code&gt;cos&lt;/code&gt; and &lt;code&gt;sin&lt;/code&gt; embeddings for the given sequence length and then using them to transform the Q and K vectors pair-wise across their head dimension &lt;em&gt;after&lt;/em&gt; the initial linear projections but &lt;em&gt;before&lt;/em&gt; the attention score calculation. This method offers several advantages: it naturally encodes relative positions, avoids adding positional information directly to word embeddings (potentially preserving more semantic information), and has shown strong empirical performance, including good generalization to sequence lengths longer than those seen during training. By integrating position information via rotation, RoPE provides a computationally efficient and effective mechanism for context-aware sequence modeling.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RotaryEmbedding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; base&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; interleaved&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;base &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; base
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interleaved &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; interleaved
        
        &lt;span class=&quot;token comment&quot;&gt;# Generate inverse frequency bands&lt;/span&gt;
        inv_freq &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;register_buffer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;inv_freq&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; inv_freq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Get device from buffer if not specified&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; device &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            device &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;inv_freq&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device
            
        &lt;span class=&quot;token comment&quot;&gt;# Generate position indices&lt;/span&gt;
        positions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute sinusoidal patterns&lt;/span&gt;
        freqs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;outer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;positions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;inv_freq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Get sine and cosine embeddings&lt;/span&gt;
        emb &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;freqs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; freqs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        cos &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;emb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        sin &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;emb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dim&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; cos&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sin

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_rotary_pos_emb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cos&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; interleaved&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Apply rotary embeddings to queries and keys&lt;/span&gt;
    batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_heads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
    cos &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cos&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cos&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [1, 1, seq_len, dim/2]&lt;/span&gt;
    sin &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sin&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sin&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [1, 1, seq_len, dim/2]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Split queries and keys for rotation&lt;/span&gt;
    half_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; head_dim &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
    q1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;half_dim&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; half_dim&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    k1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;half_dim&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; half_dim&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Apply rotation using half-dim rotary embeddings&lt;/span&gt;
    q_rotated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        q1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cos &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; q2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        q2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cos &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; q1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; sin
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    k_rotated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        k1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cos &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; k2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        k2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cos &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; k1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; sin
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; q_rotated&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_rotated
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;mixture-of-experts&quot; tabindex=&quot;-1&quot;&gt;Mixture of Experts&lt;/h2&gt;
&lt;p&gt;Mixture of Experts (shortened as MoE) is a model architecture designed to significantly increase the parameter count, and thus the potential capacity, of a neural network without incurring a proportionally massive increase in computational cost during inference or training. The core idea is to replace computationally intensive components, like the feed-forward network block in a Transformer, with multiple, smaller &amp;quot;expert&amp;quot; networks. Crucially, not all experts process every input token. Instead, a lightweight &amp;quot;router&amp;quot; or &amp;quot;gating&amp;quot; network dynamically selects a small subset of experts (typically just one or two, known as top-k routing) deemed most suitable for processing each specific input token based on its features. This conditional computation allows MoE models to possess potentially trillions of parameters while only activating a small fraction of them for any given input, maintaining manageable FLOPs compared to a similarly sized dense model.&lt;/p&gt;
&lt;p&gt;The router network is the core idea, it acts as a learned decision-maker. It takes the representation of an input token and produces scores or logits indicating the suitability of each available expert for that token. In the example code&#39;s &lt;code&gt;_compute_routing_weights&lt;/code&gt; function, these logits are often processed using a top-k function to identify the &lt;code&gt;k&lt;/code&gt; experts with the highest scores. The scores for these selected experts are then typically normalized, often using a softmax function, to produce routing weights. These weights determine the contribution of each selected expert to the final output for that token. During training, noise can be added to the router logits (as seen with &lt;code&gt;noise_std&lt;/code&gt;) to encourage exploration and prevent the router from collapsing to always favor only a few experts early on.&lt;/p&gt;
&lt;p&gt;Once the router selects the top-k experts and calculates their respective weights for a given input token, that token is dispatched only to those chosen experts. Each selected expert network (often a standard FFN, as shown in the &lt;code&gt;experts&lt;/code&gt; ModuleList) processes the token independently. The outputs produced by these active experts are then combined to form the final output for that token. This combination is typically a weighted sum, where the output of each selected expert is multiplied by its corresponding routing weight calculated by the router. For instance, if experts &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; are selected with weights &lt;code&gt;w_i&lt;/code&gt; and &lt;code&gt;w_j&lt;/code&gt;, the final output for token &lt;code&gt;x&lt;/code&gt; would be &lt;code&gt;w_i * expert_i(x) + w_j * expert_j(x)&lt;/code&gt;. This ensures that the final representation incorporates specialized knowledge from the most relevant experts.&lt;/p&gt;
&lt;p&gt;A challenge in training MoE models is ensuring that all experts are utilized effectively; otherwise, the router might learn to consistently overload a few &amp;quot;popular&amp;quot; experts while others remain underdeveloped. To counteract this, an auxiliary load balancing loss is typically incorporated into the training objective, as demonstrated by the &lt;code&gt;_compute_balance_loss&lt;/code&gt; method. This loss encourages the router to distribute the computational load (i.e., the input tokens) more evenly across all available experts, often by penalizing imbalances in either the number of tokens assigned to each expert or the sum of routing weights directed towards each expert. By successfully implementing sparse activation via routing and incorporating load balancing, MoE enables the construction of extremely large yet computationally efficient models.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MixtureOfExperts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; input_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_experts&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; top_k&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; noise_std&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;input_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hidden_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;output_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; output_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_experts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_experts
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;top_k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;top_k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_experts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;noise_std &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; noise_std
        
        &lt;span class=&quot;token comment&quot;&gt;# Create experts&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;experts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ModuleList&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sequential&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ReLU&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output_dim&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_experts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Router network&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;router &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_experts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Save expert counts and loads for balancing loss&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;register_buffer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;expert_counts&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_experts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_compute_routing_weights&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Calculate routing weights&lt;/span&gt;
        routing_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;router&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, seq_len, num_experts]&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Add noise during training to encourage exploration&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;training &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;noise_std &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            noise &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;randn_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;routing_logits&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;noise_std
            routing_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; routing_logits &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; noise
        
        &lt;span class=&quot;token comment&quot;&gt;# Get top-k experts for each token&lt;/span&gt;
        routing_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; selected_experts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;topk&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;routing_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;top_k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Normalize the routing weights with softmax&lt;/span&gt;
        routing_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;routing_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; routing_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; selected_experts
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_compute_balance_loss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; selected_experts&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; routing_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Compute auxiliary load balancing loss&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; selected_experts&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute probability of each expert being selected across batch&lt;/span&gt;
        expert_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_experts&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;selected_experts&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# For each position in selected_experts, increment the corresponding expert index&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;top_k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            expert_mask&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scatter_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; selected_experts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;k&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; routing_weights&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;k&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute mean routing probability per expert&lt;/span&gt;
        expert_routing_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; expert_mask&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute load balancing loss (all experts should receive equal probability)&lt;/span&gt;
        target_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expert_routing_probs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_experts
        balance_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mse_loss&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expert_routing_probs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_probs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_experts
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; balance_loss
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute routing weights and selected experts&lt;/span&gt;
        routing_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; selected_experts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compute_routing_weights&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Prepare output&lt;/span&gt;
        output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;output_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Dispatch to selected experts&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;top_k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Extract the current expert indices and weights&lt;/span&gt;
            expert_indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; selected_experts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, seq_len]&lt;/span&gt;
            expert_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; routing_weights&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [batch_size, seq_len, 1]&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Dispatch to each expert&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; expert_idx &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_experts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token comment&quot;&gt;# Find tokens assigned to this expert&lt;/span&gt;
                mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expert_indices &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; expert_idx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Gather input tokens assigned to this expert&lt;/span&gt;
                    expert_inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;mask&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Process with the expert&lt;/span&gt;
                    expert_outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;experts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expert_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expert_inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Scatter outputs back with appropriate weights&lt;/span&gt;
                    output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;mask&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; expert_outputs &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; expert_weights&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;mask&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Update expert counts (for monitoring)&lt;/span&gt;
                    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expert_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expert_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute auxiliary load balancing loss&lt;/span&gt;
        balance_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compute_balance_loss&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;selected_experts&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; routing_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Return output and auxiliary loss&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; balance_loss
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;learning-rate-warmup&quot; tabindex=&quot;-1&quot;&gt;Learning Rate Warmup&lt;/h2&gt;
&lt;p&gt;Learning rate warmup is a widely adopted heuristic employed during the initial phase of training neural networks to enhance stability and prevent divergence. At the beginning of training, model parameters are typically initialized randomly, often far from an optimal configuration. If a relatively large learning rate is used immediately, the initial gradients, which can also be large and erratic, may cause drastic parameter updates, potentially pushing the model into a poor region of the loss landscape or even causing numerical instability (e.g., loss explosion). Learning rate warmup mitigates this risk by starting the training process with a very small learning rate, which is then gradually increased over a predefined number of initial training steps (the &amp;quot;warmup steps&amp;quot;) until it reaches its target base value, from which point a standard learning rate schedule (like decay) might commence.&lt;/p&gt;
&lt;p&gt;The mechanism involves progressively scaling the base learning rate during the warmup phase. A common strategy, illustrated by the &lt;code&gt;LinearWarmupScheduler&lt;/code&gt; class, is linear warmup. In this approach, the learning rate &#92;( &#92;eta_t &#92;) at step &#92;( t &#92;) is calculated as &#92;( &#92;eta_t = &#92;eta_{&#92;text{base}} &#92;times &#92;frac{t}{T_{&#92;text{warmup}}} &#92;) for &#92;( t &amp;lt; T_{&#92;text{warmup}} &#92;), where &#92;( &#92;eta_{&#92;text{base}} &#92;) is the target base learning rate and &#92;( T_{&#92;text{warmup}} &#92;) is the total number of warmup steps. As seen in the &lt;code&gt;get_lr&lt;/code&gt; method, the scaling factor &lt;code&gt;scale&lt;/code&gt; increases linearly from near zero to 1 over the &lt;code&gt;warmup_steps&lt;/code&gt;. Once the step count &lt;code&gt;last_epoch&lt;/code&gt; reaches &lt;code&gt;warmup_steps&lt;/code&gt;, the learning rate becomes equal to the &lt;code&gt;base_lrs&lt;/code&gt;, and the warmup phase concludes. This gentle ramp-up allows the model to adapt gradually during the critical early stages when activations and gradients might otherwise be volatile, leading to smoother convergence and often enabling the use of higher base learning rates later in training.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LinearWarmupScheduler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_LRScheduler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; last_epoch&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; warmup_steps
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optimizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; last_epoch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_lr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;last_epoch &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# During warmup: linearly increase from 0 to base LR&lt;/span&gt;
            scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;last_epoch &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;base_lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; base_lr &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;base_lrs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# After warmup: use base learning rate&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;base_lrs
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;cosine-schedule&quot; tabindex=&quot;-1&quot;&gt;Cosine Schedule&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Cosine scheduling&lt;/strong&gt; (sometimes called &lt;strong&gt;cosine annealing&lt;/strong&gt;) is a learning rate schedule technique. Its core principle is to gradually decrease the learning rate over the course of training, following the shape of a cosine curve. Unlike step decay schedules, which reduce the learning rate abruptly at specific epochs, cosine annealing provides a smooth, continuous reduction. Typically, the learning rate starts at an initial high value and decreases following the first half-cycle of a cosine function, reaching a predefined minimum value (often close to zero) by the final training step. This smooth decay has been shown empirically to help the optimization process by allowing larger steps early in training for broad exploration of the loss landscape, and progressively smaller steps later on for fine-tuning and convergence towards a good minimum.&lt;/p&gt;
&lt;p&gt;Early in training, a higher learning rate encourages faster exploration and helps escape poor local minima. As training progresses and the model parameters approach a more optimal region, reducing the learning rate becomes crucial to avoid overshooting the minimum and to allow for more precise convergence. The cosine function provides a schedule that starts with a relatively slow decay rate, allowing the optimizer to maintain momentum initially. The decay rate then accelerates towards the middle of the schedule before slowing down again as it approaches the minimum learning rate. This final slow-down phase near the end of training is particularly important, as it allows the optimizer to carefully settle into a potentially flat minimum, which empirical evidence suggests often correlates with better generalization performance compared to sharper minima.&lt;/p&gt;
&lt;p&gt;Furthermore, cosine scheduling is often combined with a &amp;quot;warmup&amp;quot; phase, as seen in the code example. During this initial phase (e.g., &lt;code&gt;warmup_steps&lt;/code&gt;), the learning rate is typically increased linearly from a very small value (or zero) up to the main initial learning rate. This warmup period helps stabilize training in the very beginning, especially for large models or datasets where large initial learning rates applied to randomly initialized weights could lead to instability or divergence. After the warmup, the cosine decay phase begins, smoothly decreasing the learning rate from its peak value down to the target minimum (&lt;code&gt;base_lr * min_lr_ratio&lt;/code&gt;) over the remaining &lt;code&gt;total_steps - warmup_steps&lt;/code&gt;. This combination of a gentle start (warmup) followed by a smooth, theoretically motivated decay (cosine annealing) provides a robust and effective learning rate strategy that often requires less hyperparameter tuning than step-based schedules and frequently leads to improved model accuracy.&lt;/p&gt;
&lt;p&gt;There is a &lt;a href=&quot;https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.CosineAnnealingWarmRestarts.html&quot;&gt;CosineScheduler&lt;/a&gt; in PyTorch that implements this technique. Let&#39;s look at a simplified version of it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CosineAnnealingWarmupScheduler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_LRScheduler&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; total_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; min_lr_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; last_epoch&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; warmup_steps
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;total_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; total_steps
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;min_lr_ratio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; min_lr_ratio
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optimizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; last_epoch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_lr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;last_epoch &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# During warmup: linearly increase from 0 to base LR&lt;/span&gt;
            scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;last_epoch &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;base_lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; base_lr &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;base_lrs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# After warmup: cosine decay from base LR to min_lr&lt;/span&gt;
            progress &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;last_epoch &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;total_steps &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;warmup_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Cosine decay formula: min_lr + 0.5 * (base_lr - min_lr) * (1 + cos(pi * progress))&lt;/span&gt;
            scale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;min_lr_ratio &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;min_lr_ratio&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; progress&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;base_lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; scale &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; base_lr &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;base_lrs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;adamw-optimizer&quot; tabindex=&quot;-1&quot;&gt;AdamW Optimizer&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;AdamW&lt;/strong&gt; (Adam with Decoupled Weight Decay) addresses a subtle issue in the standard implementation of weight decay (L2 regularization) within adaptive optimizers like Adam. This improved optimizer gives us a method where weight decay does not accumulate in the momentum nor variance. In traditional Adam, L2 regularization is often implemented by adding the decay term (&#92;(&#92;lambda &#92;cdot &#92;text{weight}&#92;)) directly to the gradient before computing the moving averages (&#92;(m_t&#92;) and &#92;(v_t&#92;)). However, this couples the weight decay effect with the adaptive learning rate derived from the gradient moments. Consequently, parameters with historically large gradients (and thus larger &#92;(v_t&#92;) values) experience smaller effective weight decay, contrary to the goal of applying uniform regularization pressure. AdamW decouples these processes: it performs the standard Adam updates based purely on the gradients and separately applies the weight decay step directly to the weights, effectively restoring the original behavior of L2 regularization where weights decay proportionally to their magnitude, independent of their gradient history.&lt;/p&gt;
&lt;p&gt;The core update mechanism of AdamW largely follows the standard Adam procedure but modifies how weight decay is applied. At each step &#92;(t&#92;) for a parameter &#92;(&#92;theta&#92;), it first calculates the biased first moment estimate &#92;(m_t = &#92;beta_1 &#92;cdot m_{t-1} + (1 - &#92;beta_1) &#92;cdot g_t&#92;) and the biased second raw moment estimate &#92;(v_t = &#92;beta_2 &#92;cdot v_{t-1} + (1 - &#92;beta_2) &#92;cdot g_t^2&#92;), where &#92;(g_t&#92;) is the gradient at step &#92;(t&#92;), and &#92;(&#92;beta_1&#92;), &#92;(&#92;beta_2&#92;) are exponential decay rates. These are then bias-corrected: &#92;(&#92;hat{m}_t = m_t / (1 - &#92;beta_1^t)&#92;) and &#92;(&#92;hat{v}_t = v_t / (1 - &#92;beta_2^t)&#92;). The crucial difference lies in the update rule. Instead of incorporating weight decay into &#92;(g_t&#92;), AdamW first applies weight decay directly to the parameters: &#92;(&#92;theta_{t-1}&#39; = &#92;theta_{t-1} &#92;cdot (1 - &#92;text{lr} &#92;cdot &#92;lambda)&#92;), where &#92;(&#92;text{lr}&#92;) is the learning rate and &#92;(&#92;lambda&#92;) is the weight_decay factor (as seen in the line &lt;code&gt;p.data.mul_(1 - group[&#39;lr&#39;] * group[&#39;weight_decay&#39;])&lt;/code&gt;). Then, the standard Adam update using the bias-corrected moments is applied to these decayed weights: &#92;(&#92;theta_t = &#92;theta_{t-1}&#39; - &#92;text{lr} &#92;cdot &#92;hat{m}_t / (&#92;sqrt{&#92;hat{v}_t} + &#92;epsilon)&#92;). This corresponds to the code line &lt;code&gt;p.data.addcdiv_(exp_avg, denom, value=-step_size)&lt;/code&gt;, operating on the already decayed &lt;code&gt;p.data&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In deep learning practice, AdamW is employed similarly to Adam. It is instantiated by providing the model&#39;s parameters and key hyperparameters like the learning rate (&#92;(&#92;text{lr}&#92;)), beta values (&#92;(&#92;beta_1&#92;), &#92;(&#92;beta_2&#92;)), epsilon (&#92;(&#92;epsilon&#92;)), and the weight decay factor (&#92;(&#92;lambda&#92;)). The &lt;code&gt;step()&lt;/code&gt; method, typically called within the training loop after computing gradients (&lt;code&gt;loss.backward()&lt;/code&gt;), executes the update logic described above for each parameter. Its primary advantage is improved generalization, particularly observed in training large models like Transformers where regularization is critical. By ensuring that the weight decay strength is independent of the adaptive learning rate scaling, AdamW often allows for better hyperparameter tuning (especially &#92;(&#92;text{lr}&#92;) and &#92;(&#92;lambda&#92;)) and can lead to models that perform better on unseen data compared to standard Adam with L2 regularization.&lt;/p&gt;
&lt;p&gt;There is a highly optimized implementation of &lt;a href=&quot;https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html&quot;&gt;AdamW&lt;/a&gt; in PyTorch. Let&#39;s look at a simplified version of it, the example below demonstrates a minimal PyTorch implementation, initializing state variables (like &lt;code&gt;exp_avg&lt;/code&gt;, &lt;code&gt;exp_avg_sq&lt;/code&gt;) and performing the decoupled weight decay and moment-based updates within the parameter loop.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AdamW&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Optimizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; betas&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.999&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                 weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; amsgrad&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        defaults &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; betas&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;betas&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;eps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;weight_decay&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; amsgrad&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;amsgrad&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;AdamW&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; defaults&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; closure&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; closure &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; closure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; group &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;param_groups&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;params&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;continue&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Get gradient&lt;/span&gt;
                grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data
                
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; grad&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_sparse&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;AdamW does not support sparse gradients&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                amsgrad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;amsgrad&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# State initialization&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;step&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Exponential moving average of gradient values&lt;/span&gt;
                    state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;exp_avg&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Exponential moving average of squared gradient values&lt;/span&gt;
                    state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;exp_avg_sq&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; amsgrad&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;token comment&quot;&gt;# Maintains max of all exp. moving avg. of sq. grad. values&lt;/span&gt;
                        state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;max_exp_avg_sq&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                exp_avg&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; exp_avg_sq &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;exp_avg&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;exp_avg_sq&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; amsgrad&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    max_exp_avg_sq &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;max_exp_avg_sq&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                
                beta1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; beta2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;betas&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                
                state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;step&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Decay the first and second moment running average coefficient&lt;/span&gt;
                exp_avg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mul_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;beta1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; beta1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mul_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;beta2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;addcmul_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; grad&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; beta2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; amsgrad&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Maintains the maximum of all 2nd moment running avg. till now&lt;/span&gt;
                    torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;maximum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;max_exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; out&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;max_exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Use the max. for normalizing running avg. of gradient&lt;/span&gt;
                    denom &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; max_exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;eps&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    denom &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; exp_avg_sq&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;eps&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                bias_correction1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; beta1 &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;step&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                bias_correction2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; beta2 &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;step&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                step_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;lr&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bias_correction2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; bias_correction1
                
                &lt;span class=&quot;token comment&quot;&gt;# Apply weight decay BEFORE the optimization step&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;weight_decay&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mul_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;lr&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;weight_decay&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Update parameters&lt;/span&gt;
                p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;addcdiv_&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;exp_avg&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; denom&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;step_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; loss
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;multi-token-prediction&quot; tabindex=&quot;-1&quot;&gt;Multi-token Prediction&lt;/h2&gt;
&lt;p&gt;Multi-token prediction is a technique developed to accelerate the inference speed of autoregressive language models. Normally, autoregressive generation predicts tokens one by one: the model takes a sequence, predicts the single most likely next token, appends it to the sequence, and repeats the process. This sequential nature, requiring one full forward pass of the model for each generated token, becomes a significant bottleneck for latency-sensitive applications. Multi-token prediction attempts to overcome this by modifying the model&#39;s prediction head to output probabilities for multiple future tokens simultaneously based on the current hidden state, thereby reducing the number of forward passes needed to generate a sequence of a given length.&lt;/p&gt;
&lt;p&gt;The implementation typically involves adapting the final layer(s) of the language model. Instead of a single output layer (or &amp;quot;language model head&amp;quot;) mapping the final hidden state to logits over the vocabulary for the next token, a multi-token predictor might employ several strategies. One approach, as shown in the example class using separate heads, is to have multiple distinct prediction heads, each trained to predict a token at a different future offset (e.g., one head for token &#92;(t+1&#92;), another for &#92;(t+2&#92;), up to &#92;(t+N&#92;)). These heads usually take the same final hidden state (e.g., corresponding to token &#92;(t&#92;)) as input but learn different projections specialized for their respective time steps. Another approach involves using a single shared prediction head, which might require more complex mechanisms, potentially involving learned transformations of the hidden state or incorporating positional information, to generate distinct probability distributions for each of the &#92;(N&#92;) future tokens from essentially the same starting representation.&lt;/p&gt;
&lt;p&gt;Training a multi-token predictor involves teaching the model to correctly anticipate the sequence of &#92;(N&#92;) subsequent tokens given the preceding context. During the training phase, as illustrated in the &lt;code&gt;compute_loss&lt;/code&gt; method, the model receives input sequences and its predictions for the next &#92;(N&#92;) tokens are compared against the actual &#92;(N&#92;) target tokens in the training data. A loss function, usually cross-entropy, is calculated for each predicted position (&#92;(t+1&#92;) to &#92;(t+N&#92;)) and then aggregated (e.g., averaged) to form the final loss signal used for backpropagation.&lt;/p&gt;
&lt;p&gt;While this can show speed improvements, multi-token prediction has several drawbacks: the accuracy of predicting tokens further into the future tends to decrease, and the chosen sequence of &#92;(N&#92;) tokens might diverge from the optimal path that would have been taken by single-token generation. Therefore, it often represents a trade-off between generation speed and potential quality degradation that is very use-case dependent.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MultiTokenPredictor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vocab_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_predicted_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shared_prediction_head&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hidden_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden_dim
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vocab_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; vocab_size
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_predicted_tokens
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shared_prediction_head &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; shared_prediction_head
        
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; shared_prediction_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Share the same prediction head for all positions&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lm_head &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vocab_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Use separate prediction heads for each position&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lm_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ModuleList&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden_dim&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vocab_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hidden_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden_states&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
        
        &lt;span class=&quot;token comment&quot;&gt;# Get the hidden states for the last token&lt;/span&gt;
        last_hidden &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hidden_states&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shared_prediction_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Use the shared head for all predictions&lt;/span&gt;
            logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lm_head&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;last_hidden&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            next_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; logits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# For positions beyond the first, we need to make a projection&lt;/span&gt;
            multi_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    multi_token_logits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# A simple projection for demonstration; in practice, this would be more complex&lt;/span&gt;
                    projected_hidden &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; last_hidden &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Just a dummy transformation&lt;/span&gt;
                    multi_token_logits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lm_head&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;projected_hidden&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            multi_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;multi_token_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Use separate heads for each position&lt;/span&gt;
            multi_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                head&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;last_hidden&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; head &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lm_heads
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# The first position is used for next-token prediction&lt;/span&gt;
            next_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; multi_token_logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; next_token_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; multi_token_logits
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compute_loss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hidden_states&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ignore_index&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Get predictions&lt;/span&gt;
        _&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; multi_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;forward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hidden_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Prepare targets: shift labels to align with predictions&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# We need the next num_predicted_tokens tokens after the input&lt;/span&gt;
        shifted_labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        targets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            targets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Stack targets&lt;/span&gt;
        stacked_targets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;targets&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Compute loss across all predicted positions&lt;/span&gt;
        loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            loss &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cross_entropy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                multi_token_logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;view&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vocab_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                stacked_targets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                ignore_index&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ignore_index
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; loss &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_predicted_tokens
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;speculative-decoding&quot; tabindex=&quot;-1&quot;&gt;Speculative Decoding&lt;/h2&gt;
&lt;p&gt;Speculative decoding is an clever technique designed to accelerate the inference process of large autoregressive language models, significantly reducing the wall-clock time required to generate text. Standard generation is bottlenecked by its sequential nature, where the computationally expensive large model (the &amp;quot;target&amp;quot; model) must perform a full forward pass to predict just one token at a time. Speculative decoding introduces a secondary, much smaller and faster &amp;quot;draft&amp;quot; model. This draft model rapidly generates a short sequence of candidate future tokens (a &amp;quot;draft&amp;quot;). The core idea is then to use the large target model to verify this entire draft sequence in a single, parallel forward pass, potentially accepting multiple tokens at once and thus amortizing the cost of the target model&#39;s computation over several generated tokens.&lt;/p&gt;
&lt;p&gt;The mechanism hinges on comparing the predictions of the draft model with those of the target model. After the draft model proposes a sequence of &#92;( k &#92;) tokens, &#92;( d_1, d_2, &#92;dots, d_k &#92;), the target model is run once on the original input sequence concatenated with the draft. This single pass yields the target model&#39;s probability distributions for the &lt;em&gt;next&lt;/em&gt; token at &lt;em&gt;each&lt;/em&gt; position within the draft sequence. For each draft token &#92;( d_i &#92;), its validity is checked against the target model&#39;s prediction at that same position. If the target model strongly agrees with the draft token (e.g., it would have also predicted &#92;( d_i &#92;) with high probability, or based on a specific acceptance rule comparing &#92;( P_{&#92;text{target}}(d_i) &#92;) and &#92;( P_{&#92;text{draft}}(d_i) &#92;)), the token is accepted. This verification proceeds sequentially through the draft.&lt;/p&gt;
&lt;p&gt;The process continues until either all &#92;( k &#92;) draft tokens are accepted, or a draft token &#92;( d_j &#92;) is rejected. If rejection occurs at position &#92;( j &#92;), all preceding accepted draft tokens (&#92;( d_1, &#92;dots, d_{j-1} &#92;)) are kept. The remaining part of the draft (&#92;( d_j, &#92;dots, d_k &#92;)) is discarded. Crucially, the target model&#39;s probability distribution calculated at position &#92;( j &#92;) can then be used to sample a &lt;em&gt;corrected&lt;/em&gt; token to append after the accepted sequence, ensuring the overall generation statistically follows the distribution of the more powerful target model. This corrected token, along with the previously accepted ones, forms the input for the next round of draft generation. By accepting multiple tokens per target model inference step on average, speculative decoding can achieve substantial speedups (e.g., 2-3x) with minimal impact on the quality of the generated text.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SimpleSpeculativeDecoding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; draft_model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_draft_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;target_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target_model
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;draft_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; draft_model
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;max_draft_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; max_draft_tokens
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; prompt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Start with the prompt&#39;s token IDs&lt;/span&gt;
        input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Keep generating until we reach max_length or an end token&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; input_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; max_length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Step 1: Generate multiple draft tokens&lt;/span&gt;
            draft_input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            draft_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            
            &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;no_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;max_draft_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Generate next token with draft model&lt;/span&gt;
                    outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;draft_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;draft_input_ids&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    next_token_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; outputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                    next_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_token_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Save the draft token&lt;/span&gt;
                    draft_tokens&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_token&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Add to draft input&lt;/span&gt;
                    draft_input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;draft_input_ids&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; next_token&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Check for end token&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; next_token&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token_id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Step 2: Verify with target model&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;no_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token comment&quot;&gt;# Get target model probabilities for all tokens including drafts&lt;/span&gt;
                verification_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;draft_tokens&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                target_outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;target_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;verification_ids&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                target_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target_outputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; input_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Convert to probabilities&lt;/span&gt;
                target_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                
                &lt;span class=&quot;token comment&quot;&gt;# Accept tokens until mismatch or all drafts accepted&lt;/span&gt;
                accepted_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; token_id &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;draft_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Check if target model agrees with draft&lt;/span&gt;
                    target_prob &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target_probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; token_id&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    draft_prob &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Draft chose this with highest probability&lt;/span&gt;
                    
                    &lt;span class=&quot;token comment&quot;&gt;# Accept based on probability ratio (simplified)&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;random&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_prob &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; draft_prob&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        accepted_tokens&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;token_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;token comment&quot;&gt;# Rejection: get new token from target model&lt;/span&gt;
                        new_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target_logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        accepted_tokens&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_token&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Add accepted tokens to input_ids&lt;/span&gt;
            input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;accepted_tokens&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Check for end token&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; input_ids&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token_id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Decode the generated tokens&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Tue, 29 Apr 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/post_transformers/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/post_transformers/</guid>
    </item>
    
    <item>
      <title>How Democrats Failed on Crypto</title>
      <description>&lt;h1 id=&quot;how-democrats-failed-on-crypto&quot; tabindex=&quot;-1&quot;&gt;How Democrats Failed on Crypto&lt;/h1&gt;
&lt;p&gt;It&#39;s hard to look back at the previous US administration and not recognize the near complete regulatory failure on the crypto issue. This failure has now metastasized into a serious political machine on Capitol Hill, becoming a giant fountain of corruption that pools dark money from unknown sources abroad into our politics and fuels far right extremist movements. Nothing embodies this naked corruption more than the &lt;a href=&quot;https://www.msnbc.com/opinion/msnbc-opinion/trump-meme-coin-presidential-power-explainer-rcna188734&quot;&gt;Trump memecoin&lt;/a&gt;, which directly fuels policial corruption by providing an unregulated means of &lt;a href=&quot;https://www.nytimes.com/2025/04/23/technology/trump-private-dinner-crypto-memecoin.html&quot;&gt;selling access&lt;/a&gt; to the current administration, the most brazenly corrupt act in the history of the American presidency.&lt;/p&gt;
&lt;p&gt;While this pay-to-play executive branch token is new, it should come as no surprise to anyone. It is the apotheosis of exactly what crypto was designed to do: fuel corruption, crime, and undermine the state. What was once a small brush fire that could have been easily extinguished by regulators and a more proactive executive branch is now a raging forest fire. The crypto industry is now deeply entwined with political corruption and has become a major political machine for the MAGA movement.&lt;/p&gt;
&lt;p&gt;Unfortuntately, this pattern of regulatory hesitation when confronted with corporate and financial interests has become a concerning hallmark of Democratic governance in the last two decades. Just as the Obama administration&#39;s &amp;quot;too big to jail&amp;quot; approach allowed financial institutions to escape meaningful accountability through settlements rather than criminal prosecutions, the Biden administration&#39;s fragmented approach to crypto regulation created a regulatory vacuum that the industry eagerly exploited. There are also stark parallels to how multi-level marketing companies leveraged political donations and claims of entrepreneurship to avoid scrutiny in the 1970s. There is nothing new under the sun, this foolishness repeats itself with such monotonous precision.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;All of this did not have to happen. Democrats had all the levers of power neccessary to curtail this dark money machine run amok, but they didn&#39;t use them.&lt;/strong&gt; The crypto policy of leaders like Joe Biden, Chuck Schumer, Ro Khanna, Nancy Pelosi, Kamala Harris, and Maxine Waters was a complete failure. But failure is something that we can learn from and fix. But it&#39;s paramount that we understand that inaction, equivocation, and strategic ambguitiy did not work the last time and they won&#39;t work the next time either. Hopefully by acknowledging these shortcomings and learning from them, the party can develop more effective strategies for preventing these kind of inherently-destabilising and anti-democratic technologies from growing without check in the future.&lt;/p&gt;
&lt;h2 id=&quot;party-unity-%26-messaging&quot; tabindex=&quot;-1&quot;&gt;Party Unity &amp;amp; Messaging&lt;/h2&gt;
&lt;p&gt;The Democratic Party&#39;s approach to cryptocurrency regulation was characterized by deep internal divisions that prevented cohesive policy action. These divisions manifested as competing factions pushing the party leadership in opposing directions.&lt;/p&gt;
&lt;p&gt;On one side stood regulatory hawks led by Senator Elizabeth Warren (D-MA) and Representative Brad Sherman (D-CA), who consistently advocated for strict oversight of crypto. Warren and her allies emphasized crypto&#39;s potential risks to consumers, financial stability, and national security. They regularly highlighted concerns about money laundering, sanctions evasion, and fraud within the crypto ecosystem. This faction viewed cryptocurrency primarily through a lens of consumer protection and financial system stability.&lt;/p&gt;
&lt;p&gt;Opposing this perspective was a small (but not insignificant) contingent of pro-crypto Democrats who saw crypto as an opportunity for financial innovation and inclusion—or perhaps more cynically, merely as a way to fund campaigns. Some Democrats expressed fear that excessive regulation would &amp;quot;stifle innovation&amp;quot; and cede technological leadership to other countries. Other democrats just wanted to appear &amp;quot;pro-business&amp;quot; and &amp;quot;pro-innovation&amp;quot; and embraced a form of techno-determinism declaring the &amp;quot;web3 is inevitable&amp;quot; and &amp;quot;crypto is here to stay&amp;quot;, regardless of how harmful it was to society. Others were just plain confused and delegated to their staffers, who in-turn based their reccomendations on industry whitepapers due to a lack of time and expertise. And some were just &lt;a href=&quot;https://www.opensecrets.org/members-of-congress/kyrsten-sinema/summary?cid=N00033983&quot;&gt;bought&lt;/a&gt; &lt;a href=&quot;https://www.opensecrets.org/members-of-congress/kirsten-gillibrand/summary?cid=N00027658&quot;&gt;off&lt;/a&gt; &lt;a href=&quot;https://www.opensecrets.org/members-of-congress/ruben-gallego/summary?cid=N00036097&quot;&gt;by&lt;/a&gt; the crypto industry, plain and simple.&lt;/p&gt;
&lt;p&gt;This ideological rift prevented the party from developing a unified stance on crypto regulation. The competing internal pressures effectively paralyzed legislative action, creating regulatory paralysis. Party leadership, including Senate Majority Leader Chuck Schumer (D-NY) and Vice President Kamala Harris, struggled to navigate these competing interests. During the 2024 election campaign, Vice President Kamala Harris attempted to walk an impossible and incoherent fine line between the two factions, vowing to &amp;quot;encourage&amp;quot; crypto while also protecting consumers and investors.&lt;/p&gt;
&lt;p&gt;On the crypto policy debate, I&#39;m reminded of the quote &amp;quot;If one side wants to build a bridge over a canyon and the other side doesn’t, the wisest possible position isn’t to build half a bridge that stops in thin air.&amp;quot; This incoherent middle-ground approach left a policy vacuum that allowed the cryptocurrency industry to shape the narrative and regulatory landscape, and that created the current mess we find ourselves in.&lt;/p&gt;
&lt;h2 id=&quot;don&#39;t-stifle-innovation&quot; tabindex=&quot;-1&quot;&gt;Don&#39;t Stifle Innovation&lt;/h2&gt;
&lt;p&gt;The stated reluctance among some Democrats to regulate technologies, often framed as a fear of &amp;quot;stifling innovation,&amp;quot; can be viewed through several critical lenses. One perspective is that this stance serves as a convenient shield, allowing policymakers to abdicate the difficult responsibility of understanding and governing complex technical fields. Faced with the arcane intricacies of software and conflicting narratives about its potential, invoking the universally positive concept of &amp;quot;innovation&amp;quot; provides a politically palatable justification for inaction or minimal intervention, delaying potentially necessary consumer protection or financial stability measures. This approach avoids the hard work of developing nuanced regulations, instead opting for a hands-off posture that appears forward-thinking but may neglect underlying risks.&lt;/p&gt;
&lt;p&gt;Compounding this potential abdication is a genuine lack of deep technical understanding among many policymakers. The crypto space is rife with jargon, hype, contradictory narratives, and abstract promises. Without a solid grounding in computer science, finance and database technology, distinguishing genuine technological breakthroughs from inefficient or redundant applications becomes challenging. This knowledge gap makes policymakers susceptible to persuasive industry narratives that emphasize transformative potential while downplaying limitations or negative externalities. Consequently, the fear of stifling the next big thing can outweigh concerns rooted in technical realities, such as the argument made by many software engineers that existing, simpler technologies can often achieve the same goals more efficiently and without the significant negative externalities or potential for illicit use associated with blockchain applications.&lt;/p&gt;
&lt;p&gt;This is best illustrated by Rep. Ritchie Torres (D-NY) who argued, &amp;quot;The project of radically decentralizing the internet and finance strikes me as a profoundly progressive cause&amp;quot;—a statement that reveals both profound technical ignorance and political naivety. Torres, notably the second-largest recipient of hedge fund industry money in Congress, fundamentally misunderstands that both the internet and financial systems are already decentralized by design. The internet has operated as a distributed network since its inception, while our financial system includes 4,455 federally insured credit unions alongside thousands of community banks and a distributed regulatory framework split across fifty states and four federal regulators. His framing of this deregulatory agenda of rolling back consumer protections and enabling predatory gambling as &amp;quot;progressive&amp;quot; represents either a cynical misappropriation of progressive language or a complete failure to understand the systems he claims to want to transform, or merely sophistry.&lt;/p&gt;
&lt;p&gt;This quote reveals an unfortunatly common phenomenon, that it&#39;s far easier to hand-wave and posture that &amp;quot;decentralization&amp;quot; (a term with many disparete meanings) or &amp;quot;tokenization&amp;quot; (a buzzword with no clear definition or meaning) solving some long-standing legacy financial problem than it is to refute the argument with a technical discussion. Brandolini&#39;s eponymous law states: “The amount of energy needed to refute bullshit is an order of magnitude bigger than to produce it.” And oh my, does the crypto industry exploit this assymetry to the fullest extent.&lt;/p&gt;
&lt;p&gt;Furthermore, the policy discourse is significantly shaped by a powerful nexus between Ivy League institutions and the private sector. Universities like Duke, MIT and Stanford house influential research centers and professors who often become prominent voices in policy discussions, frequently advocating for a light regulatory touch to foster innovation. However, these institutions often receive substantial grants and funding from the very industries they are analyzing. This creates a potential conflict of interest, where academic research and advocacy might inadvertently serve to legitimize and provide reputation laundering for crypto ventures. This academic-private flywheel generates authoritative-sounding arguments favoring innovation, effectively marginalizing dissenting technical experts who argue that much of the claimed innovation is unsubstantiated or comes with unacceptable trade-offs.&lt;/p&gt;
&lt;p&gt;Ultimately, the narrative pushed by this academic-industry complex, combined with corporate influence and a potential lack of technical depth among policymakers, creates a challenging environment for any critical perspectives to gain traction. Software engineers and other technologists pointing out that many blockchain use-cases are technically suboptimal solutions searching for a problem struggle to have their message cut through the noise. The arguments about efficiency, scalability, security, and negative externalities are often drowned out by the well-funded, academically-backed chorus chanting &amp;quot;innovation.&amp;quot; This dynamic contributes to a policy environment where Democrats hesitate to implement robust regulations, caught between the fear of hindering progress (real or imagined) and the complex, interwoven pressures shaping the debate.&lt;/p&gt;
&lt;p&gt;The reality is that cryptocurrency offered no genuine technological innovation. It merely created unregulated financial instruments that circumvented established regulatory frameworks, while facilitating speculative gambling on arbitrary price fluctuations of digital assets with no intrinsic value. Both of these functions fundamentally undermine the principles of a society governed by consistent and equitable rule of law. Consider that cryptocurrency is roughly the same age as the iPhone, yet while we can clearly point to how the iPhone transformed communication, productivity, and daily life for billions, crypto&#39;s societal contributions remain conspicuously absent after fifteen years of development.&lt;/p&gt;
&lt;p&gt;This is the area that I have been most active in, and unfortunately I don&#39;t have a good answer to this problem. Other than software engineers need to be more democratically involved and academics need to be less corruptible. I&#39;m not holding my breath for either of these things to happen.&lt;/p&gt;
&lt;h2 id=&quot;too-niche%2C-too-wonky&quot; tabindex=&quot;-1&quot;&gt;Too Niche, Too Wonky&lt;/h2&gt;
&lt;p&gt;The reality is that crypto policy is extremely niche and the average American both doesn&#39;t even know it exists and doesn&#39;t care about it. In polling it ranks alongside salmon fishing subsidies in terms of issues the public cares about.  Which makes messaging on this issue a near-impossible tasks, and barely worth the effort until there&#39;s some sort of scandal or crisis that dominates the news cycle. The average voter just doesn&#39;t have a reason to care, relative to everyday kitchen table issues.&lt;/p&gt;
&lt;p&gt;The reality that abstract financial policy is too wonky, too niche, and too far removed from the daily lives of most Americans. The technical and legal complexity creates a significant knowledge asymmetry between people interested in this topic and the public, allowing a hegemonic bloc of neoliberal interests to dominate the discourse. This bloc—comprising technology firms, venture capitalists, and financial institutions—effectively monopolizes the expertise needed to craft regulation, while simultaneously funding think tanks and academic centers that produce policy papers aligned with their market-oriented worldview.&lt;/p&gt;
&lt;p&gt;The resulting environment becomes captured by a classic case of elite-driven policymaking with a predisposition towards inaction and conflicts of interest. A small cadre of technocratic policymakers, themselves products of the revolving door between government and private industry, find themselves intellectually divided yet structurally constrained by career incentives that discourage regulatory boldness. Their personal career trajectories often lead to lucrative positions at law firms and financial institutions with vested interests in crypto markets, creating conflicts of interest that subtly shape regulatory approaches. This dynamic reinforces a hegemonic consensus that privileges market-based solutions and appeals to  &amp;quot;innovation&amp;quot; over any kind of action, effectively neutralizing attempts at meaningful regulation while maintaining the appearance of democratic deliberation. This is not a new phenomon of course, but unfortunately it is a very common one on issues that are so far outside of the public consciousness.&lt;/p&gt;
&lt;p&gt;One might think that the transformation of the Executive Branch into a blatant pay-to-play operation under Trump would break through the public, and could be a watershed moment for public awareness on crypto corruption. Yet this scandal has failed to galvanize meaningful opposition. Instead, we&#39;re witnessing a disturbing shift in American values, where many citizens have implicitly accepted Thucydides&#39; brutal principle that &amp;quot;the strong do what they can and the weak suffer what they must.&amp;quot; This cynical embrace of power over principle—the notion that if Trump can exploit the system, he&#39;s entitled to do so—represents a fundamental abandonment of the rule of law. Such thinking doesn&#39;t just enable corruption; it celebrates it as a form of strength. This profound ethical collapse makes regulatory reform nearly impossible, as it antithetical to the very foundation of a rules-based society. But that&#39;s where we&#39;re at as a country sadly.&lt;/p&gt;
&lt;h2 id=&quot;regulatory-sclerosis&quot; tabindex=&quot;-1&quot;&gt;Regulatory Sclerosis&lt;/h2&gt;
&lt;p&gt;The period spanning from 2020 to early 2025 witnessed a concerted push by prominent Democrats, notably Senator Elizabeth Warren, to establish stricter regulatory frameworks around the burgeoning cryptocurrency industry. Driven by concerns over illicit finance, including money laundering, terrorism financing, and rampant consumer fraud, these efforts sought to bring crypto tokens under the purview of existing financial regulations. While internal party consensus began showing fractures later in this period, key figures remained steadfast in advocating for measures aimed at mitigating the perceived risks inherent in the largely unregulated crypto space.&lt;/p&gt;
&lt;p&gt;At the forefront of these legislative initiatives was the &lt;strong&gt;Digital Asset Anti-Money Laundering Act (DAAMLA)&lt;/strong&gt;. Championed vigorously by Senator Warren, this bill represented the most comprehensive attempt to impose traditional financial safeguards onto the digital asset ecosystem. First introduced in December 2022 and formally reintroduced with expanded, initially bipartisan, support in July 2023, &lt;strong&gt;DAAMLA&lt;/strong&gt; aimed to close regulatory gaps exploited by illicit actors. Early co-sponsors included figures like Senators Roger Marshall (R-KS), Joe Manchin (D-WV), and Lindsey Graham (R-SC), signalling an initial cross-party interest in addressing crypto-related risks, although some Republican support later waned.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;DAAMLA&lt;/strong&gt; proposal gained significant traction among Democrats throughout late 2023, building a broad coalition within the party. By December 2023, numerous influential Democratic senators, including several members of the crucial Senate Banking Committee like Raphael Warnock, Laphonza Butler, and Chris Van Hollen, had signed on as co-sponsors. The legislation&#39;s core provisions intended to extend Bank Secrecy Act obligations—including Know Your Customer requirements—to previously uncovered entities like wallet providers, miners, and validators. It also sought to mandate reporting for large offshore crypto transactions and directed the Treasury Department to establish robust compliance examination processes for crypto businesses.&lt;/p&gt;
&lt;p&gt;Senator Warren consistently justified the need for &lt;strong&gt;DAAMLA&lt;/strong&gt; by framing it as a critical national security measure. She argued that cryptocurrencies had become a preferred tool for rogue nations, drug cartels, ransomware gangs, and fraudsters, citing estimates that significant portions of North Korea&#39;s missile program were funded via crypto-related cybercrime and that illicit crypto transactions reached at least $20 billion in 2022 alone. This rationale resonated with some traditional financial institutions, garnering explicit support from groups like the Bank Policy Institute, which saw the bill as a step towards leveling the regulatory playing field.&lt;/p&gt;
&lt;p&gt;Beyond proposing new legislation, influential Democrats also actively opposed measures perceived as overly friendly to the crypto industry or lacking sufficient safeguards. Senator Warren, alongside Senate Banking Chairman Sherrod Brown (D-OH), stood as a primary obstacle to the Senate considering the &lt;strong&gt;Financial Innovation and Technology for the 21st Century Act (FIT 21)&lt;/strong&gt;. Despite this Republican-led bill passing the House with considerable bipartisan support, including 71 Democrats, Warren criticized it for potentially weakening regulatory oversight. Similarly, she voiced strong opposition to stablecoin bills like the proposed &lt;strong&gt;GENIUS Act&lt;/strong&gt;, arguing they risked consumer funds and national security unless they incorporated stringent anti-money laundering protections as demanded by the Treasury Department.&lt;/p&gt;
&lt;p&gt;Despite the persistent efforts led by Senator Warren to impose tighter controls, the period also revealed evolving dynamics within the Democratic party regarding cryptocurrency. A notable instance occurred in May 2024 when Senate Majority Leader Chuck Schumer and ten other Democratic senators voted against the Biden administration&#39;s stance to overturn specific SEC guidance (&lt;strong&gt;SAB 121&lt;/strong&gt;) on crypto accounting, signalling a potential softening or diversification of views among some Democrats. This divergence highlighted a growing internal debate, contrasting Warren&#39;s staunch regulatory approach with other positions emerging elsewhere within the party, setting the stage for continued policy clashes and inaction leading into 2025. And that&#39;s where we are today, despite the best intentions of the Warren faction of party, there&#39;s been very little progress on the legislative front.&lt;/p&gt;
&lt;h2 id=&quot;crypto-super-pacs&quot; tabindex=&quot;-1&quot;&gt;Crypto Super PACs&lt;/h2&gt;
&lt;p&gt;The cryptocurrency industry, initially operating on the fringes of the financial system, has rapidly evolved into a significant political force. Its business models, often predicated on minimal regulatory oversight and a continuous stream of public investment, created a powerful incentive to engage directly with policymakers. As governments worldwide began considering stricter rules to address concerns around consumer protection, market stability, and illicit finance, the crypto sector recognized the existential threat regulation posed. Consequently, the industry began to strategically marshal its considerable financial resources, transitioning from a nascent tech sector into a sophisticated lobbying powerhouse determined to shape legislation and political outcomes in its favor.&lt;/p&gt;
&lt;p&gt;This political mobilization manifested primarily through unprecedented levels of spending on lobbying and campaign finance. The industry deployed armies of lobbyists, many with deep connections forged through previous government service, to advocate directly with lawmakers and regulators. Furthermore, during critical election cycles, such as the 2024 midterms, crypto-aligned super PACs injected vast sums into closely contested races, exceeding $245 million, dwarfing the $27 million donated by Sam Bankman-Fried to a similar PAC in 2022. This deluge of money was strategically deployed to support candidates perceived as friendly to the industry&#39;s anti-regulatory stance and, crucially, to oppose those who favored stronger oversight, effectively functioning as large-scale influence buying.&lt;/p&gt;
&lt;p&gt;A key tactic in the industry&#39;s influence campaign involved specifically targeting prominent critics and regulatory advocates within the Democratic party. Figures like Senator Elizabeth Warren and Senator Sherrod Brown, then-Chair of the Senate Banking Committee, became focal points for intense opposition funding. These were not random attacks but calculated efforts to neutralize powerful voices pushing for regulation. The significant spending against Senator Brown, utilizing sophisticated and relentless campaign tactics, was a major factor contributing to his narrow defeat in the 2024 election.&lt;/p&gt;
&lt;p&gt;The political consequences of this targeted spending extended beyond individual races, significantly impacting the overall balance of power and the legislative agenda. The loss of key figures like Senator Brown, attributed in part to the crypto industry&#39;s intervention, played a role in the shift of Senate control towards the Republican party. This alteration in the political landscape severely hampered the prospects for enacting meaningful crypto regulations favored by Democrats and consumer advocates. Moreover, it weakened the legislative branch&#39;s ability to serve as a check on run-away executive power which we now have to deal with in the current administration.&lt;/p&gt;
&lt;p&gt;The cryptocurrency industry&#39;s aggressive foray into political spending raises existential questions about the health of democratic processes. When a single industry can pour hundreds of millions of dollars into elections to unseat critics and elect allies, it risks distorting policy outcomes away from the public interest and towards narrow corporate objectives. The rot runs deep in America, and crypto regulatory capture is just the latest symptom of the grotesque corruption of our political system by the Citizens United ruling.&lt;/p&gt;
&lt;h2 id=&quot;state-level-legislation&quot; tabindex=&quot;-1&quot;&gt;State-level Legislation&lt;/h2&gt;
&lt;p&gt;While the Biden administration was slow to act at the Federal level, a significant counter-trend emerged within individual states. Entrepreneurs in the crypto industry very easily captured state legislatures, and under the very simple guise of bringing business and jobs to their states, this created a scenario where the crypto industry, facing ambiguity or potential hostility from Washington D.C., found welcoming environments and operational certainty through state-level actions, effectively allowing it to establish a legitimate foothold within the United States.&lt;/p&gt;
&lt;p&gt;Among local city leaders, New York City Mayor Eric Adams emerged as one of the country&#39;s most outspoken Democratic crypto backers, even converting his salary into Bitcoin and Ether to demonstrate his commitment. However, Adams, known for his proximity to the crypto world, now faces serious fraud allegations; he has been charged in a five-count federal indictment with multiple fraud-related offenses. Prosecutors allege that he orchestrated a scheme to accept illegal campaign contributions from foreign nationals through &amp;quot;straw donors&amp;quot; who falsely claimed to be donating their own money. Additionally, he is charged with wire fraud for allegedly misappropriating funds from New York City&#39;s matching funds program by leveraging these illegal donations to obtain public matching funds.&lt;/p&gt;
&lt;p&gt;Wyoming stands out as the preeminent example of this state-led legitimization effort. Dubbed the &amp;quot;Delaware of Digital Asset Law,&amp;quot; Wyoming enacted a comprehensive suite of over 30 bills specifically designed to support the industry. Its most groundbreaking move was the creation of Special Purpose Depository Institution charters, allowing crypto companies like Kraken and Avanti to become state-chartered banks. This provided unprecedented legitimacy and, crucially, established a regulated pathway connecting crypto tokens to the traditional U.S. dollar payments system via the Federal Reserve. Wyoming further bolstered the industry by providing specific legal protections for crypto ownership, including shielding assets held in Wyoming LLCs from certain creditors.&lt;/p&gt;
&lt;p&gt;Following Wyoming&#39;s pioneering efforts, a wave of other states implemented their own crypto-friendly policies, solidifying the industry&#39;s presence across the country. Florida exempted crypto businesses from certain licensing requirements, created a regulatory sandbox for innovation, and even initiated a pilot program for paying state fees with cryptocurrency. Texas leveraged its energy resources and deregulated grid to become a hub for crypto mining, offering significant tax incentives and authorizing state-chartered banks to offer crypto custody services. Other states like Nevada, Tennessee, South Dakota, and Alaska also adopted various measures, from recognizing blockchain records legally to maintaining low tax burdens, collectively signaling a broad state-level acceptance and encouragement of the crypto sector.&lt;/p&gt;
&lt;h2 id=&quot;the-challenges-of-the-sec&quot; tabindex=&quot;-1&quot;&gt;The Challenges of the SEC&lt;/h2&gt;
&lt;p&gt;The intellectual legal debate surrounding whether most crypto assets qualify as securities under existing U.S. law delves into fundamental and ultimately deeply philosophical questions about value and legal rights in financial markets. Traditionally, securities like stocks or bonds represent a claim on an underlying entity&#39;s assets, profits, or future cash flows, imparting specific legal rights to the holder. Most crypto assets, however, lack such direct claims; their value often derives not from underlying assets or contractual entitlements but from market sentiment, technological promise, or speculative momentum—what might be termed &amp;quot;sentiment and vibes.&amp;quot; This creates a novel situation: assets potentially commanding significant market capitalizations without conferring tangible ownership or creditor rights, traded primarily on collective belief rather than intrinsic economic function. Applying securities laws designed for instruments with clear underlying claims to these fundamentally different digital tokens presents a genuine legal and conceptual challenge, forcing regulators and courts to grapple with absurd and/or worthless assets that may not cleanly fit in any category or really have any reason to exist.&lt;/p&gt;
&lt;p&gt;This inherent ambiguity fuels a legitimate debate about the appropriate regulatory framework and the very purpose of financial regulation. Bringing most crypto assets under the SEC&#39;s remit, while potentially offering investor protection, raises complex questions. Crypto assets function as speculative instruments lacking connection to productive capital formation or tangible economic value creation, then regulating them under securities law risks transforming the SEC, in this context, into something akin to a gambling regulator rather than a facilitator of efficient capital markets. This predicament highlights a profound, unresolved tension: should financial regulation solely focus on instruments funding economic activity, or must it now encompass purely speculative, digitally native assets traded on belief alone? Congress arguably should have stepped in to deliberate this complex issue, which touches upon the fundamental role of markets and finance in society—a nuanced debate involving intricate legal and economic philosophy that the public may struggle to grasp and policymakers have largely avoided confronting head-on. But Congress really isn&#39;t in the legislating business these days, so it fell on the SEC to muddle through the problem as best as they could and try and minimize public harm.&lt;/p&gt;
&lt;p&gt;On top of this complicated debate we never had, there was the claim that the SEC under Chair Gensler employed &amp;quot;regulation by enforcement&amp;quot; against the crypto industry often misrepresents standard regulatory procedure. Enforcement is a fundamental tool for agencies like the SEC, used to ensure compliance with existing laws—in this case, primarily the Securities Acts of 1933 and 1934. The SEC&#39;s actions largely involved applying the long-established Howey test to determine if crypto tokens qualified as investment contracts (securities). This application of existing legal frameworks and judicial precedent to novel technologies is a core function of regulatory bodies, not an invention of new rules post hoc through punitive measures. The SEC consistently articulated its view that most crypto tokens fit within the existing definition of a security, basing enforcement actions on violations of these established laws.&lt;/p&gt;
&lt;p&gt;Much of the crypto industry&#39;s objection stemmed from a fundamental disagreement with the SEC&#39;s application of the Howey test to their products, rather than a genuine lack of legal standards. Industry participants often argued for unique treatment or bespoke rules, viewing the application of traditional securities laws as ill-suited. When the SEC proceeded with enforcement based on its interpretation of existing law, it was labeled &amp;quot;regulation by enforcement&amp;quot; by those facing action or seeking a more lenient framework. However, this overlooks that regulatory agencies aren&#39;t obligated to create specific new rules for every new technology before enforcing existing statutes, especially when they believe current laws apply. Furthermore, the SEC did provide guidance through various communications and the enforcement actions themselves, which clarified the types of conduct considered non-compliant, albeit in a manner the industry found unfavorable.&lt;/p&gt;
&lt;p&gt;Ultimately, the &amp;quot;regulation by enforcement&amp;quot; narrative functions more as a rhetorical critique of the outcomes of the SEC&#39;s actions than a substantive legal challenge to its methods. It reflects the industry&#39;s frustration with being subjected to long-standing investor protection laws and the Howey framework, rather than demonstrating that the SEC was arbitrarily creating new rules through enforcement actions alone. Characterizing the application of established legal principles as an inherently improper process obscures the core issue: a clash between the industry&#39;s desire for a different regulatory approach and the SEC&#39;s mandate to apply existing securities laws as it interprets them. Regulation by enforcement was really just &amp;quot;enforcement&amp;quot; of long-standing laws that the industry just didn&#39;t want to comply with.&lt;/p&gt;
&lt;p&gt;However one decision by the Gensler SEC comes across as particularly wrong-headed that of the decision to declare Coinbase&#39;s registration effective in April 2021, enabling its direct listing. While the SEC&#39;s approval focused strictly on whether Coinbase met the necessary disclosure requirements for investors, not on endorsing its business practices or confirming compliance with all securities laws, this distinction proved problematic later. The approval, handled by the SEC&#39;s disclosure review division, essentially confirmed that Coinbase provided sufficient information for the public listing process, following standard procedure.&lt;/p&gt;
&lt;p&gt;The core of the controversy stems from the inherent conflicts of interest embedded in Coinbase&#39;s vertically integrated structure, which the SEC itself later challenged. In its June 2023 lawsuit, the SEC alleged that Coinbase had been operating illegally since at least 2019—well before its public listing—as an unregistered securities exchange, broker, and clearing agency simultaneously. This combination of functions within one entity is typically separated in traditional financial markets precisely to mitigate conflicts between intermediaries and their customers. The SEC argued that by acting as the marketplace, the agent facilitating trades, and the custodian holding assets and settling transactions, Coinbase created significant, unavoidable conflicts of interest detrimental to investors.&lt;/p&gt;
&lt;p&gt;This situation presents a paradox: the SEC, whose mandate includes ensuring market integrity and investor protection (partly by managing conflicts of interest), allowed a company to go public despite its core operations seemingly embodying the very conflicts the regulatory framework seeks to prevent. The paradox is heightened by evidence suggesting the SEC had warned Coinbase pre-listing about potential enforcement actions regarding its asset listings and operational structure. While much of the review predated Chair Gary Gensler&#39;s tenure, the fact remains that the agency greenlit public market access for a company whose business model it later sued as fundamentally non-compliant with securities laws designed to separate functions and avoid such conflicts. We don&#39;t know the internal machinations of what went on, but it&#39;s clear this decision was a mistake and further allowed the floodgates of capital to pour into the crypto industry on top of a predatory non-compliant business model which led us to the current mess.&lt;/p&gt;
&lt;p&gt;However compounding these choices and challenges, the Gensler SEC found itself in an exceedingly difficult position regarding the crypto market. Despite Gensler&#39;s stalwart commitment to applying securities laws, the agency faced a veritable &amp;quot;forest fire&amp;quot; of tens of thousands of individual tokens and platforms operating in clear violation of securities laws, stretching its already limited resources impossibly thin. Compared to the vast scale of the traditional US capital markets that constitute the SEC&#39;s primary and existential mandate, the crypto market, while growing, remained relatively small (relative to the size of normal capital markets). This created a resource allocation dilemma. Gensler was essentially tasked with containing this rapidly spreading digital wildfire with the equivalent of a garden hose, lacking the necessary budget and personnel.&lt;/p&gt;
&lt;p&gt;Critically, neither the Biden administration nor Congress provided the substantial reinforcements—either through significantly increased funding for the SEC or comprehensive legislative action—needed to adequately address the scale and novelty of the crypto problem, leaving the agency to manage an overwhelming situation with insufficient tools or resources. Credit should go to the stalwart staffers who tried their best despite the insurmountable problem they were tasked with. But it&#39;s clear the agency was never given the resources needed to fully tackle this problem at scale.&lt;/p&gt;
&lt;h2 id=&quot;current-administration&quot; tabindex=&quot;-1&quot;&gt;Current Administration&lt;/h2&gt;
&lt;p&gt;The current administration, is not surprisingly rife with conflicts of interest and corruption. The SEC allegedly dropped or paused multiple enforcement actions against crypto companies founded by or linked to President Trump&#39;s donors and business partners. Specifically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Binance&lt;/strong&gt;: Pausing the SEC case after the Trump family reportedly held talks to acquire a financial stake in the company.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coinbase&lt;/strong&gt;: Dismissing the SEC case shortly before its CEO attended a White House digital assets summit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Justin Sun (Tron)&lt;/strong&gt;: Pausing the SEC case against Sun, who invested $75 million in a Trump family-affiliated crypto project (World Liberty Financial) from which the family could potentially earn substantial revenue.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ripple Labs&lt;/strong&gt;: Reportedly dropping the SEC case less than two weeks after its CEO attended the White House crypto summit and after the company donated $5 million to Trump&#39;s inaugural committee.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Immutable and Uniswap&lt;/strong&gt;: Dropping investigations into these firms, which are connected to SEC Chair Nominee Paul Atkins&#39; investments via Anchorage Digital.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The SEC&#39;s Division of Corporate Finance issued a Staff Statement asserting most meme coins are not securities. This interpretation could shield President Trump&#39;s and First Lady Melania Trump&#39;s own meme coins (&lt;strong&gt;TRUMP&lt;/strong&gt; and &lt;strong&gt;MELANIA&lt;/strong&gt;) from regulatory scrutiny and federal securities laws. The subsequent closure of an investigation into the &amp;quot;Hawk Tuah&amp;quot; meme coin reinforces this concern.&lt;/p&gt;
&lt;p&gt;David Sacks, a Trump apointee and venture capitalist, was appointed as the Trump administration’s special advisor for AI and cryptocurrency-informally known as the “crypto czar.”  Craft Ventures holds stakes in other crypto-related companies, such as BitGo, Chia Network, and Dapper Labs while Craft Ventures retains exposure to the digital asset sector which David Sacks oversees.&lt;/p&gt;
&lt;p&gt;The Trump family&#39;s venture, &lt;strong&gt;World Liberty Financial&lt;/strong&gt;, is launching a stablecoin on a blockchain created by Binance, further intertwining the family&#39;s business interests with a company that has received potentially favorable regulatory treatment (paused SEC case).&lt;/p&gt;
&lt;p&gt;In addition to this, Paul Atkins, Trump&#39;s nominee for SEC Chair, holds significant crypto-related investments (up to $6 million) in firms like Securitize, Anchorage Digital, and Off the Chain Capital. These firms have connections (as investors or through supported assets) to companies like Coinbase, Ripple, Immutable, Uniswap, and Binance, which have recently benefited from favorable SEC decisions (dropped/paused cases). This raises concerns about potential conflicts of interest influencing SEC actions, even with his commitment to divest if confirmed.&lt;/p&gt;
&lt;p&gt;In Congress there are two pieces of theoretical legislation that are the industry wants to push through Congress, even though there is essentially no interest from the public for them to pass. These fall into two categories right now but might emerge as one bill in the future.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stablecoin Bill:&lt;/strong&gt; These proposed stablecoin bills (like the GENIUS and STABLE Acts) risk destabilizing the US financial system by creating a weak regulatory framework tailored to industry demands. They would allow a broad range of entities, including potentially under-regulated non-banks, to issue systemically important payment instruments, creating opportunities for regulatory arbitrage between inconsistent state and federal oversight. Mandating reserves sounds prudent, but the push to allow interest payments could transform stablecoins into risky, uninsured deposit-like products prone to runs (like we saw with money market funds in 2008), while oversight mechanisms might prove insufficient to guarantee true 1:1 backing, prioritizing industry profits and innovation theatre over genuine consumer protection and financial stability.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Market Structure Bill:&lt;/strong&gt; The proposed market structure legislation represents an attempt by the crypto industry to weaken established investor protections and undermine regulatory authority for its own benefit. By pushing to rigidly define jurisdiction and create pathways to classify many digital assets as commodities under the CFTC, the bill seeks to sideline the SEC and subject fewer products to robust securities laws, potentially leaving investors exposed. The demand for &amp;quot;practical&amp;quot; registration processes and Congressionally-mandated rules risks locking in lenient standards, hindering regulators&#39; ability to adapt to evolving risks and ultimately legitimizing a largely speculative market while sacrificing crucial safeguards under the guise of promoting innovation. This legislation dangerously weakens existing securities regulation by potentially allowing firms to wrap traditional equity products onto the blockchain, relabel them as tokens to exploit regulatory loopholes, and thus subject them to far less stringent oversight, effectively incentivizing a race to the bottom that sacrifices vital investor protections like disclosures and controls.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say if any of these bills are allowed to pass, this would be incredibly harmful to the American public. We live in unprecedented times. Never before in the history of the United States has a president been so deeply entangled in business interests while in office, and the conflicts of interest and corruption this creates are unprecedented.&lt;/p&gt;
&lt;h2 id=&quot;so-is-there-any-hope%3F&quot; tabindex=&quot;-1&quot;&gt;So is there any hope?&lt;/h2&gt;
&lt;p&gt;At least in the current configuration of American politics, with a feckless Republican congress hell-bent on authoritarianism, where Citizens United is still the law of the land, it&#39;s hard to see what can be done so long as the current Democratic party is the only vehicle for change.&lt;/p&gt;
&lt;p&gt;The current leadership is too divided, too focused on botique social issues, too out of touch with voters, and too easily corruptible by commercial interests. The next several years will uncork a geyser of corruption from the Trump crime family and their friends in the crypto industry which will make the worst excesses of the Gilded Age seem tame. They&#39;ll be hundreds if not thousands of new scandals, and crypto fraud will become an everyday fixture of American life. Crypto fraudsters will make billions fleecing the public, relocate themselves and their ill-gotten gains to Dubai to shield themelves from the consequences of democratic backsliding, all while continuing to funnel money into crypto super PACs to keep the gravy train flowing while the world burns.&lt;/p&gt;
&lt;p&gt;Elections have consequences. The American public voted to touch the stove of authoritarianism coupled with unchecked financial fraud, and now they&#39;re going to get burned. And sadly the time to stop this was four years ago, it&#39;s too late now. But on the backside of several years of this orgy of greed and corruption, there might be an opportunity for a new generation of Democrats to step up. I don&#39;t know what that party will look like, or if the republic will even survive, but hopefully they will take the crypto threat seriously and not make the same mistakes again.&lt;/p&gt;
</description>
      <pubDate>Fri, 25 Apr 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/democrats_crypto_failure/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/democrats_crypto_failure/</guid>
    </item>
    
    <item>
      <title>MLIR Part 8 - GPU Compilation with MLIR</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/gpu.jpeg&quot; width=&quot;500px&quot; alt=&quot;GPU Bear Teaches CUDA&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;gpu-compilation-with-mlir&quot; tabindex=&quot;-1&quot;&gt;GPU Compilation with MLIR&lt;/h1&gt;
&lt;p&gt;Continuing on from last time, now that we have our transformer primitives that we need to optimize, let&#39;s look at how we can translate high-level tensor operations (like &lt;code&gt;softmax&lt;/code&gt;, &lt;code&gt;attention&lt;/code&gt;, etc) expressed in MLIR down to low-level code that can run in parallel on Nvidia GPUs to improve our performance. To do that we&#39;re going to start to put together the making of a small compiler.&lt;/p&gt;
&lt;p&gt;Now we&#39;re going to need to switch to a machine with an Nvidia GPU (sorry no MacOS). So either fire up your Linux machine with a Nvidia GPU or go rent one from the various providers.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://colab.research.google.com/&quot;&gt;Google Colab&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://runpod.io/&quot;&gt;RunPod&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://lambdalabs.com/&quot;&gt;LambdaLabs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Installing CUDA Toolkit notoriously painful, so if you rent a GPU from a cloud provider this is already done for you. If you need to setup a a bespoke instance you should use one of the official docker images from Nvidia like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://hub.docker.com/layers/nvidia/cuda/12.1.0-devel-ubuntu22.04/images/sha256-3c6687b5b582c18c45bd09e51a1eeb7e31845d2e89bf29ec50163cab8d09430b&quot;&gt;nvidia/cuda:12.1.0-devel-ubuntu22.04&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hub.docker.com/layers/nvidia/cuda/12.8.1-cudnn-runtime-ubuntu22.04/images/sha256-59e0e4376a0f16d10b03d3a14344b80a866a1674cb4948cb318291387ac05010&quot;&gt;nvidia/cuda:12.8.1-cudnn-runtime-ubuntu22.04&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull nvidia/cuda:12.1.0-devel-ubuntu22.04
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--gpus&lt;/span&gt; all nvidia/cuda:12.1.0-devel-ubuntu22.04 &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But if you want/need to install it on your own machine here&#39;s some instructions, although you should refer to the many guides online for the most up to date instructions. If you have a machine which does not have CUDA Toolkit installed, you can install it using the following steps.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; dpkg &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; cuda-keyring_1.0-1_all.deb
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; update
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; build-essential

&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; cuda-toolkit-12-x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Make sure you have the nvcc compiler installed, again if you are renting a GPU this is probably already done for you.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ nvcc &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
nvcc: NVIDIA &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;R&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; Cuda compiler driver
Copyright &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2005&lt;/span&gt;-2021 NVIDIA Corporation
Built on Thu_Nov_18_09:45:30_PST_2021
Cuda compilation tools, release &lt;span class=&quot;token number&quot;&gt;11.5&lt;/span&gt;, V11.5.119
Build cuda_11.5.r11.5/compiler.30672275_0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify that you have a Nvidia GPU installed:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;lspci &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; nvidia
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then test the CUDA version and your driver version:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ nvidia-smi
+---------------------------------------------------------------------------------------+
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; NVIDIA-SMI &lt;span class=&quot;token number&quot;&gt;535.183&lt;/span&gt;.01             Driver Version: &lt;span class=&quot;token number&quot;&gt;535.183&lt;/span&gt;.01   CUDA Version: &lt;span class=&quot;token number&quot;&gt;12.2&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;-----------------------------------------+----------------------+----------------------+
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Note the specific CUDA version (In this case 12.2) this is important because you&#39;ll need to instal libraries specific to this version.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now normally we would have to recompile MLIR with the CUDA runner enabled, which sucks. So I&#39;ve made a docker image that has MLIR with the CUDA runner enabled. So if you&#39;re renting a GPU from RunPod or LambdaLabs you can just use this base image. Or if you have a local machine you can pass the GPU through to the container using &lt;a href=&quot;https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html&quot;&gt;NVIDIA Container Toolkit&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:main
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; ghcr.io/sdiehl/docker-mlir-cuda:main &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise you have compile MLIR from source again. Here are the steps:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/llvm/llvm-project
&lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; llvm-project/build
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; llvm-project/build
RUN cmake &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCUDACXX&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/cuda/bin/nvcc &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCUDA_PATH&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/cuda &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CUDA_ARCHITECTURES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;75;80;86;90&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Add your GPU architecture here&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CUDA_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/cuda/bin/nvcc &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_PROJECTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_BUILD_EXAMPLES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_TARGETS_TO_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native;NVPTX;AMDGPU&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_BUILD_TYPE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Release &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_ASSERTIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DMLIR_ENABLE_CUDA_RUNNER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DMLIR_ENABLE_CUDA_CONVERSIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DMLIR_ENABLE_NVPTXCOMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON

RUN cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; mlir-opt mlir-translate mlir-transform-opt mlir-runner
RUN cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Heads up that this is going to be a somewhat weird crash course in CUDA programming. After all our goal is to build a compiler that targets the GPU from MLIR, so we&#39;re going to very quickly cover GPU basics and then delve straight into low-level assembly programming of the GPU very quickly. Most people don&#39;t write PTX directly in day to day work, unless they&#39;re working on compilers. If this is your first time writing CUDA code, it might be useful to read one of the many guides on CUDA programming online that cover GPU programming in more detail first.&lt;/p&gt;
&lt;h1 id=&quot;cuda-ecosystem&quot; tabindex=&quot;-1&quot;&gt;CUDA Ecosystem&lt;/h1&gt;
&lt;p&gt;The CUDA compilation process involves several key components and intermediate representations that are important to understand for GPU programming. Let&#39;s first explore how CUDA programs are compiled and executed.&lt;/p&gt;
&lt;p&gt;First CUDA is an extension of C++ that allows you to program the GPU. It&#39;s a language built on top of C++ that adds features specifically for GPU programming. It usese its own compiler &lt;code&gt;nvcc&lt;/code&gt; to compile to PTX (Parallel Thread Execution), which is an intermediate assembly language for NVIDIA GPUs. PTX provides a stable programming model and instruction set for parallel computing. It&#39;s architecture-neutral, meaning it can target multiple generations of NVIDIA hardware.&lt;/p&gt;
&lt;p&gt;The compilation process then moves from PTX to device-specific binary code called CUBIN (CUDA Binary). A CUBIN file contains the actual machine code that runs on specific GPU architectures. These files are ELF-formatted and contain not just executable code, but also symbols, relocators, and debug information. While CUBIN files are typically embedded in the host executable by default, they can be generated separately using nvcc&#39;s &lt;code&gt;-cubin&lt;/code&gt; option.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;nvcc&lt;/code&gt; manages this complex compilation process. It coordinates multiple compilation stages and can produce various output formats depending on your needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It can generate PTX code for future JIT (Just-In-Time) compilation&lt;/li&gt;
&lt;li&gt;It can create architecture-specific CUBIN files&lt;/li&gt;
&lt;li&gt;It can combine multiple PTX and CUBIN files into a single FATBIN file&lt;/li&gt;
&lt;li&gt;It can embed these GPU binaries into host executables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you invoke a compiled program on the host it will then do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Determines the available GPU architecture&lt;/li&gt;
&lt;li&gt;Selects the appropriate CUBIN from the FATBIN if available&lt;/li&gt;
&lt;li&gt;Falls back to JIT-compiling the PTX code if necessary for newer architectures&lt;/li&gt;
&lt;li&gt;Loads and executes the code on the GPU&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of this is handled for the end user by the compiler if you use the &lt;code&gt;nvcc&lt;/code&gt; compiler.&lt;/p&gt;
&lt;p&gt;The FATBIN format is particularly important as it allows a single executable to support multiple GPU architectures. It can contain both PTX code (for future compatibility through JIT compilation) and pre-compiled CUBIN files (for optimal performance on known architectures). Now let&#39;s look at some simple C++ code that uses CUDA.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

// CUDA kernel function
__global__ void helloKernel() {
  printf(&amp;quot;Hello, CUDA!&#92;n&amp;quot;);
}

int main() {
  // Call the CUDA kernel
  helloKernel&amp;lt;&amp;lt;&amp;lt;1, 1&amp;gt;&amp;gt;&amp;gt;();

  // Wait for the kernel to finish
  cudaDeviceReset(0);

  return 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In CUDA&#39;s extension of C++ there is special syntax for &lt;strong&gt;launching&lt;/strong&gt; the kernel. The &lt;code&gt;helloKernel&lt;/code&gt; function is the kernel we want to execute on the GPU. The &lt;code&gt;&amp;lt;&amp;lt;&amp;lt;1, 1&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; is the CUDA syntax for launching the kernel. The first argument is the number of blocks to launch, and the second argument is the number of threads per block.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;kernel&amp;lt;&amp;lt;&amp;lt;blocks, threads&amp;gt;&amp;gt;&amp;gt;(...)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More on threads and blocks later. Now inside of the kernel we have access to special variables &lt;code&gt;blockDim&lt;/code&gt;, &lt;code&gt;blockIdx&lt;/code&gt;, and &lt;code&gt;threadIdx&lt;/code&gt;.  As a simple example let&#39;s look at a kernel that squares each element of an array. Here we have a square kernel which we&#39;ll describe the internals of later, for now it simply squares each element of the array.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;__global__ void square(int* array, int n) {
    int tid = blockDim.x * blockIdx.x + threadIdx.x;
    if (tid &amp;lt; n)
        array[tid] = array[tid] * array[tid];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get a CUBIN code we can use the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nvcc &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; square square.cu
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can run th executable code which contains the compiled CUBIN code in a binary that runs the host logic on the CPU and the CUBIN code on the GPU.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;./square
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get a PTX code out of &lt;code&gt;nvcc&lt;/code&gt; we can use the following option:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nvcc &lt;span class=&quot;token parameter variable&quot;&gt;-ptx&lt;/span&gt; square.cu &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; square.ptx
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The PTX code will look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ptx&quot;&gt;.visible .entry square(int*, int)(
        .param .u64 square(int*, int)_param_0,
        .param .u32 square(int*, int)_param_1
)
{

        ld.param.u64    %rd1, [square(int*, int)_param_0];
        ld.param.u32    %r2, [square(int*, int)_param_1];
        mov.u32         %r3, %ntid.x;
        mov.u32         %r4, %ctaid.x;
        mov.u32         %r5, %tid.x;
        mad.lo.s32      %r1, %r3, %r4, %r5;
        setp.ge.s32     %p1, %r1, %r2;
        @%p1 bra        $L__BB0_2;

        cvta.to.global.u64      %rd2, %rd1;
        mul.wide.s32    %rd3, %r1, 4;
        add.s64         %rd4, %rd2, %rd3;
        ld.global.u32   %r6, [%rd4];
        mul.lo.s32      %r7, %r6, %r6;
        st.global.u32   [%rd4], %r7;

$L__BB0_2:
        ret;

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get an understands of what&#39;s going on here, let&#39;s break down the PTX instruction by instruction. There are two parameters to the kernel, the first is a pointer to the input integer array &lt;code&gt;param_0&lt;/code&gt; which is the device address of the input integer array, and the second is &lt;code&gt;param_1&lt;/code&gt; which is the size of the input integer array.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Instruction&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;ld.param.u64 %rd1, [square(int*, int)_param_0];&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Loads the device address of the input integer array from the kernel parameter.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;ld.param.u32 %r2, [square(int*, int)_param_1];&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Loads the size of the input integer array from the kernel parameter.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mov.u32 %r3, %ntid.x;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Gets the number of threads per block in the x-dimension.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mov.u32 %r4, %ctaid.x;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Gets the ID of the current thread block in the x-dimension.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mov.u32 %r5, %tid.x;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Gets the ID of the current thread within its block in the x-dimension.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mad.lo.s32 %r1, %r3, %r4, %r5;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Calculates a linear, global-like index for the current thread based on its block and thread ID.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;setp.ge.s32 %p1, %r1, %r2;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Sets the predicate flag &lt;code&gt;%p1&lt;/code&gt; to true if the calculated index is greater than or equal to the array size (bounds check).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;@%p1 bra $L__BB0_2;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;If the calculated index is out of bounds ( &lt;code&gt;%p1&lt;/code&gt; is true), the thread branches to the return statement, effectively doing nothing.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;cvta.to.global.u64 %rd2, %rd1;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Converts the generic device address of the array to a global address.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mul.wide.s32 %rd3, %r1, 4;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Calculates the byte offset into the array for the current thread&#39;s element (assuming &lt;code&gt;int&lt;/code&gt; size is 4 bytes).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;add.s64 %rd4, %rd2, %rd3;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Calculates the absolute global memory address of the array element corresponding to the current thread&#39;s index.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;ld.global.u32 %r6, [%rd4];&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Loads the 32-bit integer value from the calculated global memory address into register &lt;code&gt;%r6&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;mul.lo.s32 %r7, %r6, %r6;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Squares the integer value in &lt;code&gt;%r6&lt;/code&gt; and stores the lower 32 bits of the result in &lt;code&gt;%r7&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;st.global.u32 [%rd4], %r7;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Stores the squared integer value from &lt;code&gt;%r7&lt;/code&gt; back into the same global memory location, overwriting the original value.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;$L__BB0_2:&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Label marking the point to branch to for out-of-bounds threads.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;ret;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Returns from the kernel execution for the current thread.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;You can also use the &lt;code&gt;cuobjdump -sass&lt;/code&gt; command to disassemble the CUBIN code into SASS (SASS stands for &amp;quot;Shader ASSembly&amp;quot;):&lt;br /&gt;
On &lt;code&gt;-arch=sm_90&lt;/code&gt; our kernel looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-asm&quot;&gt;square(int*, int):
 LDC R1, c[0x0][0x28]
 S2R R5, SR_CTAID.X
 ULDC UR4, c[0x0][0x0]
 S2R R0, SR_TID.X
 IMAD R5, R5, UR4, R0
 ULDC UR4, c[0x0][0x218]
 ISETP.GE.AND P0, PT, R5, UR4, PT
 @P0 EXIT
 LDC.64 R2, c[0x0][0x210]
 ULDC.64 UR4, c[0x0][0x208]
 IMAD.WIDE R2, R5, 0x4, R2
 LDG.E R0, desc[UR4][R2.64]
 IMAD R5, R0, R0, RZ
 STG.E desc[UR4][R2.64], R5
 EXIT
.L_x_0:
 BRA `(.L_x_0)
 NOP
 NOP
 NOP
 NOP
 NOP
 NOP
 NOP
 NOP
.L_x_1:
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;gpu-architectures&quot; tabindex=&quot;-1&quot;&gt;GPU Architectures&lt;/h2&gt;
&lt;p&gt;The above CUDA kernel, when launched across multiple threads, calculates a unique index for each thread and, if that index is within the bounds of the input integer array, loads the integer at that index from global memory, squares it, and writes the result back to the same memory location, effectively performing an in-place squaring of the array elements in parallel. Threads whose calculated index falls outside the array bounds do not perform any memory operations.&lt;/p&gt;
&lt;p&gt;The GPU code such as &lt;code&gt;sm_53&lt;/code&gt;, consistently begins with the prefix &lt;code&gt;sm_&lt;/code&gt;. A single virtual GPU architecture may encompass multiple corresponding real architectures. The CUBIN can be executed on all GPUs within the same generation, but it is incompatible with GPUs from older or newer generations. For example, a CUBIN compiled for &lt;code&gt;sm_52&lt;/code&gt; is compatible with &lt;code&gt;sm_50&lt;/code&gt;, &lt;code&gt;sm_52&lt;/code&gt;, and &lt;code&gt;sm_53&lt;/code&gt; GPUs, but not with &lt;code&gt;sm_60&lt;/code&gt; GPUs. Furthermore, the performance of a CUBIN compiled for &lt;code&gt;sm_52&lt;/code&gt; when executed on &lt;code&gt;sm_53&lt;/code&gt; GPUs may be inferior to that of a CUBIN specifically compiled for &lt;code&gt;sm_53&lt;/code&gt; running on the same architecture.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Compile for sm_52&lt;/span&gt;
nvcc square.cu &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; square --gpu-architecture&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;compute_52 --gpu-code&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;sm_52

&lt;span class=&quot;token comment&quot;&gt;# Compile for sm_52, sm_53, sm_60&lt;/span&gt;
nvcc square.cu &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; square --gpu-architecture&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;compute_52 --gpu-code&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;sm_52,sm_53,sm_60
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most common architectures are:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Architecture&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;SM Version&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;GPUs&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Features&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Maxwell&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_50, sm_52, sm_53&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;GTX 750 Ti, GTX 960, GTX 970, GTX 980&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Dynamic Parallelism, CUDA Dynamic Parallelism&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Pascal&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_60, sm_61, sm_62&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;P100, GTX 1080, GTX 1080 Ti&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;HBM2 Memory, NVLink, Unified Memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Volta&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_70, sm_72&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;V100, Titan V&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;First-gen Tensor Cores, Independent Thread Scheduling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Turing&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_75&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;T4, RTX 2080, RTX 2080 Ti&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;First-gen RT Cores, Second-gen Tensor Cores&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Ampere&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_80, sm_86, sm_87&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;A100, A40, RTX 3090&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Third-gen Tensor Cores, TF32 precision, Structural Sparsity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Ada Lovelace&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_89&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;RTX 4090, L40, L40S&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Third-gen RT Cores, Fourth-gen Tensor Cores, AV1 encode/decode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Hopper&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_90, sm_90a&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;H100, H200&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Fourth-gen Tensor Cores, Transformer Engine, 900 GB/s NVLink&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Blackwell&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;sm_100, sm_100a, sm_101, sm_101a, sm_120, sm_120a&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;B100, B200&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;Fifth-gen Tensor Cores, Improved occupancy, Enhanced shared memory&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;cuda-python-ecosystem&quot; tabindex=&quot;-1&quot;&gt;Cuda Python Ecosystem&lt;/h2&gt;
&lt;p&gt;To use NVIDIA CUDA libraries in a Python Poetry project, first add the NVIDIA package index. If you&#39;re using Poetry, you can do this by adding the the &lt;code&gt;nvidia-pyindex&lt;/code&gt; package and following to extra index url.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;poetry &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; nvidia-pyindex &lt;span class=&quot;token parameter variable&quot;&gt;--source&lt;/span&gt; https://pypi.ngc.nvidia.com
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-toml&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token table class-name&quot;&gt;extra&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;index-url&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://pypi.ngc.nvidia.com&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following libraries are now available. The &lt;code&gt;cu12&lt;/code&gt; suffix indicates the CUDA 12.x version, you may need to adjust this depending on your CUDA version. The one core library you&#39;ll need to install is &lt;code&gt;cuda-python&lt;/code&gt; which is &lt;a href=&quot;https://pypi.org/project/cuda-python/&quot;&gt;available on PyPI&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cuda-python&lt;/code&gt; - Pythonic access to CUDA Runtime and other core functionalities&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then the core libraries are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nvidia-cuda-runtime-cu12&lt;/code&gt; - Core runtime library providing essential runtime functionality&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cublas-cu12&lt;/code&gt; - cuBLAS library for GPU-accelerated Basic Linear Algebra Subroutines&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cudnn-cu12&lt;/code&gt; - cuDNN library of deep learning primitives (scaled dot-product attention, convolution, matrix multiplication, softmax, and pooling)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cudnn-frontend&lt;/code&gt; - Python bindings for cuDNN frontend API&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are also instrumentation and compiler utilities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nvidia-cuda-nvrtc-cu12&lt;/code&gt; - Runtime Compilation library (NVRTC) for program compilation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-nvml-dev-cu12&lt;/code&gt; - Management Library (NVML) for monitoring and managing GPUs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-nvtx-cu12&lt;/code&gt; - Tools Extension (NVTX) for custom profiling and tracing instrumentation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cuda-nvcc-cu12&lt;/code&gt; - Compiler (NVCC) for compiling C/C++ code&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-nvjitlink-cu12&lt;/code&gt; - JIT Linker for runtime linking of device code&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cuda-sanitizer-api-cu12&lt;/code&gt; - Memory Checker for detecting memory errors in applications&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cuda-cupti-cu12&lt;/code&gt; - Profiling Tools Interface (CUPTI) for performance analysis and profiling&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The other librares are more specialized to specific tasks in scientific computing, signal processing, etc. Not going to cover these in detail here but they exist.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nvidia-cufft-cu12&lt;/code&gt; - cuFFT library for computing Fast Fourier Transforms&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-curand-cu12&lt;/code&gt; - cuRAND library for generating random numbers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cusolver-cu12&lt;/code&gt; - cuSOLVER library for dense and sparse direct solvers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-cusparse-cu12&lt;/code&gt; - cuSPARSE library for sparse matrix operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-npp-cu12&lt;/code&gt; - Performance Primitives (NPP) for image, video, and signal processing&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-nvjpeg-cu12&lt;/code&gt; - nvJPEG library for hardware-accelerated JPEG encoding/decoding&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvidia-opencl-cu12&lt;/code&gt; - Implementation of OpenCL for GPU computing&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;threads%2C-blocks%2C-and-warps&quot; tabindex=&quot;-1&quot;&gt;Threads, Blocks, and Warps&lt;/h1&gt;
&lt;p&gt;When we write CUDA, a &lt;strong&gt;kernel&lt;/strong&gt; refers to a function that you define to execute on the GPU. When you initiate a kernel launch, you are not merely invoking a single function; instead, you are creating hundreds or even thousands of parallel threads that concurrently execute that function across various data elements.&lt;/p&gt;
&lt;p&gt;This execution model is commonly known as the Single-Instruction Multiple-Thread (SIMT) paradigm, which emphasizes executing one program across many pieces of data. Say for example we have a kernel that computes the square of each element in an array. Don&#39;t worry about the magical &lt;code&gt;blockDim.x&lt;/code&gt; and &lt;code&gt;blockIdx.x&lt;/code&gt;, more on that later.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;__global__ void square(float* array, int n) {
    int tid = blockDim.x * blockIdx.x + threadIdx.x;
    if (tid &amp;lt; n)
        array[tid] = array[tid] * array[tid];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;CUDA kernels possess two key properties: they cannot explicitly return a value, necessitating that all result data be written to an array passed to the function (for scalar computations, this typically involves passing a one-element array); additionally, kernels must explicitly declare their thread hierarchy upon invocation, specifying the number of thread blocks and the number of threads per block. It is important to note that while a kernel is compiled once, it can be executed multiple times with varying block sizes or grid sizes.&lt;/p&gt;
&lt;p&gt;Ok breaking this down, this bit of code is just giving each thread its own unique index, &lt;code&gt;tid&lt;/code&gt;, to work with in the arrays. The kernel uses that index to let each thread add one element from A and B together. The &lt;code&gt;__global__&lt;/code&gt; keyword tells us that this function is a CUDA kernel running on the GPU, and we can kick it off from the CPU side. That &lt;code&gt;if (tid &amp;lt; n)&lt;/code&gt; check is there because we might end up launching a few more threads than we actually need for the array (we usually round up to a nice number), so any extra threads just sit idle if their index goes out of bounds.&lt;/p&gt;
&lt;p&gt;To execute a kernel from our main CPU program, known as the &amp;quot;host code,&amp;quot; we must define the number of threads to be used. While the kernel operates on the GPU, it is essential to have CPU code that sets up and launches the kernel. For instance our runner code might look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;int N = 10000;
int threadsPerBlock = 256;
int numberOfBlocks = (N + threadsPerBlock - 1) / threadsPerBlock;

square&amp;lt;&amp;lt;&amp;lt;numberOfBlocks, threadsPerBlock&amp;gt;&amp;gt;&amp;gt;(d_array, N);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To unpack the ideas here, CUDA organizes threads into warps, which consist of 32 threads executing in unison, and these warps are further organized into blocks. Each block operates on a Streaming Multiprocessor (SM), which has finite resources such as registers and shared memory. The size of the block influences the allocation of these resources and determines the number of warps that can run simultaneously, which is known as &lt;strong&gt;occupancy&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Efficient resource management is crucial for maximizing GPU performance. The GPU scheduler is responsible for distributing blocks across the available SMs. When the number of blocks exceeds the number of SMs, the excess blocks are queued and scheduled for execution as resources become available. This scheduling process is influenced by various factors, including the amount of shared memory and the size of the register file per SM, which ultimately determine the number of blocks that can be executed concurrently.&lt;/p&gt;
&lt;p&gt;The other major issue is so-called &lt;strong&gt;warp divergence&lt;/strong&gt;. This refers to the situation where threads within a warp diverge in their execution paths due to conditional statements, such as if-statements. Ideally, all threads in a warp should execute the same instruction simultaneously. However, when some threads take one route while others take another, the execution becomes serialized, leading to inefficiencies. The GPU hardware utilizes mask bits to manage which threads should follow each path, ensuring that all threads complete their tasks correctly. While this mechanism preserves correctness, it can adversely affect performance, as idle threads waste computational resources during divergent execution paths. Consequently, the overall throughput of the GPU may decline, particularly in cases with significant divergence. To enhance performance, developers should aim to minimize conditional branching within warps, thereby optimizing GPU execution efficiency.&lt;/p&gt;
&lt;h2 id=&quot;memory-hierarchy&quot; tabindex=&quot;-1&quot;&gt;Memory Hierarchy&lt;/h2&gt;
&lt;p&gt;There are three types of memory in a GPU:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Register Memory&lt;/strong&gt;: The fastest type of GPU memory, located directly on the GPU chip. It&#39;s accessible only to individual threads and has the shortest lifetime, lasting only for the duration of the thread.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Shared Memory&lt;/strong&gt;: This memory is shared by all threads within a GPU block. It&#39;s faster than global memory but slower than register memory. Shared memory allows for inter-thread communication and data sharing, lasting for the duration of the block.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Memory&lt;/strong&gt;: The largest memory pool in a GPU, accessible by all threads across all blocks. It&#39;s slower than register and shared memory but has the largest capacity. Global memory is used for storing data that needs to be shared across multiple blocks or threads.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most important performance consideration is &lt;strong&gt;memory transfer overhead&lt;/strong&gt; between CPU (host) and GPU (device). Since data transfer between host and device memory is relatively slow (relative to on-chip memory access), it&#39;s beneficial to minimize these transfers by keeping data on the GPU as long as possible. This often means performing multiple computations on the same data before transferring results back to the host, rather than transferring data back and forth for each operation. You want to live on the GPU as much as possible only returning to the CPU when necessary.&lt;/p&gt;
&lt;p&gt;It&#39;s also crucial to understand &lt;strong&gt;memory coalescing&lt;/strong&gt; - the ability to combine multiple memory accesses into a single transaction. Memory coalescing occurs when threads in a warp access contiguous memory locations. For optimal performance, developers should ensure that memory access patterns are coalesced whenever possible, as uncoalesced memory access can significantly impact performance by requiring multiple memory transactions.&lt;/p&gt;
&lt;p&gt;Memory &lt;strong&gt;bank conflicts&lt;/strong&gt; can also impact performance, particularly when using shared memory. Bank conflicts occur when multiple threads attempt to access different addresses in the same memory bank simultaneously. To avoid bank conflicts, you should carefully consider their shared memory access patterns and potentially pad their data structures to ensure optimal memory access distribution across banks. Using appropriate padding and memory access patterns can help maximize memory bandwidth utilization.&lt;/p&gt;
&lt;h2 id=&quot;kernel-invocation-%26-memory-movement&quot; tabindex=&quot;-1&quot;&gt;Kernel Invocation &amp;amp; Memory Movement&lt;/h2&gt;
&lt;p&gt;To manage the CUDA runtime there are several core functions &lt;a href=&quot;https://nvidia.github.io/cuda-python/cuda-bindings/latest/api.html&quot;&gt;in the Python API&lt;/a&gt; (which are just wrappers around the C API). The following are the core functions from the &lt;code&gt;cuda.cuda&lt;/code&gt; module in the Python bindings.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cuInit&lt;/code&gt; - Initializes the CUDA driver API.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuDeviceGet&lt;/code&gt; - Retrieves a handle to the first compute device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cudaSetDevice&lt;/code&gt; - Sets the current device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuCtxCreate&lt;/code&gt; - Creates a compute context on the specified device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuModuleLoadData&lt;/code&gt; - JIT compiles a PTX string to a device binary.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuModuleGetFunction&lt;/code&gt; - Retrieves a handle to a kernel function from a module.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuLaunchKernel&lt;/code&gt; - Launches a kernel on the specified device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuMemcpyHtoD&lt;/code&gt; - Copies data from host to device memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuMemcpyDtoH&lt;/code&gt; - Copies data from device to host memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuMemFree&lt;/code&gt; - Frees memory on the device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuMemAlloc&lt;/code&gt; - Allocates memory on the device.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuCtxDestroy&lt;/code&gt; - Destroys a compute context.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cuModuleUnload&lt;/code&gt; - Unloads a module from the device.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The error handling is a bit annoying in the CUDA Python API because it&#39;s a wrapper around C API which uses integer return codes. So for all of these functions you need to check the return value and raise an exception if it&#39;s not a success value. To make this easier there is a helper function &lt;code&gt;checkCudaErrors&lt;/code&gt; that will raise an exception if the return value is not a success value.&lt;/p&gt;
&lt;p&gt;To setup a CUDA context we can use the following boilerplate functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# gpu_setup.py&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Setup CUDA and create a context.&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cu  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cudart &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cudart  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nvrtc &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; nvrtc  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_cudaGetErrorEnum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CUresult&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuGetErrorName&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; name &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; err &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CUresult&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CUDA_SUCCESS &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;unknown&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cudart&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cudaError_t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; cudart&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cudaGetErrorName&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nvrtc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nvrtcResult&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; nvrtc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nvrtcGetErrorString&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unknown error type: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;checkCudaErrors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;CUDA error code=&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;_cudaGetErrorEnum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;findCudaDevice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    devID &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cudart&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cudaSetDevice&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;devID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; devID


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;findCudaDeviceDRV&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    devID &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuInit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    cuDevice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuDeviceGet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;devID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; cuDevice


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setup_cuda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;device_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Initializing CUDA...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Initialize CUDA&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuInit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get device&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; device_id &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        device &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; findCudaDeviceDRV&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        device_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# For printing purposes&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        device &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuDeviceGet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;device_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create context&lt;/span&gt;
    context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuCtxCreate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;CUDA context created on device &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;device_id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; context


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;cleanup_cuda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Destroying CUDA context...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuCtxDestroy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;CUDA context destroyed.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s look at how to manage memory between the host (CPU) and device (GPU) using the CUDA Python API. The following example demonstrates the essential memory operations you&#39;ll need for GPU programming:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First, we allocate memory on the GPU using &lt;code&gt;cuMemAlloc&lt;/code&gt;. This reserves space on the device for our data.&lt;/li&gt;
&lt;li&gt;Next, we transfer data from the host to the device with &lt;code&gt;cuMemcpyHtoD&lt;/code&gt; (Host to Device).&lt;/li&gt;
&lt;li&gt;After computations (which we&#39;ll cover later), we retrieve results using &lt;code&gt;cuMemcpyDtoH&lt;/code&gt; (Device to Host).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In our example below, we create a simple float array, send it to the GPU, and then copy it back to verify everything worked correctly. This pattern forms the foundation of GPU computing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# gpu_memory.py&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Minimal example demonstrating CUDA context setup and basic memory movement&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; gpu_setup &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; setup_cuda&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cleanup_cuda
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cu  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Setup CUDA context&lt;/span&gt;
context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; setup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Allocate memory on GPU&lt;/span&gt;
    buffer_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sizeof&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_float&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    device_ptr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemAlloc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;buffer_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create and initialize host array&lt;/span&gt;
    host_array &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Host array: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;host_array&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy data from host to device&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemcpyHtoD&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;device_ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; host_array&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; buffer_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create array for results&lt;/span&gt;
    result_array &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy data back from device to host&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemcpyDtoH&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result_array&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device_ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; buffer_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Result array: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;result_array&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Free GPU memory&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemFree&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;device_ptr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Always clean up the context&lt;/span&gt;
    cleanup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s launch a simple custom kernel that performs a basic operation. This example shows the complete workflow for executing GPU code directly from PTX assembly without needing to compile CUDA C++ code. The kernel simply sets the first element of an array to the value 42, but it illustrates the essential components of GPU kernel execution: initializing a CUDA context, allocating GPU memory, defining a kernel in PTX assembly language, loading the PTX code into a CUDA module, setting up execution dimensions (a single thread in a single block), launching the kernel with appropriate arguments, synchronizing to ensure completion, and finally transferring the result back to the host for verification. The PTX assembly code includes thread and block ID checks to ensure only the first thread performs the assignment.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# gpu_memory.py&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Minimal example demonstraitng lauching a kernel&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; gpu_setup &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; setup_cuda&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cleanup_cuda
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cu  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# This is the trivial kernel that sets the first element &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# of the array to 42 using a single thread&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# __global__ void set_value_kernel(int *data) {&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     if (threadIdx.x == 0 &amp;amp;&amp;amp; blockIdx.x == 0) {&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#         *data = 42;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#     }&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# }&lt;/span&gt;

ptx_kernel &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;&quot;
.visible .entry kernel(int*)( .param .u64 kernel(int*)_param_0) {
    ld.param.u64    %rd1, [kernel(int*)_param_0];
    mov.u32         %r1, %tid.x;
    mov.u32         %r2, %ctaid.x;
    or.b32          %r3, %r1, %r2;
    setp.ne.s32     %p1, %r3, 0;
    @%p1 bra        $L__BB0_2;
    cvta.to.global.u64      %rd2, %rd1;
    mov.u32         %r4, 42;
    st.global.u32   [%rd2], %r4;
$L__BB0_2:
    ret;
}
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Setup CUDA context&lt;/span&gt;
context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; setup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Allocate a single integer on the device&lt;/span&gt;
    data_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;itemsize
    d_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemAlloc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Set up grid and block dimensions - just one thread&lt;/span&gt;
    grid_dims &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    block_dims &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare arguments for the kernel&lt;/span&gt;
    args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;d_data&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    arg_types &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# None for pointer types&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Load the module&lt;/span&gt;
    module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleLoadData&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptx_kernel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get kernel function&lt;/span&gt;
    kernel_func &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleGetFunction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;kernel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare kernel arguments&lt;/span&gt;
    kernel_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Launch kernel&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuLaunchKernel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            kernel_func&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# shared memory bytes&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# stream&lt;/span&gt;
            kernel_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# kernel args&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# extra&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Synchronize&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuCtxSynchronize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy result back to host&lt;/span&gt;
    host_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemcpyDtoH&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;host_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; host_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Verify result&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Result: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;host_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; host_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Expected 42 but got &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;host_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Success! Kernel executed correctly.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Free device memory&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuMemFree&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Unload module&lt;/span&gt;
    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleUnload&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    cleanup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;thread-blocks-and-grids&quot; tabindex=&quot;-1&quot;&gt;Thread Blocks and Grids&lt;/h2&gt;
&lt;p&gt;Previously, we discussed how threads are organized into warps, typically comprising 32 threads each, which are then grouped into blocks. These blocks are assigned to SMs for execution. Now, let’s broaden our perspective. A thread block can be understood as a set of threads that work together using shared memory and synchronization mechanisms, all executing on the same SM.&lt;/p&gt;
&lt;p&gt;To inspect the GPU we can use the &lt;code&gt;cuda-python&lt;/code&gt; library to interrogate the GPU driver API to get information about the available parallelism properties.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; CUdevice_attribute&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cuDeviceGetAttribute&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cuDeviceGetName&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cuInit

&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuInit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; DEVICE_NAME &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuDeviceGetName&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
DEVICE_NAME &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEVICE_NAME&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;replace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;x00&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MAX_THREADS_PER_BLOCK &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuDeviceGetAttribute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    CUdevice_attribute&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MAX_BLOCK_DIM_X &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuDeviceGetAttribute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    CUdevice_attribute&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MAX_GRID_DIM_X &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuDeviceGetAttribute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    CUdevice_attribute&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
err&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; SMs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuDeviceGetAttribute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    CUdevice_attribute&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;GPU Device: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;DEVICE_NAME&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Number of multiprocessors: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;SMs&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Maximum number of threads per block: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;MAX_THREADS_PER_BLOCK&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Maximum number of blocks per grid: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;MAX_BLOCK_DIM_X&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Maximum number of threads per grid: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;MAX_GRID_DIM_X&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example on an A100 GPU we get the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Device Name: A100
Maximum number of multiprocessors: 108
Maximum number of threads per multiprocessor: 1024
Maximum number of threads per block: 1024
Maximum number of blocks per grid: 2147483647
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The use of blocks is driven by practical considerations: there is a hardware limit on the number of threads that can be launched in a single block, typically capped at 1024 threads on modern GPUs. If your computation requires more threads than this limit, you must divide them into multiple blocks. Think of a block as a team of threads collaboratively addressing a specific segment of data. The complete grid represents all the blocks initiated by a kernel, symbolizing the various teams working together to accomplish the overall task.&lt;/p&gt;
&lt;p&gt;For example if we wanted to add two arrays of &lt;code&gt;2048&lt;/code&gt; elements, we could utilize two blocks of &lt;code&gt;1024&lt;/code&gt; threads each—where block 0 processes indices &lt;code&gt;0-1023&lt;/code&gt; and block 1 processes indices &lt;code&gt;1024-2047&lt;/code&gt;. In general, if you have &lt;code&gt;N&lt;/code&gt; elements and your block can accommodate a maximum of &lt;code&gt;B&lt;/code&gt; threads, you would launch &lt;code&gt;ceil(N/B)&lt;/code&gt; blocks to ensure all elements are processed.&lt;/p&gt;
&lt;p&gt;Blocks are also super handy for scaling and scheduling. Think of it this way: your GPU has a bunch of SMs (let&#39;s say 20), and each SM can juggle a few blocks at once depending on what resources are available. So if you throw 100 blocks at it, but only 20 can run at the same time (one on each SM). The GPU kicks off those 20 blocks in parallel, and whenever one wraps up, it immediately grabs another waiting block and puts it to work on that freed-up SM.&lt;/p&gt;
&lt;p&gt;From your perspective as a programmer, all 100 blocks are working together to get your answer - it&#39;s like having &lt;code&gt;100 * blockSize&lt;/code&gt; threads all running. But behind the scenes, the GPU is cleverly distributing those blocks across its hardware. The beauty is you don&#39;t need to stress about launching more threads than your GPU can physically handle at once. The runtime just time-slices those blocks as needed. Plus, blocks give you a natural way to spread work across multiple GPUs or dial back how much parallel work happens at once (which can be a essentiall when you&#39;re bumping up against resource limits like shared memory or registers).&lt;/p&gt;
&lt;p&gt;Threads within the same block have powerful collaboration capabilities that set them apart. They share access to fast on-chip memory and can synchronize using barrier operations, enabling efficient coordination for tasks requiring inter-thread communication. However, this collaboration is strictly limited to threads in the same block.-Threads across different blocks must resort to slower global memory for communication and cannot directly synchronize with each other. This decision serves an important purpose: it allows for flexible scheduling of blocks across SMs and enables CUDA programs to scale seamlessly across various GPU architectures, regardless of how many SMs they contain.&lt;/p&gt;
&lt;p&gt;We typically choose thread block sizes that are powers of 2 (such as 128, 256, or 512) to align with the GPU&#39;s warp size of 32 threads and optimize for hardware characteristics. This sizing impacts several performance factors: it maximizes resource utilization, optimizes memory access patterns, and enhances the GPU&#39;s ability to effectively hide memory latency through efficient thread scheduling.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# N is size of the array to process&lt;/span&gt;
N &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10000&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# We calculate the number of blocks needed based on the total elements and threads per block&lt;/span&gt;
 &lt;span class=&quot;token comment&quot;&gt;# Typically a power of 2 like 128, 256, or 512&lt;/span&gt;
threads_per_block &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ceiling division to ensure all elements are processed&lt;/span&gt;
num_blocks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;N &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; threads_per_block &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; threads_per_block 
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;shared-memory-%26-synchronization&quot; tabindex=&quot;-1&quot;&gt;Shared Memory &amp;amp; Synchronization&lt;/h2&gt;
&lt;p&gt;Shared memory in CUDA represents one of the most powerful optimization tools available to improving performance, it functions as a programmable cache that dramatically reduces memory access latency. Located on-chip within each SM, shared memory offers bandwidth that can be 100x higher than global memory while providing latency that&#39;s roughly 100x lower. This performance advantage stems from its physical proximity to the compute cores—unlike global memory which resides off-chip and requires data to travel across the much slower PCIe bus or memory controllers. When threads in a block cooperate on calculations involving the same data, shared memory eliminates redundant global memory loads, effectively amortizing the cost of accessing global memory across all threads in the block.&lt;/p&gt;
&lt;p&gt;The mechanics behind shared memory reflect CUDA&#39;s thread hierarchy design. When a kernel launches, each thread block gets allocated its own dedicated portion of shared memory that persists for the block&#39;s lifetime and is visible only to threads within that specific block. This memory is organized into banks (typically 32 banks on modern GPUs) that can be accessed simultaneously by different threads, enabling high-bandwidth parallel access. However, when multiple threads attempt to access different addresses within the same bank (i.e. bank conflicts) these accesses get serialized, degrading performance.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;__syncthreads()&lt;/code&gt; function is core to working with shared memory. It forces all threads in a block to wait until everyone reaches that point in the code before continuing execution. This prevents nasty race conditions where some threads might try to read data that other threads haven&#39;t finished writing yet, which is a common source of subtle and hard-to-debug errors.&lt;/p&gt;
&lt;p&gt;In this example we declare a &lt;code&gt;__shared__&lt;/code&gt; array that is visible to all threads in the block. We then load data from global memory to shared memory, synchronize to make sure all threads have loaded their data, process the data in shared memory, and then write the results back to global memory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

__global__ void sharedMemoryExample(float* input, float* output, int n) {
    // Declare shared memory array - visible to all threads in the block
    __shared__ float sharedData[256];
    
    // Calculate global thread ID
    int tid = blockIdx.x * blockDim.x + threadIdx.x;
    
    // Load data from global memory to shared memory
    if (tid &amp;lt; n) {
        sharedData[threadIdx.x] = input[tid];
    }
    
    // Synchronize to make sure all threads have loaded their data
    __syncthreads();
    
    // Process data in shared memory (simple example: add 1 to each element)
    if (tid &amp;lt; n) {
        sharedData[threadIdx.x] += 1.0f;
    }
    
    // Synchronize again before writing results back
    __syncthreads();
    
    // Write the processed data back to global memory
    if (tid &amp;lt; n) {
        output[tid] = sharedData[threadIdx.x];
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using shared memory enables algorithmic techniques that would otherwise be impractical. In tiled algorithms, data is processed in small chunks (called tiles) that fit within shared memory, allowing operations like matrix multiplication to achieve near-theoretical peak performance by dramatically reducing global memory traffic. For stencil computations, shared memory allows threads to collaborate on overlapping regions, eliminating duplicate global memory loads. In reduction operations like sum or max calculations, shared memory facilitates efficient parallel reduction patterns where threads progressively combine results within a block. The key design pattern is to load data from global to shared memory, perform computation exclusively on shared memory, then write results back to global memory. This transforms what would otherwise be memory-bound computations into compute-bound ones, effectively leveraging the GPU&#39;s massive computational throughput while minimizing its primary bottleneck: memory access latency.&lt;/p&gt;
&lt;h2 id=&quot;connecting-to-mlir&quot; tabindex=&quot;-1&quot;&gt;Connecting to MLIR&lt;/h2&gt;
&lt;p&gt;Ok, so our goal now is to be able to dynamically (i.e. at runtime) specify high-level tensor operations and have them be lowered to the appropriate GPU code using MLIR. We&#39;ll leverage MLIR&#39;s progressive lowering capabilities to transform high-level tensor operations into optimized GPU code through a series of well-defined stages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;High-Level Representation&lt;/strong&gt;: We start with abstract tensor computations expressed in the &lt;code&gt;linalg&lt;/code&gt; dialect (matrix multiplication, convolutions, element-wise operations). These operations represent pure mathematical intent without implementation details, keeping our code close to the domain.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Affine Transformation&lt;/strong&gt;: Using passes like &lt;code&gt;-convert-linalg-to-loops&lt;/code&gt; or &lt;code&gt;-convert-linalg-to-affine-loops&lt;/code&gt;, we transform these high-level operations into the &lt;code&gt;affine&lt;/code&gt; dialect, which represents loops and memory accesses with precise mathematical relationships. This exposes opportunities for crucial optimizations like tiling, fusion, and loop interchange that improve memory locality and computational efficiency.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GPU Mapping&lt;/strong&gt;: We then map these loop structures to the GPU&#39;s execution model through passes like &lt;code&gt;-convert-affine-for-to-gpu&lt;/code&gt; and &lt;code&gt;-convert-parallel-loops-to-gpu&lt;/code&gt;. This transforms our code into the &lt;code&gt;gpu&lt;/code&gt; dialect, which explicitly represents concepts like thread blocks and threads while remaining hardware-agnostic.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Hardware-Specific Lowering&lt;/strong&gt;: Finally, we transition to NVIDIA-specific functionality through the &lt;code&gt;nvvm&lt;/code&gt; dialect, and ultimately to LLVM IR with GPU intrinsics. The final stages convert this to NVPTX assembly and then to CUBIN machine code that runs directly on the GPU.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Throughout this pipeline, each lowering step preserves program semantics while moving closer to the target hardware. We systematically transform from mathematical expressions to explicit loop nests, then to GPU execution constructs, and finally to hardware-specific code. This approach allows us to express computations at a high level while still generating highly optimized GPU code that leverages the full capabilities of the hardware.&lt;/p&gt;
&lt;h2 id=&quot;the-gpu-dialect&quot; tabindex=&quot;-1&quot;&gt;The &lt;code&gt;gpu&lt;/code&gt; Dialect&lt;/h2&gt;
&lt;p&gt;The GPU dialect in MLIR provides middle-level abstractions for launching GPU kernels that follow programming models similar to CUDA or OpenCL. This dialect abstracts away device-specific and driver-specific operations needed to launch GPU kernels, providing a streamlined path toward GPU execution from MLIR.&lt;/p&gt;
&lt;p&gt;The dialect uses &lt;code&gt;gpu&lt;/code&gt; as its canonical prefix and exposes operations that wrap common GPU primitives. In some idealized future world this might be used to target multiple GPU backends, but for now we&#39;ll just focus on the Nvidia specific operations with the goal of lowering to PTX.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Device Operations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;gpu.launch&lt;/code&gt; - Launches a GPU kernel with the specified grid and block dimensions. Takes block and thread configurations and contains the kernel body.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.launch_func&lt;/code&gt; - Launches a GPU function with the specified grid and block dimensions. Takes a function reference and block and thread configurations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.barrier&lt;/code&gt; - Synchronizes all threads within a thread block, ensuring all threads reach this point before continuing execution.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.binary&lt;/code&gt; - Represents a binary blob containing compiled GPU code (e.g. PTX or CUBIN format) that can be loaded and executed.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.printf&lt;/code&gt; - Prints formatted output from within GPU kernels, similar to C&#39;s printf function. Useful for debugging GPU code.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.return&lt;/code&gt; - Returns values from a GPU function/kernel back to the host.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.terminator&lt;/code&gt; - Marks the end of a GPU launch region, required as the last operation in a gpu.launch body.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.wait&lt;/code&gt; - Waits for asynchronous GPU operations to complete before continuing host execution.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.yield&lt;/code&gt; - Yields values from a structured control flow region in GPU code, similar to scf.yield.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Thread and Block Operations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;gpu.block_id&lt;/code&gt; - Returns the ID of the current block within the grid along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.block_dim&lt;/code&gt; - Returns the dimensions of blocks in the grid along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.block_size&lt;/code&gt; - Returns the total number of threads in a block along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.grid_id&lt;/code&gt; - Returns the ID of the current grid being executed along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.grid_dim&lt;/code&gt; - Returns the dimensions of the grid along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.grid_size&lt;/code&gt; - Returns the total number of blocks in the grid along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.thread_id&lt;/code&gt; - Returns the ID of the current thread within its block along a specified dimension (x, y, or z)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Memory Operations&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;gpu.alloc&lt;/code&gt; - Allocates memory on the GPU device. Takes a shape and element type and returns a memref pointing to the allocated memory.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.memcpy&lt;/code&gt; - Copies memory between host and device or between different locations on the device. Can handle host-to-device, device-to-host, and device-to-device transfers.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.dealloc&lt;/code&gt; - Deallocates memory previously allocated on the GPU device using gpu.alloc.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.host_register&lt;/code&gt; - Registers a memref as host-local memory, allowing it to be accessed directly by the host.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.host_unregister&lt;/code&gt; - Unregisters a memref for access from device.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The dialect is designed to be a target-agnostic representation that can be lowered to specific GPU backends. It provides abstractions for kernel invocations and may eventually include device management capabilities that aren&#39;t present at lower levels like LLVM IR intrinsics for GPUs.&lt;/p&gt;
&lt;p&gt;The dialect expects GPU code to be organized within &lt;code&gt;gpu.module&lt;/code&gt; operations and kernels to be represented as &lt;code&gt;gpu.func&lt;/code&gt; operations. This structure provides a clear separation between host and device code, making it easier to handle the compilation and execution pipeline. Non-kernel functions, such as device library calls, can be defined using &lt;code&gt;func.func&lt;/code&gt; or other non-GPU dialect operations, providing some flexibility in how GPU programs are structured.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// GPU module&lt;/span&gt;
  gpu.&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@gpu_module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// GPU function&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
        gpu.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Host function&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch_func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@gpu_module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt; 
        blocks&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
        threads&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
        args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The alternative form is to use the &lt;code&gt;gpu.launch&lt;/code&gt; operation which embeds the kernel function in a region passed as an argument to the launch operation. The &lt;code&gt;gpu.terminator&lt;/code&gt; operation is used to mark the end of the region.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch&lt;/span&gt;
        blocks&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        threads&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.printf&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello from %d&#92;n&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.terminator&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;nvgpu-dialect&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;nvgpu&lt;/code&gt; Dialect&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;nvgpu&lt;/code&gt; dialect functions as an intermediary in the MLIR ecosystem, connecting higher-level target-agnostic dialects like &lt;code&gt;gpu&lt;/code&gt; and &lt;code&gt;vector&lt;/code&gt; with the lower-level NVVM dialect specifically designed for NVIDIA GPUs. By representing PTX-specific operations while continuing to use MLIR&#39;s high-level abstractions such as &lt;code&gt;memref&lt;/code&gt; and &lt;code&gt;tensor&lt;/code&gt; dialects, it creates a bridge that preserves the benefits of both worlds.&lt;/p&gt;
&lt;p&gt;Using this dialect we can access NVIDIA-specific hardware features through the NVGPU dialect without needing to directly manipulate complex NVVM intrinsics. This intermediate layer significantly simplifies the generation of efficient GPU code while maintaining the clarity and expressiveness of higher-level MLIR representations.&lt;/p&gt;
&lt;p&gt;The dialect primarily focuses on exposing advanced NVIDIA GPU capabilities such as the &lt;strong&gt;Tensor Memory Accelerator&lt;/strong&gt; (TMA) for efficient tensor transfers between global and shared memory. It also provides mechanisms for &lt;strong&gt;asynchronous memory operations&lt;/strong&gt; that enable computation and memory transfers to overlap, effectively hiding latency. Memory access patterns are optimized through &lt;strong&gt;swizzling&lt;/strong&gt; techniques that reduce bank conflicts, while memory barriers (mbarriers) offer sophisticated synchronization tools for coordinating operations across threads.&lt;/p&gt;
&lt;p&gt;Warp-level programming receives special attention in the NVGPU dialect, with fine-grained controls that expose hardware-specific features like warp matrix multiply-accumulate operations and synchronization primitives. These capabilities are essential for implementing high-performance matrix operations and other compute-intensive workloads by leveraging specialized hardware units on newer NVIDIA GPUs like Hopper and Blackwell chips.&lt;/p&gt;
&lt;p&gt;The dialect&#39;s position in the compilation pipeline is strategically important, sitting between the more general GPU dialect and the hardware-specific NVVM dialect. This placement allows it to translate higher-level concepts into optimized hardware-specific operations while shielding developers from unnecessary complexity. When it&#39;s time to generate final code, the NVGPU dialect can be lowered to NVVM using the &lt;code&gt;-convert-nvgpu-to-nvvm&lt;/code&gt; pass, which transforms the NVGPU operations into corresponding NVVM dialect intrinsics.&lt;/p&gt;
&lt;p&gt;For now we&#39;re going punt on these features, but later on we&#39;ll explore these capabilities (particularly TMA and async memory operations) in greater detail.&lt;/p&gt;
&lt;h2 id=&quot;nvvm-dialect&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;nvvm&lt;/code&gt; Dialect&lt;/h2&gt;
&lt;p&gt;The NVVM dialect is a target-specific dialect that represents the LLVM IR for NVIDIA GPUs. It includes operations for GPU-specific constructs, such as thread and block indexing, synchronization, and memory operations.&lt;/p&gt;
&lt;p&gt;NVVM IR is a compiler IR (intermediate representation) based on the LLVM IR. The NVVM IR is designed to represent GPU compute kernels (for example, CUDA kernels). High-level language front-ends, like the CUDA C compiler front-end, can generate NVVM IR. The NVVM compiler (which is based on LLVM) generates PTX code from NVVM IR.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;CUDA Builtin&lt;/th&gt;
&lt;th&gt;NVVM Intrinsic&lt;/th&gt;
&lt;th&gt;MLIR Operation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;threadId.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@llvm.nvvm.read.ptx.sreg.tid.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gpu.thread_id {x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;blockIdx.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@llvm.nvvm.read.ptx.sreg.ctaid.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gpu.block_id {x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;blockDim.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@llvm.nvvm.read.ptx.sreg.ntid.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gpu.block_dim {x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;gridDim.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@llvm.nvvm.read.ptx.sreg.nctaid.{x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gpu.grid_dim {x,y,z}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;__syncthreads()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@llvm.nvvm.barrier0()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gpu.barrier&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&quot;mlir-python-bindings&quot; tabindex=&quot;-1&quot;&gt;MLIR Python Bindings&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://mlir.llvm.org/docs/Bindings/Python/&quot;&gt;MLIR Python bindings&lt;/a&gt; provide a interface to the MLIR C++ internals, allowing developers to programmatically manipulate MLIR&#39;s intermediate representations without relying on command-line tools. These bindings expose the core MLIR functionality through Python, enabling users to parse MLIR text, construct and modify the MLIR AST, apply transformation passes, and even JIT-compile and execute MLIR code directly from Python.&lt;/p&gt;
&lt;p&gt;Working with the Python bindings eliminates many of the friction points associated with the traditional command-line approach to MLIR. Rather than chaining together multiple command-line invocations, developers can create end-to-end compilation pipelines entirely within Python, making the development process more interactive and debuggable. However, building these bindings from source can be challenging due to their complex dependencies on LLVM and MLIR. To simplify adoption, pre-built wheels are available that package the bindings with all necessary dependencies, making installation as straightforward as a pip command.&lt;/p&gt;
&lt;p&gt;To install the bindings, you can use pip:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; mlir_python_bindings &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt; https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or if you&#39;re using Poetry add the following to your &lt;code&gt;pyproject.toml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-yaml&quot;&gt;[tool.poetry.dependencies]
python = &amp;quot;^3.10&amp;quot;
mlir-python-bindings = { version = &amp;quot;*&amp;quot;, source = &amp;quot;mlir-wheels&amp;quot;}

[[tool.poetry.source]]
name = &amp;quot;mlir-wheels&amp;quot;
url = &amp;quot;https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest&amp;quot;
priority = &amp;quot;supplemental&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok now let&#39;s use the Python bindings to lower a simple vector addition kernel to PTX.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.square&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we&#39;ll apply the passes&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Module
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;passmanager &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PassManager

mlir_module_str &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;vecadd.mlir&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; Context&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Parse the input module&lt;/span&gt;
    module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_module_str&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

pm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PassManager&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;enable_ir_printing&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;print_after_change&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;canonicalize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;one-shot-bufferize{ bufferize-function-boundaries function-boundary-type-conversion=identity-layout-map }&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;canonicalize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;convert-linalg-to-affine-loops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;func.func(affine-loop-invariant-code-motion)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;func.func(convert-affine-for-to-gpu)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-kernel-outlining&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lower-affine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-decompose-memrefs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;expand-strided-metadata&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;normalize-memrefs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;gpu.module(convert-gpu-to-nvvm{index-bitwidth=0 use-bare-ptr-memref-call-conv })&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;nvvm-attach-target&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;chip&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;chip_type&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; features&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;ptx80 O&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;convert-nvvm-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;reconcile-unrealized-casts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-to-llvm { use-bare-pointers-for-host use-bare-pointers-for-kernels }&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok so there&#39;s a lot going on here, let&#39;s break down each flag in the command:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;one-shot-bufferize{ bufferize-function-boundaries function-boundary-type-conversion=identity-layout-map }&lt;/code&gt;: Converts tensor operations to buffer operations in a single pass, handling function boundaries with identity layout mapping.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-linalg-to-affine-loops&lt;/code&gt;: Transforms high-level linalg operations into affine loop nests that explicitly iterate over tensor elements.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;func.func(affine-loop-invariant-code-motion)&lt;/code&gt;: Performs loop-invariant code motion on affine loops, moving computations outside loops when possible to reduce redundant calculations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;func.func(convert-affine-for-to-gpu)&lt;/code&gt;: Maps affine loops to GPU execution model, distributing iterations across GPU threads and blocks.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu-kernel-outlining&lt;/code&gt;: Extracts GPU kernel regions into separate GPU functions that can be launched from host code.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lower-affine&lt;/code&gt;: Converts affine dialect operations to standard control flow and arithmetic operations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu-decompose-memrefs&lt;/code&gt;: Decomposes complex memref types into simpler ones that can be handled by the GPU backends.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;expand-strided-metadata&lt;/code&gt;: Expands metadata for strided memory accesses to explicit calculations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;normalize-memrefs&lt;/code&gt;: Normalizes memory references to a form expected by the GPU backends.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu.module(convert-gpu-to-nvvm{index-bitwidth=0 use-bare-ptr-memref-call-conv })&lt;/code&gt;: Converts GPU dialect operations to NVVM dialect (NVIDIA&#39;s LLVM-based IR), using bare pointer calling conventions for memrefs.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvvm-attach-target{chip={chip_type} features=+ptx80 O=3}&lt;/code&gt;: Attaches target-specific information to the NVVM module, specifying the GPU architecture, PTX version 8.0 features, and optimization level 3.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-nvvm-to-llvm&lt;/code&gt;: Translates NVVM dialect to standard LLVM dialect for further processing.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reconcile-unrealized-casts&lt;/code&gt;: Resolves any remaining type conversion issues by reconciling unrealized cast operations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu-to-llvm { use-bare-pointers-for-host use-bare-pointers-for-kernels }&lt;/code&gt;: Converts remaining GPU dialect operations to LLVM dialect, using bare pointers for both host and device code.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After lowering our linalg operations to affine loops with &lt;code&gt;convert-linalg-to-affine-loops&lt;/code&gt; we get the following MLIR module:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the &lt;code&gt;convert-affine-for-to-gpu&lt;/code&gt; pass. This pass transforms affine loops into GPU kernel launch region.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.subi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10_1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.subi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10_1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch&lt;/span&gt;
    blocks&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg8 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg9 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    threads&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg6&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg7&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg11 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg12 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg13 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.terminator&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Applying the &lt;code&gt;gpu-kernel-outlining&lt;/code&gt; pass. This pass extracts the GPU kernel launch region into a separate GPU function which has the GPU intrinsics to reference the block and thread indices.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.container_module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.subi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10_1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.subi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10_1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch_func&lt;/span&gt;  &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;
        blocks in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        threads in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1_3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0_0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  gpu.&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; kernel &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_id_x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_id&lt;/span&gt;  x
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_id_y &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_id&lt;/span&gt;  y
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_id_z &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_id&lt;/span&gt;  z
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;thread_id_x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.thread_id&lt;/span&gt;  x
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;thread_id_y &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.thread_id&lt;/span&gt;  y
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;thread_id_z &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.thread_id&lt;/span&gt;  z
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;grid_dim_x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.grid_dim&lt;/span&gt;  x
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;grid_dim_y &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.grid_dim&lt;/span&gt;  y
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;grid_dim_z &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.grid_dim&lt;/span&gt;  z
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_dim_x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_dim&lt;/span&gt;  x
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_dim_y &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_dim&lt;/span&gt;  y
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_dim_z &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.block_dim&lt;/span&gt;  z
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;block_id_x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;thread_id_x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      gpu.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rest of the passes are fairly mechanical lowering passes that convert the GPU dialect to NVVM dialect. We then get a module with a &lt;code&gt;gpu.module&lt;/code&gt; that contains the GPU kernel.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.container_module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.poison&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.poison&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;19&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;21&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;23&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;27&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch_func&lt;/span&gt;  &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt; blocks in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; threads in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;27&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;27&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; 
    llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  gpu.&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token attribute&quot;&gt;#nvvm.target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;O &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;sm_90&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; features &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;+ptx80&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr)&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.read.ptx.sreg.ctaid.x&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.sext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.read.ptx.sreg.tid.x&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.sext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.getelementptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; llvm.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.fmul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.getelementptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      llvm.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;
      llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can extract the GPU function from the module.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@square_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr)&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.read.ptx.sreg.ctaid.x&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.sext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;nvvm.read.ptx.sreg.tid.x&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.sext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.getelementptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; llvm.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.fmul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mul&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.getelementptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        llvm.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;
        llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then translate the MLIR module to LLVM IR.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;; ModuleID = &#39;LLVMDialectModule&#39;
source_filename = &amp;quot;LLVMDialectModule&amp;quot;

define ptx_kernel void @square_kernel(i64 %0, i64 %1, ptr %2, ptr %3) {
  %5 = call i32 @llvm.nvvm.read.ptx.sreg.ctaid.x()
  %6 = sext i32 %5 to i64
  %7 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
  %8 = sext i32 %7 to i64
  %9 = add i64 %0, %6
  %10 = add i64 %1, %8
  %11 = mul i64 %9, 10
  %12 = add i64 %11, %10
  %13 = getelementptr float, ptr %2, i64 %12
  %14 = load float, ptr %13, align 4
  %15 = fmul float %14, %14
  %16 = mul i64 %9, 10
  %17 = add i64 %16, %10
  %18 = getelementptr float, ptr %3, i64 %17
  store float %15, ptr %18, align 4
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare noundef i32 @llvm.nvvm.read.ptx.sreg.ctaid.x() #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare noundef i32 @llvm.nvvm.read.ptx.sreg.tid.x() #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}

!0 = !{i32 2, !&amp;quot;Debug Info Version&amp;quot;, i32 3}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then compile to PTX using the &lt;code&gt;nvptx&lt;/code&gt; backend to LLVM, which we invokve via &lt;code&gt;llc&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;//
// Generated by LLVM NVPTX Back-End
//

.version 7.8
.target sm_90
.address_size 64

	// .globl	square_kernel           // -- Begin function square_kernel
                                        // @square_kernel
.visible .entry square_kernel(
	.param .u64 square_kernel_param_0,
	.param .u64 square_kernel_param_1,
	.param .u64 .ptr .align 1 square_kernel_param_2,
	.param .u64 .ptr .align 1 square_kernel_param_3
)
{
	.reg .b32 	%r&amp;lt;3&amp;gt;;
	.reg .f32 	%f&amp;lt;3&amp;gt;;
	.reg .b64 	%rd&amp;lt;15&amp;gt;;

// %bb.0:
	ld.param.u64 	%rd1, [square_kernel_param_0];
	ld.param.u64 	%rd2, [square_kernel_param_3];
	cvta.to.global.u64 	%rd3, %rd2;
	ld.param.u64 	%rd4, [square_kernel_param_1];
	ld.param.u64 	%rd5, [square_kernel_param_2];
	cvta.to.global.u64 	%rd6, %rd5;
	mov.u32 	%r1, %ctaid.x;
	cvt.s64.s32 	%rd7, %r1;
	mov.u32 	%r2, %tid.x;
	cvt.s64.s32 	%rd8, %r2;
	add.s64 	%rd9, %rd1, %rd7;
	add.s64 	%rd10, %rd4, %rd8;
	mad.lo.s64 	%rd11, %rd9, 10, %rd10;
	shl.b64 	%rd12, %rd11, 2;
	add.s64 	%rd13, %rd6, %rd12;
	ld.global.f32 	%f1, [%rd13];
	mul.rn.f32 	%f2, %f1, %f1;
	add.s64 	%rd14, %rd3, %rd12;
	st.global.f32 	[%rd14], %f2;
	ret;
                                        // -- End function
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;mlir-pipeline&quot; tabindex=&quot;-1&quot;&gt;MLIR Pipeline&lt;/h2&gt;
&lt;p&gt;Now we can construct two modules &lt;code&gt;compile.py&lt;/code&gt; and &lt;code&gt;run.py&lt;/code&gt; module implements our GPU code generation pipeline, transforming high-level MLIR into optimized PTX assembly.&lt;/p&gt;
&lt;p&gt;The full source code is available &lt;a href=&quot;https://github.com/sdiehl/gpu-offload&quot;&gt;on Github&lt;/a&gt; and also as a &lt;a href=&quot;https://github.com/sdiehl/gpu-offload/blob/main/Minimal.ipynb&quot;&gt;notebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In our example we define a primary function &lt;code&gt;compile_mlir_to_ptx&lt;/code&gt; that orchestrates the entire compilation process. Inside, it first parses the MLIR string into a module representation, then applies our GPU compilation pipeline through a carefully ordered sequence of transformation passes. These passes progressively lower the code from high-level tensor operations to GPU-specific constructs, including bufferization to handle memory accesses, converting linalg operations to affine loops, mapping those loops to GPU blocks and threads, and finally generating NVIDIA-specific code via the NVVM dialect. After transformations, the function extracts the GPU module and converts it to PTX assembly using LLVM tools, resulting in code that can be directly executed on NVIDIA GPUs without further compilation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; subprocess

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Context&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Module
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;passmanager &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PassManager


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_mlir_to_ptx&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_module_str&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sm_75&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Compiles MLIR module string to PTX code.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; Context&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Parse the input module&lt;/span&gt;
        module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_module_str&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Apply GPU compilation pipeline&lt;/span&gt;
        module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; gpu_module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply_gpu_pipeline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip_type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Generate PTX from the GPU module&lt;/span&gt;
        ptx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; generate_ptx&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gpu_module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip_type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ptx


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_gpu_pipeline&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sm_75&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Applies the GPU compilation pipeline to the MLIR module.&quot;&quot;&quot;&lt;/span&gt;
    pm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PassManager&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;enable_ir_printing&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;print_after_change&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;canonicalize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;one-shot-bufferize{ bufferize-function-boundaries function-boundary-type-conversion=identity-layout-map }&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;canonicalize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;convert-linalg-to-affine-loops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;func.func(affine-loop-invariant-code-motion)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;func.func(convert-affine-for-to-gpu)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-kernel-outlining&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lower-affine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-decompose-memrefs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;expand-strided-metadata&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;normalize-memrefs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;gpu.module(convert-gpu-to-nvvm{index-bitwidth=0 use-bare-ptr-memref-call-conv })&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;nvvm-attach-target&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;chip&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;chip_type&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; features&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;ptx80 O&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;convert-nvvm-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;reconcile-unrealized-casts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpu-to-llvm { use-bare-pointers-for-host use-bare-pointers-for-kernels }&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Extract the GPU module&lt;/span&gt;
    gpu_module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_gpu_module&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; gpu_module


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;extract_gpu_module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Module&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Extracts the GPU module from a transformed MLIR module.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Navigate the operation tree to find the GPU module&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Structure: module -&gt; region[0] -&gt; block[0] -&gt; operations[1] (GPU host-device code)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# -&gt; region[0] -&gt; block[0] -&gt; operations[0] (GPU module)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        main_func_op &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operation&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;regions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        gpu_module_op &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; main_func_op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;regions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Create a new module from the GPU module operation&lt;/span&gt;
        gpu_module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gpu_module_op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; gpu_module
    &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;IndexError&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AttributeError&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Failed to extract GPU module: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; e


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;generate_ptx&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gpu_module_str&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chip_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sm_75&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Generates PTX from an MLIR GPU module string.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# First convert MLIR to LLVM IR&lt;/span&gt;
    llvm_ir_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mlir-translate&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;--mlir-to-llvmir&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;gpu_module_str&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        capture_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        text&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; llvm_ir_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;returncode &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Error generating LLVM IR:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stderr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;

    llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm_ir_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout

    &lt;span class=&quot;token comment&quot;&gt;# Then convert LLVM IR to PTX&lt;/span&gt;
    ptx_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;llc&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;-march=nvptx64&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;-mcpu=&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;chip_type&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        capture_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        text&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ptx_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;returncode &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Error generating PTX:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptx_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stderr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ptx_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can define the &lt;code&gt;run_kernel&lt;/code&gt; function which serves as the interface between our Python code and the GPU hardware, managing the execution of our compiled PTX kernels. It takes a PTX code string, kernel name, arguments with their types, and the grid and block dimensions that define our parallelization strategy. The function first loads the PTX code into a CUDA module using the CUDA driver API, then retrieves a handle to the named kernel function. After preparing the kernel arguments in the format expected by the CUDA runtime, it launches the kernel with the specified execution configuration, determining how many thread blocks and threads per block will process our data.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    ptx_code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    kernel_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    arg_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    grid_dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    block_dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Run a PTX kernel.&quot;&quot;&quot;&lt;/span&gt;
    module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleLoadData&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptx_code&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    kernel_func &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleGetFunction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; kernel_name&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    kernel_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuLaunchKernel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            kernel_func&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            grid_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            block_dims&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# shared memory bytes&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# stream&lt;/span&gt;
            kernel_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# kernel args&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# extra&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuCtxSynchronize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    checkCudaErrors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuModuleUnload&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following code is a complete end-to-end example of GPU kernel execution using our compilation pipeline. It first compiles MLIR code from a file into PTX assembly, then sets up a CUDA context and allocates memory on both host and device. The example prepares a square matrix operation where each thread processes one element, organizing computation with a grid dimension matching the matrix rows and block dimension matching the columns. After copying input data to the GPU, it executes the kernel with the appropriate arguments and dimensions, then retrieves the results back to the host into a NumPy array.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cu
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cudart &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cudart
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nvrtc &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; nvrtc
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; subprocess
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;compile&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; compile_mlir_to_ptx
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; run &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; run_kernel

ptx_code &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; compile_mlir_to_ptx&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;square.mlir&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

cuda_context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; setup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Allocate device memory&lt;/span&gt;
    d_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; allocate_device_memory&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    output_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    d_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; allocate_device_memory&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy input data to device&lt;/span&gt;
    copy_host_to_device&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d_input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Run kernel&lt;/span&gt;
    grid_dims &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# One thread block per row&lt;/span&gt;
    block_dims &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# One thread per column&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare arguments according to the PTX code&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# square_kernel(&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#     .param .u64 square_kernel_param_0,                // Grid dimension offset&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#     .param .u64 square_kernel_param_1,                // Block dimension offset&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#     .param .u64 .ptr .align 1 square_kernel_param_2,  // Input pointer&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#     .param .u64 .ptr .align 1 square_kernel_param_3   // Output pointer&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# )&lt;/span&gt;
    args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Grid dimension offset&lt;/span&gt;
        &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Block dimension offset&lt;/span&gt;
        d_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Input pointer&lt;/span&gt;
        d_output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Output pointer&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    arg_types &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_int&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_int&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Using None for pointer types&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Running kernel on GPU...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    run_kernel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        ptx_code&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;square_kernel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        arg_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        grid_dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        block_dims&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy results back to host&lt;/span&gt;
    copy_device_to_host&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d_output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Verify results&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Verifying results...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;assert_allclose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output_data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expected_output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rtol&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Success! Results verified.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Clean up resources&lt;/span&gt;
    free_device_memory&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d_input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    free_device_memory&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d_output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    cleanup_cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cuda_context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that&#39;s it, it&#39;s the skeleton of a very tiny compiler pipeline which takes dynamic tensor expressions in MLIR and then compiles them to PTX and executes them on the GPU. Now our goal moving forward is to target that same MLIR from something more natural to program in like a eDSL in Python to express kernel operations and then have our pipeline take that and compile it to MLIR and them for the GPU. More on this in the next section.&lt;/p&gt;
&lt;h2 id=&quot;embedding-binaries&quot; tabindex=&quot;-1&quot;&gt;Embedding Binaries&lt;/h2&gt;
&lt;p&gt;As an aside, the alternative path to compiling a GPU kernel is to compile it to a binary and then embed that binary in the MLIR module and then have the host code load the binary and launch the kernel.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;gpu.binary&lt;/code&gt; operation in MLIR represents a compiled GPU kernel that can be loaded and executed on a GPU device. It encapsulates the binary representation of GPU code, which can be in various formats such as PTX assembly, CUBIN binary, or HSACO for AMD GPUs. This operation is particularly useful when you want to embed pre-compiled GPU kernels directly into your MLIR module, allowing for direct execution without going through the compilation pipeline at runtime.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;gpu-module-to-binary&lt;/code&gt; pass is a transformation that converts GPU modules into GPU binaries. This pass scans through the MLIR module to find all nested GPU modules and serializes them according to the target attributes attached to each module. It produces a GPU binary with an object for every target architecture specified. The pass supports various output formats including offloading representation, assembly code, binaries, and fatbinaries.&lt;/p&gt;
&lt;p&gt;All this is doing under the hood is running &lt;code&gt;ptxas&lt;/code&gt; on the PTX code and embedding the output in the MLIR module with the given arguments the &lt;code&gt;gpu-module-to-binary&lt;/code&gt; pass passed to the &lt;code&gt;ptxas&lt;/code&gt; command. For exmaple the following MLIR module contains a GPU binary for the &lt;code&gt;sm_70&lt;/code&gt; architecture.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.container_module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.binary&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@vecadd_kernel&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token attribute&quot;&gt;#gpu.object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token attribute&quot;&gt;#nvvm.target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;chip &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;sm_70&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    offload &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;BC&#92;C0&#92;DE5&#92;14 [... truncated ...] kernel20.1.0nvptx64-nvidia-cudaLLVMDialectModule&#92;00&#92;00&#92;00&#92;00&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then in the host code you can launch the kernel with the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;gpu.launch_func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@vecadd_kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;@vecadd_kernel&lt;/span&gt;
  blocks in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  threads in &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
  args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/cuda-installation-guide-linux/&quot;&gt;CUDA Installation Guide for Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html&quot;&gt;CUDA C Programming Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/&quot;&gt;CUDA C Best Practices Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/archive/12.6.0/&quot;&gt;CUDA Toolkit Documentation 12.6&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://nvidia.github.io/cuda-python/cuda-bindings/latest/api.html&quot;&gt;CUDA Python API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://nvidia.github.io/cuda-python/cuda-bindings/latest/module/driver.html#cuda.bindings.driver.cuLaunchKernel&quot;&gt;CUDA Python launchKernel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.nvidia.com/blog/cuda-refresher-cuda-programming-model/&quot;&gt;CUDA Refresher: The CUDA Programming Model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/nvvm-ir-spec/&quot;&gt;NVVM IR Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://llvm.org/docs/NVPTXUsage.html&quot;&gt;LLVM User Guide for NVPTX Back-end&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/cuda-quick-start-guide/#linux&quot;&gt;Cuda Quick Start Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=V3Q9IjsgXvA&quot;&gt;2024 EuroLLVM - Zero to Hero: Programming Nvidia Hopper Tensor Core with MLIR&#39;s NVGPU Dialect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://llvm.org/docs/Docker.html&quot;&gt;A guide to Dockerfiles for building LLVM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.nvidia.com/blog/understanding-ptx-the-assembly-language-of-cuda-gpu-computing/&quot;&gt;Understanding PTX: The Assembly Language of CUDA GPU Computing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.nvidia.com/cuda/nvmath-python/latest/bindings/cublas.html&quot;&gt;cuBLAS (nvmath.bindings.cublas)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://nvidia.github.io/cuda-python/cuda-core/latest/&quot;&gt;cuda.core - Pythonic access to CUDA Runtime&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Sat, 19 Apr 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_gpu/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_gpu/</guid>
    </item>
    
    <item>
      <title>MLIR Part 7 - Transformers</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/arch.jpeg&quot; width=&quot;500px&quot; alt=&quot;Transformer Architecture&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;transformers&quot; tabindex=&quot;-1&quot;&gt;Transformers&lt;/h1&gt;
&lt;p&gt;The transformer architecture revolutionized the field of natural language processing when introduced in the landmark 2017 paper &lt;a href=&quot;https://arxiv.org/abs/1706.03762&quot;&gt;&lt;em&gt;Attention is All You Need&lt;/em&gt;&lt;/a&gt;. Breaking away from traditional sequence models, transformers employ &lt;strong&gt;self-attention&lt;/strong&gt; mechanisms (more on this later) as their core building block, enabling them to capture long-range dependencies in data with remarkable efficiency. In essence, the transformer can be viewed as a general-purpose computational substrate—a programmable logical tissue that reconfigures based on training data and can be stacked as layers build large models exhibiting fascinating emergent behaviors.&lt;/p&gt;
&lt;p&gt;Each of these layers contains two essential sublayers working in tandem: a &lt;strong&gt;multi-head self-attention mechanism&lt;/strong&gt; and a &lt;strong&gt;position-wise feed-forward network&lt;/strong&gt;. The multi-head attention allows the model to simultaneously focus on different parts of the input sequence, capturing various relationships between tokens from different representational perspectives. The feed-forward network, applied identically to each position, then introduces non-linearity and expands the model&#39;s capacity to learn complex patterns.&lt;/p&gt;
&lt;p&gt;Since the attention mechanism processes all tokens in parallel, transformers require an innovative approach to handling sequential information. This challenge is addressed through positional encodings—either learned embeddings or fixed sinusoidal functions—that are added to the token embeddings at the input layer. These encodings provide the model with crucial information about token positions that would otherwise be lost in the parallel processing.&lt;/p&gt;
&lt;p&gt;The power of transformers stems from their ability to dynamically weight the importance of different tokens during processing. Consider the word &amp;quot;bank&amp;quot; in the sentence &amp;quot;I went to the bank to deposit money.&amp;quot; When processing this ambiguous word, the model can attend strongly to contextual clues like &amp;quot;deposit&amp;quot; and &amp;quot;money,&amp;quot; helping it correctly interpret &amp;quot;bank&amp;quot; as a financial institution rather than a riverside. This contextual awareness enables transformers to excel at tasks requiring nuanced language understanding.&lt;/p&gt;
&lt;p&gt;Layer normalization and residual connections further enhance the transformer architecture. Layer normalization stabilizes the learning process by normalizing activations within each layer, while residual connections create shortcuts for gradient flow during training. These components work together to mitigate the vanishing gradient problem that often plagues deep networks, allowing transformers to be trained effectively despite their depth.&lt;/p&gt;
&lt;p&gt;Remarkably, researchers found that as they scaled up transformer models by increasing their layer count, embedding dimensions, and training data, these architectures demonstrated remarkable emergent capabilities. Larger models exhibit abilities not present in their smaller counterparts, including few-shot learning, reasoning, and even rudimentary understanding of concepts they weren&#39;t explicitly trained on. This scaling effect has driven much of the recent progress in artificial intelligence.&lt;/p&gt;
&lt;p&gt;The decoder-only Transformer architecture which we&#39;ll build from scratch here, is composed of multiple identical decoder blocks stacked vertically. Unlike the original Transformer which had both encoder and decoder components, modern language models typically use only the decoder portion for next-token prediction tasks. This architecture has proven highly scalable, with models ranging from hundreds of millions to now billions of parameters in frontier models.&lt;/p&gt;
&lt;p&gt;Let&#39;s walk through a pure NumPy implementation of the GPT-2 model, component by component. It only comes out to about 100 lines of code in total. The full source code is available &lt;a href=&quot;https://github.com/sdiehl/gpt2-weights&quot;&gt;on GitHub here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are several data structures of the neural network weights that we&#39;ll download from the internet later. For now just know that they&#39;re a collection of arrays of various dimensions.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;LinearParams&lt;/code&gt; - A linear layer with weights and bias parameters for the linear transformations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LayerNormParams&lt;/code&gt; - A layer normalization layer with gain and bias parameters for normalization.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AttentionParams&lt;/code&gt; - A multi-head attention layer containing query, key, value and output projection parameters.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;BlockParams&lt;/code&gt; - A transformer block containing attention and feed-forward parameters.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ModelParams&lt;/code&gt; - The full model parameters including token embeddings, position embeddings, transformer blocks and final layer norm.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HParams&lt;/code&gt; - Hyperparameters for the model including the number of layers, heads, embedding dimension, etc.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In our code we&#39;ll use the following abbreviations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;g&lt;/code&gt; - Gamma (scale parameter for layer normalization)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;b&lt;/code&gt; - Beta (bias parameter)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;w&lt;/code&gt; - Weight matrix/array&lt;/li&gt;
&lt;li&gt;&lt;code&gt;wte&lt;/code&gt; - Word/Token Embeddings&lt;/li&gt;
&lt;li&gt;&lt;code&gt;wpe&lt;/code&gt; - Word Position Embeddings&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ln&lt;/code&gt; - Layer Normalization&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mlp&lt;/code&gt; - Multi-Layer Perceptron&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fc&lt;/code&gt; - Fully Connected layer&lt;/li&gt;
&lt;li&gt;&lt;code&gt;qkv&lt;/code&gt; - Query, Key, Value (attention components)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;attn&lt;/code&gt; - Attention&lt;/li&gt;
&lt;li&gt;&lt;code&gt;proj&lt;/code&gt; - Projection (linear transformation)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;bpe-tokenization&quot; tabindex=&quot;-1&quot;&gt;BPE Tokenization&lt;/h2&gt;
&lt;p&gt;Byte-Pair Encoding (shortened as BPE) is a tokenization algorithm that addresses the challenge of out-of-vocabulary words by breaking words into smaller units called subwords. Initially developed as a data compression technique, BPE was adapted for natural language processing by treating words as sequences of characters. The core idea of BPE is to iteratively merge the most frequent adjacent symbol pairs in a corpus into new subword units. This process begins by splitting each word in the corpus into individual characters, forming the initial vocabulary consisting of all unique characters.&lt;/p&gt;
&lt;p&gt;The BPE algorithm then proceeds with &lt;strong&gt;iterative merging&lt;/strong&gt;. In each iteration, it counts all adjacent symbol pairs across the corpus. The pair with the highest frequency is then selected and merged into a new symbol or subword. This merge operation is applied to all occurrences of the selected pair in the corpus, effectively removing the space between them in the tokenized representations of the words. The newly formed subword is added to the vocabulary. This iterative process continues until the vocabulary reaches a predefined target size or no more frequent pairs can be found. The sequence of merge operations performed during training is crucial for tokenizing new, unseen words.&lt;/p&gt;
&lt;p&gt;Once the BPE tokenizer is trained (i.e., the sequence of merges is determined), it can be used to tokenize new text. When tokenizing a word, the tokenizer first checks if the whole word (with the initial word boundary marker) exists in the vocabulary. If not, it splits the word into characters and iteratively applies the learned merge rules in the order they were learned. Any character not present in the initial character set might be replaced by an unknown token (e.g., &lt;code&gt;&amp;lt;UNK&amp;gt;&lt;/code&gt;). BPE is widely used in modern language models and is particularly useful for handling rare words and languages without clear word boundaries like Chinese, where initial tokenization might start with individual characters.&lt;/p&gt;
&lt;p&gt;We&#39;re not going to implement BPE from scratch, but you can find a simple Python implementation &lt;a href=&quot;https://github.com/karpathy/nanoGPT/blob/master/data/bpe.py&quot;&gt;here&lt;/a&gt;. Instead we&#39;ll just load the existing tokenizer from the original GPT-2 repo.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;encoder &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_encoder&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# encode a string into a list of tokens&lt;/span&gt;
tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; encoder&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# [15496, 995]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we see the tokenizer has encoded the string &amp;quot;Hello world&amp;quot; into a list of tokens &lt;code&gt;[15496, 995]&lt;/code&gt; which happens to have two words and two tokens. However the string &amp;quot;Barack Obama&amp;quot; is encoded into &lt;code&gt;[10374, 441, 2486]&lt;/code&gt; which has two words and three tokens.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/tokenize.png&quot; alt=&quot;Tokenization&quot; width=&quot;500&quot; /&gt;
&lt;/center&gt;
&lt;h2 id=&quot;tokenization-and-position-embeddings&quot; tabindex=&quot;-1&quot;&gt;Tokenization and Position Embeddings&lt;/h2&gt;
&lt;p&gt;From the string input we first tokenize it into a list of tokens and then we look up the token embeddings for each token in the &lt;code&gt;params.wte&lt;/code&gt; matrix.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wpe&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This line is crucial for preparing the input sequence before it gets processed by the main transformer blocks. It combines two types of information for each token in the input (the position and the token itself). In the model configuration there are two large embedding matrices the &lt;em&gt;token&lt;/em&gt; embedding matrix and the &lt;em&gt;position&lt;/em&gt; embedding matrix.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Token Embeddings (&lt;code&gt;params.wte[inputs]&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;inputs&lt;/code&gt; parameter represents your sequence of input tokens as a list of integers (token IDs). For example, if your input text &amp;quot;Hello world&amp;quot; is tokenized into &lt;code&gt;[15496, 995]&lt;/code&gt;, then &lt;code&gt;inputs&lt;/code&gt; would be &lt;code&gt;[15496, 995]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;params.wte&lt;/code&gt; refers to the &lt;strong&gt;W&lt;/strong&gt;ord &lt;strong&gt;T&lt;/strong&gt;oken &lt;strong&gt;E&lt;/strong&gt;mbedding matrix. It&#39;s essentially a large table (lookup table) where each row corresponds to a unique token ID in the model&#39;s vocabulary, and the row itself is a vector (e.g., of size 768 for the small GPT-2 model). This vector is the learned numerical representation, or &amp;quot;embedding,&amp;quot; for that specific token.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;params.wte[inputs]&lt;/code&gt; operation performs a lookup. It takes each integer ID in your &lt;code&gt;inputs&lt;/code&gt; list and retrieves the corresponding embedding vector from the &lt;code&gt;params.wte&lt;/code&gt; matrix. The result is a matrix where each row is the token embedding for the corresponding token in your input sequence. So, for &lt;code&gt;[15496, 995]&lt;/code&gt;, you&#39;d get a matrix with two rows: the embedding for token 15496 and the embedding for token 995.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Position Embeddings (&lt;code&gt;params.wpe[range(len(inputs))]&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Transformers process tokens in parallel using self-attention, which doesn&#39;t inherently know the order of tokens. To provide this sequential information, we use position embeddings. The &lt;code&gt;len(inputs)&lt;/code&gt; function calculates the length of your input sequence (e.g., 2 for &lt;code&gt;[15496, 995]&lt;/code&gt;). Using &lt;code&gt;range(len(inputs))&lt;/code&gt; generates a sequence of position indices starting from 0: &lt;code&gt;[0, 1]&lt;/code&gt; in our example.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;params.wpe&lt;/code&gt; refers to the &lt;strong&gt;W&lt;/strong&gt;ord &lt;strong&gt;P&lt;/strong&gt;osition &lt;strong&gt;E&lt;/strong&gt;mbedding matrix. Similar to &lt;code&gt;wte&lt;/code&gt;, it&#39;s a lookup table, but its rows correspond to positions in a sequence (0, 1, 2, ... up to the maximum sequence length the model can handle). Each row is a learned vector representing that specific position. GPT-2 uses &lt;em&gt;learned&lt;/em&gt; positional embeddings, meaning these vectors are parameters learned during training.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code&gt;params.wpe[range(len(inputs))]&lt;/code&gt; performs a lookup using the position indices. It retrieves the embedding vector for position 0, then position 1, and so on, for the length of your input. The result is a matrix where each row is the position embedding for the corresponding position in the input sequence.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/token_embeddings.png&quot; alt=&quot;Word Token &amp; Word Position Embeddings&quot; width=&quot;500&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;&lt;strong&gt;Addition (&lt;code&gt;+&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The token embedding matrix (representing &lt;em&gt;what&lt;/em&gt; each token is) and the position embedding matrix (representing &lt;em&gt;where&lt;/em&gt; each token is in the sequence) are simply added together element-wise. Both matrices have the same shape (sequence length × embedding dimension).&lt;/p&gt;
&lt;p&gt;The resulting matrix &lt;code&gt;x&lt;/code&gt; now contains vectors where each vector encodes both the token&#39;s identity and its position. This combined representation is the final input that gets fed into the stack of transformer blocks for processing.&lt;/p&gt;
&lt;p&gt;In summary, this line &lt;code&gt;x = params.wte[inputs] + params.wpe[range(len(inputs))]&lt;/code&gt; creates the initial representation for the input sequence by looking up the learned vector for each token and adding the learned vector for its position. This allows the subsequent transformer layers to process the tokens while being aware of their order.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/embedding.png&quot; alt=&quot;Token Embeddings&quot; width=&quot;500&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;The size of the matrix is &lt;code&gt;[sequence_length, embedding_dimension]&lt;/code&gt;. We denote sequence length as &lt;code&gt;N&lt;/code&gt; in the code.&lt;/p&gt;
&lt;h2 id=&quot;gelu&quot; tabindex=&quot;-1&quot;&gt;GELU&lt;/h2&gt;
&lt;p&gt;GELU (Gaussian Error Linear Unit) is an activation function used in the Transformer architecture, particularly within the feed-forward networks (FFN) of each block. Its primary role is to introduce non-linearity into the model, allowing it to learn more complex patterns and relationships in the data. Without non-linear activation functions, stacking multiple layers would be equivalent to a single linear transformation, severely limiting the model&#39;s representational power. In math it&#39;s defined as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{GELU}(x) = 0.5 &#92;cdot x &#92;cdot &#92;left(1 + &#92;text{erf}&#92;left(&#92;frac{x}{&#92;sqrt{2}}&#92;right)&#92;right)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where &lt;code&gt;erf&lt;/code&gt; is the &lt;a href=&quot;https://en.wikipedia.org/wiki/Error_function&quot;&gt;Guass error function&lt;/a&gt;. In practice we&#39;ll use an approximation where we use the &lt;code&gt;tanh&lt;/code&gt; function to approximate the error function so we don&#39;t have to compute integrals involved in the error function. In Python this approximation is implemented as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# gelu:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;gelu&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.044715&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that this is a scalar function that is applied element-wise to the input.&lt;/p&gt;
&lt;h2 id=&quot;softmax&quot; tabindex=&quot;-1&quot;&gt;Softmax&lt;/h2&gt;
&lt;p&gt;While GELU handles non-linearity within feed-forward networks, the transformer architecture also needs a way to convert raw scores into meaningful probability distributions. This is where the softmax function comes into play. Softmax is a crucial function used primarily in two places within the Transformer architecture: within the attention mechanism and at the final output layer. Its purpose is to convert a vector of raw scores (logits) into a probability distribution. It takes an input vector and exponentiates each element, making them all positive, and then normalizes these values by dividing by their sum. The result is a vector where all elements are between 0 and 1, and they all sum up to 1.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{softmax}(x)_i = &#92;frac{e^{x_i}}{&#92;sum_j e^{x_j}}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In the attention mechanism (&lt;code&gt;attention&lt;/code&gt; function), softmax is applied to the scaled dot-product scores between queries and keys. This transforms the raw similarity scores into attention weights, which represent the probability distribution over the value vectors – indicating how much focus each value should receive.&lt;/p&gt;
&lt;p&gt;At the final layer of the model (just before generating the next token), softmax is typically applied to the logits produced by the final linear projection. This converts the model&#39;s raw output scores for each word in the vocabulary into probabilities, allowing us to interpret the scores and sample the next token based on this distribution.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# softmax:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;softmax&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    exp_x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; exp_x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;exp_x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;layer-normalization&quot; tabindex=&quot;-1&quot;&gt;Layer Normalization&lt;/h2&gt;
&lt;p&gt;Beyond activation functions and probability conversions, neural networks need methods to stabilize training, especially in deep architectures like transformers. Layer Normalization (LayerNorm) serves this critical purpose. Unlike Batch Normalization which normalizes across the batch dimension, Layer Normalization normalizes the inputs across the features (embedding dimension) for &lt;em&gt;each&lt;/em&gt; data sample independently.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{LayerNorm}(x) = &#92;gamma &#92;cdot &#92;frac{x - &#92;mu}{&#92;sqrt{&#92;sigma^2}} + &#92;beta&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where $&#92;mu$ is the mean and $&#92;sigma^2$ is the variance of the input, and $&#92;gamma$ and $&#92;beta$ are learnable gain and bias parameters.&lt;/p&gt;
&lt;p&gt;In a Transformer block, LayerNorm is typically applied &lt;em&gt;before&lt;/em&gt; the multi-head attention sub-layer and &lt;em&gt;before&lt;/em&gt; the feed-forward network sub-layer (this is known as pre-LN). It works by calculating the mean and variance of all the activations within a single layer for a single training example. It then normalizes the activations using this mean and variance. Crucially, it also introduces learnable gain (&lt;code&gt;g&lt;/code&gt;) and bias (&lt;code&gt;b&lt;/code&gt;) parameters, allowing the network to scale and shift the normalized output, preserving its representational capacity.&lt;/p&gt;
&lt;p&gt;LayerNorm helps mitigate issues like vanishing or exploding gradients, allows for higher learning rates, and makes the model less sensitive to the scale of the parameters and initialization. By normalizing the activations within each layer, it ensures that the inputs to the subsequent layers are consistently scaled, leading to smoother and more stable training dynamics.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# layer_norm:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   g : (768,)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   b : (768,)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;layer_norm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-5&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mean &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;variance &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; eps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;linear-projection&quot; tabindex=&quot;-1&quot;&gt;Linear Projection&lt;/h2&gt;
&lt;p&gt;With normalization in place to stabilize the network, transformers need a way to transform representations from one space to another. This is where linear layers come in. The linear layer, also known as a fully connected or dense layer, is one of the most fundamental components in neural networks. It performs a linear transformation on the input data.&lt;/p&gt;
&lt;p&gt;Mathematically, it computes &lt;code&gt;output = input @ weights + bias&lt;/code&gt;, where &lt;code&gt;@&lt;/code&gt; represents matrix multiplication. The &lt;code&gt;input&lt;/code&gt; is the data fed into the layer, &lt;code&gt;weights&lt;/code&gt; is a matrix of learnable parameters, and &lt;code&gt;bias&lt;/code&gt; is a learnable vector that gets added to the result. The weights matrix effectively projects the input vector from its original dimension to a new dimension (defined by the shape of the weights matrix), and the bias allows for shifting the output.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# linear:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   w : (768, 3072)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   b : (3072,)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 3072)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;linear&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x @ w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;feed-forward-network&quot; tabindex=&quot;-1&quot;&gt;Feed-Forward Network&lt;/h2&gt;
&lt;p&gt;Having covered the individual building blocks of normalization, activation, and linear transformation, we can now examine how these components come together in the Feed-Forward Network (FFN). The FFN, sometimes called the position-wise feed-forward network, is the second main sub-layer within each Transformer block (following the multi-head attention sub-layer).&lt;/p&gt;
&lt;p&gt;It consists of two linear transformations with a non-linear activation function (the GELU function we explored earlier) applied in between. The first linear layer usually expands the dimensionality of the input (e.g., from 768 to 3072 in GPT-2), the GELU activation introduces non-linearity, and the second linear layer projects the result back down to the original input dimensionality (e.g., 3072 back to 768).&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/ffn.png&quot; alt=&quot;Feed-Forward Network&quot; width=&quot;200&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;This FFN is applied independently to each position in the sequence. While the attention mechanism allows tokens to interact with each other, the FFN processes the information at each position separately, transforming the representation based on the features learned by the model. It adds significant representational capacity to the model, allowing it to learn more complex functions beyond what the attention mechanism alone can capture.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# ffn:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   c_fc_w : (768, 3072)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   c_fc_b : (3072,)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   c_proj_w : (3072, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   c_proj_b : (768,)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ffn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    c_fc_w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    c_fc_b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    c_proj_w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    c_proj_b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gelu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_fc_w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_fc_b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_proj_w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_proj_b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;attention&quot; tabindex=&quot;-1&quot;&gt;Attention&lt;/h2&gt;
&lt;p&gt;Attention is the core idea of the transformer architecture. It is a mechanism inspired by human visual attention. Just as we focus on certain parts of an image or specific words in a sentence while ignoring others, attention allows a model to dynamically weigh the importance of different parts of the input data when processing a particular element. For sequential data like text, this means that when the model is processing one word, it can &amp;quot;pay attention&amp;quot; to other relevant words in the sequence, regardless of their distance, to better understand the context and meaning. This selective focus helps the model capture long-range dependencies and relationships more effectively than older sequence models.&lt;/p&gt;
&lt;p&gt;At the heart of each attention calculation are three key matrices derived from the input sequence:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Query (Q):&lt;/strong&gt; Represents the current token or position we are processing. Think of it as asking: &amp;quot;What specific information am I looking for right now?&amp;quot;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key (K):&lt;/strong&gt; Represents all the tokens in the sequence (including the current one). Think of it as labels or identifiers for the information contained in each token: &amp;quot;What kind of information does each token offer?&amp;quot;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value (V):&lt;/strong&gt; Also represents all tokens in the sequence. Think of it as the actual content or representation of each token: &amp;quot;What is the actual information conveyed by each token?&amp;quot;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The process begins with an initial projection where the input sequence &lt;code&gt;x&lt;/code&gt; passes through three separate linear layers to generate initial Q, K, and V matrices spanning the entire input dimension. These large matrices are then split into smaller chunks along their embedding dimension, creating separate Q, K, and V sets for each attention &amp;quot;head&amp;quot;. Formally self-attention is computed as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{Attention}(Q,K,V)=&#92;text{softmax} &#92;left( &#92;frac{QK^T}{&#92;sqrt{d_k}} &#92;right) &#92;cdot V&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Q&lt;/code&gt;, &lt;code&gt;K&lt;/code&gt;, &lt;code&gt;V&lt;/code&gt; are the query, key, and value matrices, and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;d_k&lt;/code&gt; is the dimension of the key vectors.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;attention&lt;/code&gt; function implements the core scaled dot-product attention mechanism, the fundamental building block used within each head of the Multi-Head Attention layer. It calculates how much focus or &amp;quot;attention&amp;quot; each input token (represented by its Value vector) should receive when processing a specific token (represented by its Query vector).&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/attention_diagram.png&quot; alt=&quot;Attention&quot; width=&quot;400&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;The process starts by computing the dot product between the Q matrix and the transpose of the K matrix (&lt;code&gt;q @ k.T&lt;/code&gt;). This step measures the raw similarity or compatibility between each query and all keys. These raw scores are then scaled down by dividing them by the square root of the dimension of the key vectors (&lt;code&gt;np.sqrt(q.shape[-1])&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The causal mask is a mask that is added to these scaled scores. This mask prevents positions from attending to subsequent positions, ensuring predictions are based only on previous tokens. The mask effectively assigns very large negative values to scores corresponding to future tokens.&lt;/p&gt;
&lt;p&gt;Next, a &lt;code&gt;softmax&lt;/code&gt; function is applied to the masked, scaled scores. This converts the scores into probability distributions (attention weights) where each weight indicates the relative importance of a specific key (and its corresponding value) to the query.&lt;/p&gt;
&lt;p&gt;Finally, the function computes the matrix product of these attention weights and the V matrix (&lt;code&gt;attention_weights @ v&lt;/code&gt;). This yields the output of the attention mechanism: a weighted sum of the Value vectors, where the weights are the calculated attention probabilities. The resulting vector for each query is thus a blend of information from the input sequence, emphasizing the most relevant parts based on the query-key interactions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# attention:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   q : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   k : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   v : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   mask : (N, N)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 64)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    q&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mask&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    attention_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q @ k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; mask
    attention_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attention_scores&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; attention_weights @ v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can visualize the attention mechanism by plotting the attention weights for each head. For example for the phrase &lt;em&gt;&amp;quot;The quick brown fox jumps over the lazy dog&amp;quot;&lt;/em&gt; we can plot the attention weights in the first block of the transformer stack to see which pairs of words are attended to most by each head. Here we see &lt;em&gt;&amp;quot;jumps&amp;quot;&lt;/em&gt; and &lt;em&gt;&amp;quot;fox&amp;quot;&lt;/em&gt; are attended to most by the first head.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/attention.png&quot; alt=&quot;Attention&quot; width=&quot;800px&quot; /&gt;
&lt;/center&gt;
&lt;h2 id=&quot;multi-head-attention&quot; tabindex=&quot;-1&quot;&gt;Multi-Head Attention&lt;/h2&gt;
&lt;p&gt;While the basic attention mechanism provides a way to capture relationships between tokens, transformers take this a step further with Multi-Head Attention. This enhancement allows the model to capture different types of relationships simultaneously, greatly expanding its representational power. Multi-Head Attention is a core component that allows the model to jointly attend to information from different representation subspaces at different positions. Instead of performing a single attention calculation, MHA runs the attention mechanism multiple times (&amp;quot;heads&amp;quot;) in parallel on different learned linear transformations of the input. This allows the model to capture various types of relationships (e.g., syntactic, semantic) simultaneously.&lt;/p&gt;
&lt;p&gt;Within each head, scaled dot-product attention is performed. Attention scores are computed by taking the dot product of the Q with all Ks, measuring the relevance between the current token and every other token. These scores are scaled (divided by the square root of the key dimension) and passed through a softmax function to become probabilities (attention weights). These weights indicate how much focus to place on each V.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/mha.png&quot; alt=&quot;Multi-Head Attention&quot; width=&quot;400&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;The size of each attention head&#39;s query matrix (64) is derived by dividing the model&#39;s embedding dimension (768) by the number of attention heads (12). When the outputs of all heads are concatenated back together, we recover the original embedding dimension:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;n_head &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The attention weights are then used to multiply the corresponding V vectors, producing a weighted sum. This creates an output vector for the head that blends information from the most relevant tokens. Finally, the output vectors from all attention heads are concatenated back together. This combined vector is passed through a final linear layer to produce the overall output of the Multi-Head Attention block. This multi-faceted approach enables the model to capture richer contextual information by looking at the sequence from multiple &amp;quot;perspectives&amp;quot; concurrently.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# mha:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mha&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_attn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_proj&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# qkv projection&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [N, 768] -&gt; [N, 3*768]&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# split into qkv&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [N, 3*768] -&gt; [3, N, 768]&lt;/span&gt;
    qkv &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# split into heads&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [3, N, 768] -&gt; [3, n_head, N, 64]&lt;/span&gt;
    qkv_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; qkv&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# apply causal mask to hide future inputs&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [N, N]&lt;/span&gt;
    causal_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tri&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e10&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# perform attention on each head&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [3, n_head, N, 64] -&gt; [n_head, N, 64]&lt;/span&gt;
    out_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;attention&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; causal_mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;qkv_heads&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# merge heads&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [n_head, N, 64] -&gt; [N, 768]&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;out_heads&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# out projection&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# [N, 768] -&gt; [N, 768]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; linear&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;transformer-block&quot; tabindex=&quot;-1&quot;&gt;Transformer Block&lt;/h2&gt;
&lt;p&gt;With both the attention mechanism and feed-forward networks defined, we can now see how these components are integrated into the complete Transformer block. The Transformer block is the fundamental repeating unit of the GPT-2 architecture, combining multi-head attention with feed-forward processing, normalization, and residual connections to create a computational unit that can be stacked multiple times.&lt;/p&gt;
&lt;p&gt;Each block takes a sequence of input vectors and processes them through two main sub-layers, incorporating residual connections and layer normalization around each. This structure creates a balanced flow of information that allows both preservation of original inputs (through residual connections) and transformation into new representations.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/transformers.png&quot; alt=&quot;Transformer Block Sequence&quot; width=&quot;600px&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;The first sub-layer is the Multi-Head Attention mechanism we just explored. Before the input enters the MHA, it undergoes layer normalization. The output of the MHA is then added back to the original input (a residual connection), combining the attention-derived context with the original positional representation. This sum then passes to the second sub-layer.&lt;/p&gt;
&lt;p&gt;The second sub-layer is the Feed-Forward Network described earlier. Similar to the first sub-layer, the input (which is the output of the first residual connection) first goes through layer normalization. The normalized output is then processed by the FFN. The output of the FFN is then added back to its input via another residual connection. The final output of the transformer block is this sum, which then serves as the input to the next identical transformer block in the stack.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# transformer_block:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   x : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#   out : (N, 768)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;transformer_block&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    mlp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MLPParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    attn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; AttentionParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    ln_1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LayerNormParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    ln_2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LayerNormParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    n_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# First sub-block: Layer norm -&gt; Attention -&gt; Residual&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; layer_norm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ln_1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ln_1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mha&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_attn&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_attn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_proj&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;n_head&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; a

    &lt;span class=&quot;token comment&quot;&gt;# Second sub-block: Layer norm -&gt; FFN -&gt; Residual&lt;/span&gt;
    m &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; layer_norm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ln_2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ln_2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    m &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ffn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        c_fc_w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_fc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        c_fc_b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_fc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        c_proj_w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        c_proj_b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; m

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;full-model&quot; tabindex=&quot;-1&quot;&gt;Full Model&lt;/h2&gt;
&lt;p&gt;Now that we&#39;ve examined the individual components and the Transformer block structure, we can assemble everything into a complete model. These blocks are stacked multiple times (e.g., 12 times in the smallest GPT-2 model) to build a deep network capable of sophisticated language understanding and generation. The combination of attention mechanisms, feed-forward networks, layer normalization, and residual connections creates an architecture that effectively processes sequential information, captures complex dependencies, and trains stably despite its depth.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;gpt2&lt;/code&gt; function represents the complete forward pass of the GPT-2 model for a given sequence of input token IDs. It orchestrates the flow of data through all the previously defined components, transforming raw token IDs into contextual representations and ultimately into predictions for the next token.&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/gpt2.png&quot; alt=&quot;GPT-2 Model&quot; width=&quot;300&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;The journey begins with embedding the input tokens. This is achieved by summing the token embeddings (looked up in &lt;code&gt;params.wte&lt;/code&gt;) and the positional embeddings (looked up in &lt;code&gt;params.wpe&lt;/code&gt;) for each token in the input sequence. This initial combined embedding captures both the semantic meaning of each token and its position in the sequence, providing the foundation for all subsequent processing.&lt;/p&gt;
&lt;p&gt;This embedded sequence then flows through the stack of Transformer blocks. Each block refines the representation through its attention and feed-forward mechanisms, gradually building more sophisticated contextual understanding. The code explicitly implements this stacking for a 12-layer model, where the output of one block becomes the input to the next. With each successive layer, the model can capture increasingly complex patterns and relationships in the data.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;gpt2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ModelParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Get token embeddings and position embeddings&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wpe&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Apply transformer block stack ( 12 blocks total )&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; transformer_block&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            n_head&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;n_head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            mlp&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            attn&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            ln_1&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            ln_2&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Apply final layer norm and project to vocabulary&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; layer_norm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x @ params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wte&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T  &lt;span class=&quot;token comment&quot;&gt;# Project to vocabulary&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; logits
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After processing through all the transformer blocks, the resulting sequence undergoes a final layer normalization to stabilize the values. This normalized output with shape &lt;code&gt;(N, 768)&lt;/code&gt; (for GPT-2 small) represents the final contextual embeddings for each token in the sequence. To convert these embeddings into predictions over the vocabulary, the model performs a projection using the transposed token embedding matrix (&lt;code&gt;params.wte.T&lt;/code&gt;). This clever weight-sharing technique (using the same embedding matrix for both input embedding and output projection) reduces the total parameter count.&lt;/p&gt;
&lt;p&gt;The result is a tensor of logits with shape &lt;code&gt;(N, 50257)&lt;/code&gt; (for GPT-2), where 50,257 is the vocabulary size. These logits represent the unnormalized prediction scores for each possible next token at each position in the sequence. Every token in the vocabulary receives a score, and these scores can be ranked to determine which tokens are most likely to follow in the sequence. A typical example of these logits for a single position might look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;114.83902718&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;111.23177705&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;116.58203861&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;118.4023539&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;118.92616557&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;113.37047973&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shape&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50257&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For autoregressive generation, we typically only care about predicting the next token after the end of our current sequence. By selecting the logits corresponding to the last position and applying an operation like &lt;code&gt;argmax&lt;/code&gt;, we can identify the most likely next token:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;next_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This token ID can then be appended to our sequence, and the process repeated to generate text one token at a time. But for this approach to work, we need properly initialized model parameters loaded from pre-trained weights, which brings us to our next topic.&lt;/p&gt;
&lt;h2 id=&quot;safetensors-and-model-parameters&quot; tabindex=&quot;-1&quot;&gt;Safetensors and Model Parameters&lt;/h2&gt;
&lt;p&gt;To utilize pre-trained models like GPT-2, we need to load and organize their parameters effectively. This section explains how we structure and load these parameters using the safetensors format, a secure and efficient method for storing model weights. For that we load them from HuggingFace in which they are stored in the &lt;code&gt;model.safetensors&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;Safetensors is a fast and safe (i.e. it doesn&#39;t use Pickle, which requires execution of arbitrary code) format for storing tensors. The format uses a simple key/value structure where keys are UTF-8 encoded strings representing tensor names (e.g. &lt;code&gt;model.layers.0.attention.weight&lt;/code&gt;), values are binary tensor data with a fixed header containing shape and dtype information, and a metadata section at the start of the file contains an index of all tensors and their offsets.&lt;/p&gt;
&lt;p&gt;Internally the model parameters for GPT-2 are stored in the following key/value format:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;wpe.weight&quot;&lt;/span&gt;             &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;wte.weight&quot;&lt;/span&gt;             &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;50257&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.attn.bias&quot;&lt;/span&gt;          &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.attn.c_attn.bias&quot;&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2304&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.attn.c_attn.weight&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2304&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.attn.c_proj.bias&quot;&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.attn.c_proj.weight&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.ln_1.bias&quot;&lt;/span&gt;          &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.ln_1.weight&quot;&lt;/span&gt;        &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.ln_2.bias&quot;&lt;/span&gt;          &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.ln_2.weight&quot;&lt;/span&gt;        &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.mlp.c_fc.bias&quot;&lt;/span&gt;      &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.mlp.c_fc.weight&quot;&lt;/span&gt;    &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.mlp.c_proj.bias&quot;&lt;/span&gt;    &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;h.0.mlp.c_proj.weight&quot;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3072&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;ln_f.bias&quot;&lt;/span&gt;              &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;ln_f.weight&quot;&lt;/span&gt;            &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;768&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each of these is a tensor with a shape and a dtype that hold the learned parameters for that component, these are the result of the model training process.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;LayerNormParams&lt;/code&gt; dataclass represents the layer normalization parameters, which include a scale (&lt;code&gt;g&lt;/code&gt;) and bias (&lt;code&gt;b&lt;/code&gt;) for each layer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LayerNormParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Gamma (scale)&lt;/span&gt;
    b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Beta (bias)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;LinearParams&lt;/code&gt; dataclass represents the linear projection parameters, which include a weight matrix (&lt;code&gt;w&lt;/code&gt;) and a bias vector (&lt;code&gt;b&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LinearParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Weight matrix&lt;/span&gt;
    b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Bias vector&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;MLPParams&lt;/code&gt; dataclass represents the feed-forward network parameters, which include two linear layers: a first linear layer (&lt;code&gt;c_fc&lt;/code&gt;) and a second linear layer (&lt;code&gt;c_proj&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MLPParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    c_fc&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams  &lt;span class=&quot;token comment&quot;&gt;# First linear layer&lt;/span&gt;
    c_proj&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams  &lt;span class=&quot;token comment&quot;&gt;# Second linear layer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;AttentionParams&lt;/code&gt; dataclass represents the attention parameters, which include a query-key-value projection (&lt;code&gt;c_attn&lt;/code&gt;) and an output projection (&lt;code&gt;c_proj&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AttentionParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    c_attn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams  &lt;span class=&quot;token comment&quot;&gt;# QKV projection&lt;/span&gt;
    c_proj&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LinearParams  &lt;span class=&quot;token comment&quot;&gt;# Output projection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;TransformerBlockParams&lt;/code&gt; dataclass represents a single transformer block, which includes a layer normalization (&lt;code&gt;ln_1&lt;/code&gt;), a feed-forward network (&lt;code&gt;mlp&lt;/code&gt;), an attention mechanism (&lt;code&gt;attn&lt;/code&gt;), and a second layer normalization (&lt;code&gt;ln_2&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TransformerBlockParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    ln_1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LayerNormParams  &lt;span class=&quot;token comment&quot;&gt;# First layer norm&lt;/span&gt;
    ln_2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LayerNormParams  &lt;span class=&quot;token comment&quot;&gt;# Second layer norm&lt;/span&gt;
    mlp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MLPParams  &lt;span class=&quot;token comment&quot;&gt;# MLP block&lt;/span&gt;
    attn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; AttentionParams  &lt;span class=&quot;token comment&quot;&gt;# Attention block&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;ModelParams&lt;/code&gt; dataclass represents the complete model parameters, which include a token embedding matrix (&lt;code&gt;wte&lt;/code&gt;), a position embedding matrix (&lt;code&gt;wpe&lt;/code&gt;), a list of transformer blocks (&lt;code&gt;blocks&lt;/code&gt;), and a final layer normalization (&lt;code&gt;ln_f&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ModelParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    wte&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Token embeddings&lt;/span&gt;
    wpe&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray  &lt;span class=&quot;token comment&quot;&gt;# Position embeddings&lt;/span&gt;
    blocks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;TransformerBlockParams&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Transformer blocks&lt;/span&gt;
    ln_f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; LayerNormParams  &lt;span class=&quot;token comment&quot;&gt;# Final layer norm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;HParams&lt;/code&gt; dataclass represents the hyperparameters of the model, which include the number of transformer layers (&lt;code&gt;n_layer&lt;/code&gt;), the number of attention heads (&lt;code&gt;n_head&lt;/code&gt;), and the context length (&lt;code&gt;n_ctx&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HParams&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    n_layer&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Number of transformer layers&lt;/span&gt;
    n_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Number of attention heads&lt;/span&gt;
    n_ctx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Context length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can load the model parameters from a &lt;code&gt;model.safetensors&lt;/code&gt; file using the &lt;code&gt;safe_open&lt;/code&gt; function. This file can be downloaded from the &lt;a href=&quot;https://huggingface.co/openai-community/gpt2&quot;&gt;Hugging Face Model Hub&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;tensors &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; safe_open&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;weights_path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; framework&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;numpy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; key &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Build transformer blocks&lt;/span&gt;
blocks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;n_layer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    prefix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;h.&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
    block &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TransformerBlockParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        ln_1&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LayerNormParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.ln_1.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.ln_1.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        ln_2&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LayerNormParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.ln_2.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.ln_2.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        mlp&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;MLPParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            c_fc&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LinearParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.mlp.c_fc.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.mlp.c_fc.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            c_proj&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LinearParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.mlp.c_proj.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.mlp.c_proj.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        attn&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;AttentionParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            c_attn&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LinearParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.attn.c_attn.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.attn.c_attn.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            c_proj&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LinearParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                w&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.attn.c_proj.weight&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.attn.c_proj.bias&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    blocks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;block&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Build final model params&lt;/span&gt;
params &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ModelParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    wte&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;wte.weight&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    wpe&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;wpe.weight&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    blocks&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    ln_f&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;LayerNormParams&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ln_f.weight&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tensors&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ln_f.bias&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;greedy-decoding&quot; tabindex=&quot;-1&quot;&gt;Greedy Decoding&lt;/h2&gt;
&lt;p&gt;With the model architecture defined and parameters loaded, we&#39;re now ready to put everything together and generate text. The simplest approach for text generation is greedy decoding, where at each step we simply choose the token with the highest probability as predicted by our model.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;generate&lt;/code&gt; function below encapsulates the entire text generation process, from encoding the initial prompt to iteratively generating new tokens. This function showcases how all the components we&#39;ve explored—tokenization, embedding, transformer blocks, and vocabulary projection—work together to create a functioning language model.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_tokens_to_generate&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    encoder &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_encoder&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    params&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hparams &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_gpt2_weights&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    input_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; encoder&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    generated_token_ids&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; flush&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    current_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_ids&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_tokens_to_generate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; gpt2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_ids&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;hparams&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;n_head&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        next_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        next_id_int &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        current_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_id_int&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        generated_token_ids&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;next_id_int&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        token_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; encoder&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;next_id_int&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;token_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; flush&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; encoder&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;generated_token_ids&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The generation process starts by encoding the user&#39;s prompt into token IDs using our BPE tokenizer. We initialize an empty list to keep track of the tokens we&#39;ll generate. Then we enter a loop where, at each iteration, we:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Feed the current sequence of token IDs (the original prompt plus all tokens generated so far) through our GPT-2 model&lt;/li&gt;
&lt;li&gt;Extract the logits for the last token position, which represent the model&#39;s prediction for what comes next&lt;/li&gt;
&lt;li&gt;Take the argmax of these logits to select the most likely next token&lt;/li&gt;
&lt;li&gt;Append this new token to our list of generated tokens and to the current sequence&lt;/li&gt;
&lt;li&gt;Decode and print the new token to show the generation in real-time&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After generating the requested number of tokens, we decode the full sequence of generated tokens back into text and return it. And that&#39;s it that&#39;s all it take to impmlement a naive version of a GPT-2 model. There&#39;s only about six &amp;quot;kernel&amp;quot; operations that we need to implement (&lt;code&gt;softmax&lt;/code&gt;, &lt;code&gt;gelu&lt;/code&gt;, &lt;code&gt;layer_norm&lt;/code&gt;, &lt;code&gt;linear&lt;/code&gt;, &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;matmul&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;mean&lt;/code&gt;) and the rest is just a lot of bookkeeping to handle the data structures.&lt;/p&gt;
&lt;p&gt;But as you notice, this model is &lt;em&gt;very&lt;/em&gt; slow and inefficient averaging about 2-3 tokens per second on a modern CPU. There are many opportunities for both fusion and parallelization via lowering into MLIR and PTX, which we&#39;ll explore in the next section.&lt;/p&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2205.14135&quot;&gt;FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2305.14314&quot;&gt;QLoRA: Efficient Finetuning of Quantized LLMs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://jalammar.github.io/illustrated-gpt2/&quot;&gt;The Illustrated GPT-2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://nlp.seas.harvard.edu/annotated-transformer/&quot;&gt;The Annotated Transformer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/pytorch-labs/attention-gym&quot;&gt;attention-gym&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Fri, 11 Apr 2025 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_transformers/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_transformers/</guid>
    </item>
    
    <item>
      <title>MLIR Part 6 - Specializing Python with E-graphs</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/egglog.jpeg&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;specializing-python-with-e-graphs&quot; tabindex=&quot;-1&quot;&gt;Specializing Python with E-graphs&lt;/h1&gt;
&lt;p&gt;We&#39;ve explored progressively more sophisticated techniques for optimizing numerical computations. We started with basic MLIR concepts, moved through memory management and linear algebra, and then neural network implementations. Each layer has added new capabilities for expressing and optimizing computations. Now we&#39;re reading to build our first toy compiler for Python expressions.&lt;/p&gt;
&lt;p&gt;In this section, we&#39;ll explore how to use the egglog library to perform term rewriting and optimization on Python expressions and compile them into MLIR.&lt;/p&gt;
&lt;p&gt;The entire source code for this section is &lt;a href=&quot;https://github.com/sdiehl/mlir-egglog&quot;&gt;available on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;equality-saturaiton-and-e-graphs&quot; tabindex=&quot;-1&quot;&gt;Equality Saturaiton and E-Graphs&lt;/h2&gt;
&lt;p&gt;Before we dive into the implementation, let&#39;s review the key concepts of equality saturation and e-graphs.&lt;/p&gt;
&lt;p&gt;Take as an example if we have the rewrites.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;x * 2&lt;/code&gt; → &lt;code&gt;x &amp;lt;&amp;lt; 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x*y/x&lt;/code&gt; → &lt;code&gt;y&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And we try to apply it over the expression &lt;code&gt;(a * 2)/2&lt;/code&gt; becomes &lt;code&gt;(a &amp;lt;&amp;lt; 1)/2&lt;/code&gt;. However we should have cancelled the &lt;code&gt;2&lt;/code&gt; in the numerator and denominator and got &lt;code&gt;a&lt;/code&gt; which results in a simpler expression. The order of rewrites is important and we want to find an optimal order of rewrites that reduces the expression to a form according to a cost function. This is called the &lt;em&gt;phase ordering problem&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;egg&lt;/code&gt; library employs an approach that involves exhaustively applying all possible rewrites to an expression, effectively addressing the phase ordering problem through the use of an e-graph. This approach allows for the exploration of all possible rewrites, followed by the extraction of the most optimal form of the expression.&lt;/p&gt;
&lt;p&gt;In linear algebra for example, matrix operations with NumPy like transpose, multiplication, are quite expensive because they involve touching every element of the matrix. But there is a wide &lt;a href=&quot;https://dustinstansbury.github.io/theclevermachine/linear-algebra-identities&quot;&gt;range of identities&lt;/a&gt; that can be applied to reduce the number of operations.&lt;/p&gt;
&lt;p&gt;Compilers like LLVM and even the &lt;code&gt;linalg&lt;/code&gt; dialect of MLIR doesn&#39;t know about these identities and so can&#39;t necessarily abstract away the expensive operations by applying rewrites. However at a high-level (our core language) we can use e-graph to produce much more efficient tensor manipulation operations before lowering them into MLIR.&lt;/p&gt;
&lt;p&gt;For example, the following identities are quite common in linear algebra:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(A B)^T = B^T A^T&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(A^T)^T = A&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Or in Python:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; A
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By applying these rules, we can optimize NumPy expressions at compile time, leading to significant performance improvements. For instance, in our example, we&#39;ve successfully reduced three loops—comprising one multiplication and two transposes—down to just two loops, which consist of one multiplication and one transpose. This optimization not only simplifies the computation but also enhances efficiency. In common uses of NumPy, there are numerous opportunities for such optimizations, often referred to as low-hanging fruit. These optimizations can be systematically applied to reduce the number of operations required, thereby streamlining the execution of numerical computations. This is particularly beneficial before even LLVM&#39;s auto-vectorization comes into play, as it allows us to leverage the full potential of our expressions and achieve faster execution times.&lt;/p&gt;
&lt;p&gt;An e-graph (equality graph) is a data structure that compactly represents many equivalent expressions. Instead of maintaining a single canonical form for expressions, e-graphs maintain classes of equivalent expressions. This approach allows for more flexible and efficient term rewriting.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at a concrete example using egglog library to do basic simplification. First we have to define our expression model.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; __future__ &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; annotations
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i64Like&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    
    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@classmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cls&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; StringLike&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__add__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__mul__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create an e-graph to store our expressions&lt;/span&gt;
egraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EGraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define our expressions and give them names in the e-graph&lt;/span&gt;
expr1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;let&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;expr1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Num&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 2 * (x + 3)&lt;/span&gt;
expr2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;let&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;expr2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# 6 + 2x&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define our rewrite rules using a decorated function&lt;/span&gt;
&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;register&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_num_rule&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;                    &lt;span class=&quot;token comment&quot;&gt;# Commutativity of addition&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Distributive property&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Constant folding for addition&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Num&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Constant folding for multiplication&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Apply rules until no new equalities are found&lt;/span&gt;
egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Check if expr1 and expr2 are equivalent&lt;/span&gt;
egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;check&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eq&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Extract the simplified form of expr1&lt;/span&gt;
egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;extract&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the &lt;code&gt;egraph.display()&lt;/code&gt; function we can visualize the e-graph.&lt;/p&gt;
&lt;p&gt;The input expression before equality saturation:&lt;/p&gt;
&lt;center style=&quot;margin: 20px&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/egg_1.png&quot; style=&quot;max-width: 400px&quot; alt=&quot;egg_1&quot; /&gt;
&lt;em&gt;Input expression before equality saturation&lt;/em&gt;
&lt;/center&gt;
&lt;p&gt;Then the output with all the equivalences classes is a network of expressions:&lt;/p&gt;
&lt;center style=&quot;margin: 20px&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/egg_2.png&quot; style=&quot;max-width: 400px&quot; alt=&quot;egg_2&quot; /&gt;
&lt;em&gt;Output expression after equality saturation&lt;/em&gt;
&lt;/center&gt;
&lt;p&gt;From there we can extract the expression we want according to a custom cost function.&lt;/p&gt;
&lt;h2 id=&quot;foundation-layer&quot; tabindex=&quot;-1&quot;&gt;Foundation Layer&lt;/h2&gt;
&lt;p&gt;Ok now let&#39;s apply this to our basic expression compiler. Our compiler pipeline has several key stages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Python function decoration and type annotation&lt;/li&gt;
&lt;li&gt;Expression tree extraction&lt;/li&gt;
&lt;li&gt;Term rewriting and optimization using e-graphs&lt;/li&gt;
&lt;li&gt;MLIR code generation&lt;/li&gt;
&lt;li&gt;LLVM compilation and JIT execution&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The foundation layer of our compiler provides the core abstractions for representing and manipulating mathematical expressions. This layer is crucial as it forms the basis for all higher-level optimizations and transformations. Let&#39;s explore each component in detail.&lt;/p&gt;
&lt;h3 id=&quot;expression-model-(expr_model.py)&quot; tabindex=&quot;-1&quot;&gt;Expression Model (&lt;code&gt;expr_model.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;At the heart of our compiler is an expression model that represents mathematical expressions as an abstract syntax tree (AST). The model is implemented using Python&#39;s dataclasses for clean and efficient representation.&lt;/p&gt;
&lt;h4 id=&quot;core-expression-types&quot; tabindex=&quot;-1&quot;&gt;Core Expression Types&lt;/h4&gt;
&lt;p&gt;The base class for all expressions is the &lt;code&gt;Expr&lt;/code&gt; class, which provides the fundamental operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__add__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; as_expr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__mul__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; as_expr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... other operations&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expression model consists of three fundamental types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Literals&lt;/strong&gt;: Constants in our expressions&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FloatLiteral&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    fval&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Floating-point constant&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IntLiteral&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    ival&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Integer constant&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Symbols&lt;/strong&gt;: Variables and function names&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    name&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Variable or function name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Operations&lt;/strong&gt;: Both unary and binary operations&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UnaryOp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    operand&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr  &lt;span class=&quot;token comment&quot;&gt;# Single operand&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BinaryOp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    lhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr  &lt;span class=&quot;token comment&quot;&gt;# Left-hand side&lt;/span&gt;
    rhs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr  &lt;span class=&quot;token comment&quot;&gt;# Right-hand side&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BinaryOp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;pass&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Addition&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frozen&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Sin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;UnaryOp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;pass&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Sine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;built-in-functions-(builtin_functions.py)&quot; tabindex=&quot;-1&quot;&gt;Built-in Functions (&lt;code&gt;builtin_functions.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The built-in functions module provides a NumPy-like interface for mathematical operations. This makes it easier for users to write expressions in a familiar syntax while still leveraging our optimization framework. It includes common mathematical constants and helper functions for operations like absolute value.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# A mock NumPy namespace that we convert into our own expression model&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; math
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expr_model &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    cos&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tan&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    asin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    acos&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    atan&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tanh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    sinh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    cosh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    sqrt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    exp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    log&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    log10&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    log2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    float32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    int64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    maximum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# noq&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Constants&lt;/span&gt;
e &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;e
pi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi


&lt;span class=&quot;token comment&quot;&gt;# Define abs function&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; maximum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;relu&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; maximum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


__all__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;sin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;cos&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;tan&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;asin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;acos&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;atan&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;tanh&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;sinh&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;cosh&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;sqrt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;exp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;log&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;log10&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;log2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;float32&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;int64&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;e&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;pi&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;maximum&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;abs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;term-ir-(term_ir.py)&quot; tabindex=&quot;-1&quot;&gt;Term IR (&lt;code&gt;term_ir.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The Term IR layer provides an intermediate representation optimized for term rewriting and equality saturation. A key feature of the Term IR is a cost model for different operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;COST_BASIC_ARITH &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Basic arithmetic (single CPU instruction)&lt;/span&gt;
COST_CAST &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# Type conversion operations&lt;/span&gt;
COST_DIV &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Division&lt;/span&gt;
COST_POW_INTEGER &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Integer power&lt;/span&gt;
COST_SQRT &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;# Square root&lt;/span&gt;
COST_LOG &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# Logarithm&lt;/span&gt;
COST_EXP &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# Exponential&lt;/span&gt;
COST_POW &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# General power operation&lt;/span&gt;
COST_TRIG_BASIC &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;75&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Basic trigonometric functions&lt;/span&gt;
COST_HYPERBOLIC &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;180&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Hyperbolic functions&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These costs are used by the e-graph optimization engine to make decisions about which transformations to apply. The costs roughly correspond to the computational complexity of each operation on modern hardware.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; __future__ &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; annotations

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; egglog
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; StringLike&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i64Like&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f64Like  &lt;span class=&quot;token comment&quot;&gt;# noqa: F401&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrite
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Generator
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expr_model &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; FloatLiteral&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; IntLiteral
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; abc &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; abstractmethod

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;as_egraph&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Convert a syntax tree expression to an egraph term.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; expr_model

    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Literals and Symbols&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; FloatLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fval&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_f32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; IntLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ival&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Binary Operations&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; expr_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Rest of the operations&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The cost model is used to guide the e-graph optimization engine to find the most cost-effective implementation according to our cost model. For example&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
e^x &#92;cdot e^x &#92;cdot e^x = e^{3x}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The LHS has 3 multiplications and the RHS has 1 multiplication. So the cost applied to the extraction will select the RHS.&lt;/p&gt;
&lt;h2 id=&quot;transformation-layer&quot; tabindex=&quot;-1&quot;&gt;Transformation Layer&lt;/h2&gt;
&lt;p&gt;One of the most powerful features of our compiler is its ability to symbolically interpret Python functions. This process transforms regular Python functions into our IR representation, allowing us to apply optimizations on the resulting expression tree.&lt;/p&gt;
&lt;p&gt;The interpretation process is handled by the &lt;code&gt;interpret&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; types
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; inspect

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; expr_model &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ir


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;interpret&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; types&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Symbolically interpret a python function.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Get the function&#39;s signature&lt;/span&gt;
    sig &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; inspect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;signature&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create symbolic parameters for each of the function&#39;s arguments&lt;/span&gt;
    params &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; sig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    symbolic_params &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Bind the symbolic parameters to the function&#39;s arguments&lt;/span&gt;
    ba &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bind&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;symbolic_params&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Inject our globals (i.e. np) into the function&#39;s globals&lt;/span&gt;
    custom_globals &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__globals__&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;copy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    custom_globals&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;update&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create a temporary function with our custom globals&lt;/span&gt;
    tfn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; types&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionType&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__code__&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        custom_globals&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__name__&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__defaults__&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__closure__&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; tfn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;ba&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;ba&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function begins with parameter analysis, where it analyzes the input function&#39;s signature to determine its parameters. For each parameter, it creates a symbolic representation using our &lt;code&gt;Symbol&lt;/code&gt; class. These symbols will be used to track variables through the expression tree.&lt;/p&gt;
&lt;p&gt;Next, the symbolic parameters are bound to the function&#39;s argument slots, creating a mapping between parameter names and their symbolic representations. The function then injects our custom implementations of mathematical operations, such as NumPy functions, into the function&#39;s global namespace. This allows us to intercept calls to these functions and replace them with our symbolic operations.&lt;/p&gt;
&lt;p&gt;A temporary function is created with the modified globals, while retaining the same code, name, and closure as the original function. Finally, the function is executed with the symbolic parameters, resulting in an expression tree that represents the computation.&lt;/p&gt;
&lt;p&gt;For example, given a Python function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interpretation process will:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create symbols for &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;np.sin&lt;/code&gt; and &lt;code&gt;np.cos&lt;/code&gt; with our symbolic versions&lt;/li&gt;
&lt;li&gt;Execute the function with symbolic inputs&lt;/li&gt;
&lt;li&gt;Return an expression tree representing &lt;code&gt;Sin(Symbol(&amp;quot;x&amp;quot;)) + Cos(Symbol(&amp;quot;y&amp;quot;))&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This symbolic interpretation is what allows us to capture Python computations in a form that can be optimized using our e-graph machinery.&lt;/p&gt;
&lt;h3 id=&quot;ir-conversion-(ir_to_mlir.py)&quot; tabindex=&quot;-1&quot;&gt;IR Conversion (&lt;code&gt;ir_to_mlir.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The IR to MLIR conversion layer serves as a crucial bridge between our high-level expression representation and MLIR&#39;s lower-level dialect. This conversion process is implemented in &lt;code&gt;ir_to_mlir.py&lt;/code&gt; and involves several steps that leverage Python&#39;s dynamic execution capabilities along with AST manipulation.&lt;/p&gt;
&lt;p&gt;The conversion pipeline begins with the &lt;code&gt;convert_term_to_expr&lt;/code&gt; function, which transforms an IR term into our internal expression model. This function employs Python&#39;s built-in &lt;code&gt;ast&lt;/code&gt; module to parse and manipulate the abstract syntax tree of the term. The process is particularly interesting because it uses Python&#39;s execution environment as part of the conversion process.&lt;/p&gt;
&lt;p&gt;When a term arrives for conversion, it first goes through AST parsing. The function creates a Python AST from the string representation of the term, which allows us to manipulate the code structure before execution. A key part of this process is the &lt;code&gt;mangle_assignment&lt;/code&gt; function, which ensures the result of the expression is properly captured in a variable named &lt;code&gt;_out&lt;/code&gt;. This mangling step is crucial because it provides a way to extract the final result from the execution environment.&lt;/p&gt;
&lt;p&gt;The execution environment is carefully constructed using a &lt;code&gt;function_map&lt;/code&gt; dictionary that maps operation names to their corresponding implementations. This map includes basic arithmetic operations (Add, Sub, Mul, Div), mathematical functions (Sin, Cos, Exp, Log), and type conversion operations (CastF32, CastI64). Each of these operations is mapped to either a method from our expression model or a function from our builtin functions module.&lt;/p&gt;
&lt;p&gt;The second major component is the &lt;code&gt;convert_term_to_mlir&lt;/code&gt; function, which takes the converted expression and generates MLIR code. This function handles the final transformation into MLIR&#39;s textual format. It processes function arguments through the &lt;code&gt;argspec&lt;/code&gt; parameter, creating a mapping between argument names and their MLIR representations (e.g., converting &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;%arg_x&lt;/code&gt;). The actual MLIR generation is delegated to the &lt;code&gt;MLIRGen&lt;/code&gt; class, which walks through the expression tree and produces corresponding MLIR operations.&lt;/p&gt;
&lt;p&gt;For example, when converting a simple arithmetic expression like &lt;code&gt;a + b * c&lt;/code&gt;, the pipeline would:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Parse the expression into an AST&lt;/li&gt;
&lt;li&gt;Transform it into our internal expression model using the function map&lt;/li&gt;
&lt;li&gt;Generate MLIR code with proper memory references and operations&lt;/li&gt;
&lt;li&gt;Wrap the operations in a proper MLIR function structure with appropriate type annotations&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;convert_term_to_expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tree&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; IRTerm&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Convert a term to an expression.
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Parse the term into an AST&lt;/span&gt;
    astree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ast&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tree&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Mangle the assignment&lt;/span&gt;
    astree&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ast&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fix_missing_locations&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mangle_assignment&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;astree&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;body&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Execute the AST&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Any&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;astree&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;string&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;exec&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; function_map&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get the result&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;globals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;_out&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;convert_term_to_mlir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tree&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; IRTerm&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argspec&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Convert a term to MLIR.
    &quot;&quot;&quot;&lt;/span&gt;

    expr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; convert_term_to_expr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tree&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    argnames &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argspec&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    argmap &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;%arg_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; argnames&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    source &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MLIRGen&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argmap&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; source
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;optimization-layer&quot; tabindex=&quot;-1&quot;&gt;Optimization Layer&lt;/h2&gt;
&lt;p&gt;Now we can start to write our own rewrite rules to apply over our expression tree.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;birewrite_subsume&lt;/code&gt; helper function is a generator that yields rewrite rules for the e-graph. It takes two terms and yields a rewrite rule that converts the first term to the second, making the first term unable to be matched against or extracted. We use this to unidirectionally convert generic Terms into specialized binary and unary operations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;birewrite_subsume&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Generator&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; subsume&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The basic simplification module implements fundamental mathematical rewrites that form the foundation of our term rewriting system. These rules are organized in the &lt;code&gt;basic_math&lt;/code&gt; ruleset and include several key categories of transformations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Term Translation Rules&lt;/strong&gt;: These rules allow conversion between generic Terms and their specialized forms (Add, Mul, Div, Pow)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Identity Rules&lt;/strong&gt;: Rules for handling mathematical identities like $x + 0 = x$ and $x &#92;cdot 1 = x$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Associativity Rules&lt;/strong&gt;: Rules that handle the associative properties of operations like $(x + y) + z = x + (y + z)$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Power Rules&lt;/strong&gt;: Special handling for powers, including cases like $x^0 = 1$ and $x^1 = x$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Arithmetic Simplification&lt;/strong&gt;: Rules that simplify common arithmetic patterns like $x + x = 2 &#92;cdot x$&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Each rule is implemented using egglog&#39;s rewrite system.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;term_ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mul&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Div&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pow&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PowConst&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; birewrite_subsume
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f64
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Generator


&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@ruleset&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;basic_math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f64
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Generator&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Allow us to translate Term into their specializations&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; birewrite_subsume&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; birewrite_subsume&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; birewrite_subsume&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Div&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; birewrite_subsume&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x + 0 = x (integer case)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# x + 0.0 = x (float case)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_f32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# 0.0 + x = x (float case)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_f32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x * 1 = x&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x * 0 = 0&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# (x + y) + z = x + (y + z)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# (x * y) * z = x * (y * z)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x + x = 2 * x&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x * x = x^2&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# (x^y) * (x^z) = x^(y + z)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x^i = x * x^(i - 1)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;PowConst&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x^0 = 1&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;PowConst&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_f32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x^1 = x&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;PowConst&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# x^i = x * x^(i - 1)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;PowConst&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PowConst&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similar to the basic simplification module, the trigonometric simplification module provides a comprehensive set of rules for simplifying expressions involving trigonometric and hyperbolic functions. The &lt;code&gt;trig_simplify&lt;/code&gt; ruleset implements several important categories of transformations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fundamental Identities&lt;/strong&gt;: Core trigonometric identities like $ &#92;sin^2(x) + &#92;cos^2(x) = 1 $&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Double Angle Formulas&lt;/strong&gt;: Rules for simplifying expressions like $ &#92;sin(x + y) $ and $ &#92;cos(x + y) $&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hyperbolic Identities&lt;/strong&gt;: Similar rules for hyperbolic functions, including identities for $ &#92;sinh $, $ &#92;cosh $, and $ &#92;tanh $&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These rules are particularly important for optimizing numerical computations involving trigonometric functions, which are common in scientific computing and machine learning applications. The module carefully balances the tradeoff between expression simplification and computational efficiency, using the cost model defined in the Term IR to guide its decisions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;term_ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Sinh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Cosh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Tanh&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pow&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Add
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f64
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; rewrite


&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@ruleset&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;trig_simplify&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i64&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fval&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f64&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Fundamental trig identities&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# sin²(x) + cos²(x) = 1&lt;/span&gt;
    two &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; two&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; two&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_f32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Double angle formulas&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Hyperbolic identities&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; rewrite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lit_i64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;egglog-optimizer-(egglog_optimizer.py)&quot; tabindex=&quot;-1&quot;&gt;Egglog Optimizer (&lt;code&gt;egglog_optimizer.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The optimization engine ties together all the rewrite rules and provides the main interface for applying optimizations to Python functions. It consists of several key components:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Rule Composition&lt;/strong&gt;: The ability to combine multiple rulesets either sequentially or in parallel&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expression Extraction&lt;/strong&gt;: Logic for converting between the Python AST and the term representation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization Pipeline&lt;/strong&gt;: A structured approach to applying rules until reaching a fixed point&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MLIR Generation&lt;/strong&gt;: Final conversion of the optimized expression to MLIR code&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The optimizer uses the e-graph data structure to efficiently explore equivalent expressions and find the most cost-effective implementation according to our cost model.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; inspect
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; types &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FunctionType

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; EGraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Ruleset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;egraph &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; UnstableCombinedRuleset

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;term_ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; as_egraph
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;python_to_ir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; interpret
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; builtin_functions &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ns
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expr_model &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Expr
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ir_to_mlir &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; convert_term_to_mlir

&lt;span class=&quot;token comment&quot;&gt;# Rewrite rules&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;basic_simplify &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; basic_math
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;trig_simplify &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; trig_simplify

OPTS&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Ruleset &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;basic_math&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; trig_simplify&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ast&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    root &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; as_egraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ast&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    egraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EGraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;let&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;root&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# The user can compose rules as (rule1 | rule2) to apply them in parallel&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# or (rule1, rule2) to apply them sequentially&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; opt &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; UnstableCombinedRuleset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# For individual rules, create a temporary ruleset&lt;/span&gt;
            temp_ruleset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;temp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            temp_ruleset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp_ruleset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    extracted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;extract&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# if debug:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;#     egraph.display()&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; extracted


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; OPTS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Convert np functions accordinging to the namespace map&lt;/span&gt;
    exprtree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; interpret&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;np&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ns&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    extracted &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;exprtree&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrites&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; debug&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get the argument spec&lt;/span&gt;
    argspec &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; inspect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;signature&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    params &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argspec&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; convert_term_to_mlir&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;extracted&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These modules work together to provide a powerful system for optimizing mathematical expressions, particularly those involving trigonometric and transcendental functions. The system is extensible, allowing new rules to be added easily, and provides a solid foundation for building more specialized optimizations on top.&lt;/p&gt;
&lt;p&gt;The egglog optimizer supports two ways to compose rewrite rules: parallel and sequential composition. When rules are combined using the | operator (parallel composition), they are applied simultaneously in the same iteration of the e-graph saturation process. This allows multiple transformations to be explored concurrently. In contrast, when rules are combined using a tuple or sequence (sequential composition), they are applied one after another, with each rule set running to saturation before moving to the next. This sequential approach can be useful when certain transformations should only be attempted after others have completed.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Example 1: Parallel Composition&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Both rulesets are applied simultaneously in each iteration&lt;/span&gt;
parallel_rules &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; simplify_adds &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; simplify_muls
egraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EGraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;parallel_rules&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Example 2: Sequential Composition&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# simplify_adds runs to completion before simplify_muls starts&lt;/span&gt;
sequential_rules &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;simplify_adds&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; simplify_muls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
egraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EGraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ruleset &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; sequential_rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ruleset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;saturate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mlir-generation-(mlir_gen.py)&quot; tabindex=&quot;-1&quot;&gt;MLIR Generation (&lt;code&gt;mlir_gen.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The MLIR code generator is responsible for transforming our optimized expression trees into executable MLIR code. The generator follows a systematic approach to produce vectorized kernels that can efficiently process N-dimensional arrays. Let&#39;s examine the key components and design principles:&lt;/p&gt;
&lt;p&gt;The generator produces a function that follows this template:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel_worker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Kernel body&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The generated kernel accepts two memref arguments - an input and output buffer - and processes them element-wise using affine loops. This design allows for efficient vectorized operations on arrays of any dimension.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel_worker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.emit_c_interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Get dimension of input array&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;dim &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Process each element in a flattened manner&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;dim &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// Kernel body&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;expression-translation&quot; tabindex=&quot;-1&quot;&gt;Expression Translation&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;MLIRGen&lt;/code&gt; class implements a multi-pass translation strategy that begins with subexpression expansion, where the generator unfolds the expression tree into a complete set of subexpressions using the &lt;code&gt;unfold&lt;/code&gt; method. This process ensures that common subexpressions are identified and can be reused. Next, the generator employs topological ordering, sorting subexpressions by complexity, using string length as a proxy to ensure that simpler expressions are evaluated before more complex ones that might depend on them. Finally, the code generation pipeline is executed, which first loads input variables from the memref, generates intermediate computations for subexpressions, and stores the final result back to the output memref.&lt;/p&gt;
&lt;p&gt;The generator employs a smart caching mechanism to avoid redundant computations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;walk&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; expr &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; as_source&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lookup&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; as_source&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lookup&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This caching strategy ensures that each subexpression is computed exactly once, common subexpressions are reused through MLIR&#39;s SSA (Static Single Assignment) form, and the generated code maintains optimal efficiency.&lt;/p&gt;
&lt;h4 id=&quot;mlir-dialect-usage&quot; tabindex=&quot;-1&quot;&gt;MLIR Dialect Usage&lt;/h4&gt;
&lt;p&gt;Our generator then walks over the expression tree and maps our high-level expressions to appropriate MLIR dialects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Arithmetic operations use the &lt;code&gt;arith&lt;/code&gt; dialect (e.g., &lt;code&gt;arith.addf&lt;/code&gt;, &lt;code&gt;arith.mulf&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Mathematical functions use the &lt;code&gt;math&lt;/code&gt; dialect (e.g., &lt;code&gt;math.sin&lt;/code&gt;, &lt;code&gt;math.exp&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Memory operations use the &lt;code&gt;memref&lt;/code&gt; dialect for array access&lt;/li&gt;
&lt;li&gt;Loop structures use the &lt;code&gt;affine&lt;/code&gt; dialect for optimized iteration&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, a Python expression like &lt;code&gt;sin(x) + cos(y)&lt;/code&gt; would be translated into:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sin &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;math.sin&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg_x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;cos &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;math.cos&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg_y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;cos &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The generator handles type conversions automatically, standardizing floating-point operations to &lt;code&gt;f32&lt;/code&gt; and using &lt;code&gt;i32&lt;/code&gt; or &lt;code&gt;i64&lt;/code&gt; for integer operations as appropriate. When needed, explicit type casts are generated, such as &lt;code&gt;arith.sitofp&lt;/code&gt; for converting integers to floating-point values. This type system ensures type safety while maintaining compatibility with MLIR&#39;s strong typing requirements.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; textwrap &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; indent
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Callable
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; expr_model &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ir

KERNEL_NAME &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;kernel_worker&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Numpy vectorized kernel that supports N-dimensional arrays&lt;/span&gt;
kernel_prologue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&quot;&quot;
func.func @&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;KERNEL_NAME&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;(
    %arg0: memref&amp;lt;?xf32&gt;,
    %arg1: memref&amp;lt;?xf32&gt;
) attributes  &#92;{&#92;{
    %c0 = arith.constant 0 : index
    
    // Get dimension of input array
    %dim = memref.dim %arg0, %c0 : memref&amp;lt;?xf32&gt;
    
    // Process each element in a flattened manner
    affine.for %idx = %c0 to %dim &#92;{&#92;{
&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;

kernel_epilogue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    }
    return
}
&quot;&quot;&quot;&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MLIRGen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Generate textual MLIR from a symbolic expression.
    &quot;&quot;&quot;&lt;/span&gt;

    root&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr
    cache&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    subexprs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# local variables&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; root&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; argmap&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Use the keys from argmap as the variable names&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;root &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; root
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;argmap&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subexprs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Generate MLIR code for the root expression.
        &quot;&quot;&quot;&lt;/span&gt;
        subexprs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unfold&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        subexprs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        buf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# First load input arguments from memref&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; var &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            buf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;%arg_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; = affine.load %arg0[%idx] : memref&amp;lt;?xf32&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; subex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subexprs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Skip if this is just a variable reference&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subex&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; subex&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;continue&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Recurse and cache the subexpression&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;walk&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            orig &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;subex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Generate a unique name for the subexpression&lt;/span&gt;
            k &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;%v&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;subex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subexprs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; orig

            &lt;span class=&quot;token comment&quot;&gt;# Append the subexpression to the buffer&lt;/span&gt;
            buf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;orig&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;walk&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        res &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Handle the output&lt;/span&gt;
        buf&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;affine.store &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, %arg1[%idx] : memref&amp;lt;?xf32&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Format the kernel body&lt;/span&gt;
        kernel_body &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; indent&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;buf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;    &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; kernel_prologue &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; kernel_body &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; kernel_epilogue

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unfold&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Unfold an expression into a set of subexpressions.
        &quot;&quot;&quot;&lt;/span&gt;
        visited &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        all_subexprs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        to_visit &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; to_visit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            current &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; to_visit&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            all_subexprs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; current &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; visited&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;continue&lt;/span&gt;
            visited&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            to_visit&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;extend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;get_children&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; all_subexprs

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;walk&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Walk an expression recursively and generate MLIR code for subexpressions,
        caching the intermediate expressions in a lookup table.
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; expr &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;lookup&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; as_source&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lookup&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; as_source&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lookup&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_children&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Get child expressions for an AST node.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;BinaryOp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;UnaryOp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operand&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FloatLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IntLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; NotImplementedError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unsupported expression type: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;as_source&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Convert expressions to MLIR source code using arith and math dialects.
    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Literals and Symbols&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FloatLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fval&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.constant &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IntLiteral&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ival&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.constant &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : i32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; name &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;%arg_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;%&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Binary Operations&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.addf &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.mulf &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Div&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.divf &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Maximum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rhs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.maximumf &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rhs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Unary Math Operations&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Log&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sinh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Cosh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; op&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            op_name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__name__&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lower&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;math.&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;op_name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;operand&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Neg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;operand&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.negf &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32&quot;&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Type Casting&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CastF32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;operand&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.sitofp &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : i64 to f32&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CastI64&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;operand&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;arith.fptosi &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;lookup_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;op&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : f32 to i64&quot;&lt;/span&gt;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; NotImplementedError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unsupported expression type: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mlir-backend-(mlir_backend.py)&quot; tabindex=&quot;-1&quot;&gt;MLIR Backend (&lt;code&gt;mlir_backend.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The MLIR backend to our compiler is responsible for transforming our high-level MLIR code through various lowering stages until it reaches LLVM IR and finally executable code. Let&#39;s walk through the key components and design principles:&lt;/p&gt;
&lt;p&gt;The backend supports two primary compilation targets:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;enum&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Enum&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    OPENMP &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;openmp&quot;&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Parallel execution using OpenMP&lt;/span&gt;
    BASIC_LOOPS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;loops&quot;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Sequential execution with basic loops&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows the compiler to generate either parallel code using OpenMP for multi-threaded execution or simpler sequential code depending on the application&#39;s needs.&lt;/p&gt;
&lt;p&gt;The compilation process is organized into several distinct phases, each applying specific MLIR optimization passes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Common Initial Transformations&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;COMMON_INITIAL_OPTIONS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--debugify-level=locations&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--inline&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-affine-loop-normalize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-affine-parallelize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-affine-super-vectorize&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--affine-scalrep&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-lower-affine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-vector-to-scf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-linalg-to-loops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These passes handle function inlining, loop normalization, vectorization, and initial dialect conversions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Target-Specific Lowering&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OpenMP Path&lt;/strong&gt;: Converts structured control flow to OpenMP operations and then to LLVM&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;OPENMP_OPTIONS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-scf-to-openmp&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-openmp-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-vector-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-math-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... additional lowering passes&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Basic Loops Path&lt;/strong&gt;: Direct conversion to sequential LLVM IR&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;BASIC_LOOPS_OPTIONS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-scf-to-cf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-vector-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;-convert-math-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... additional lowering passes&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Final LLVM IR Generation&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;MLIR_TRANSLATE_OPTIONS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--mlir-print-local-scope&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--mlir-to-llvmir&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;--verify-diagnostics&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;code&gt;MLIRCompiler&lt;/code&gt; class orchestrates the entire compilation process through three main stages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MLIR to LLVM Dialect&lt;/strong&gt; (&lt;code&gt;to_llvm_dialect&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Converts high-level MLIR operations to the LLVM dialect&lt;/li&gt;
&lt;li&gt;Applies target-specific optimizations (OpenMP or basic loops)&lt;/li&gt;
&lt;li&gt;Handles memory layout and type conversions&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LLVM Dialect to LLVM IR&lt;/strong&gt; (&lt;code&gt;mlir_translate_to_llvm_ir&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Translates the LLVM dialect representation to textual LLVM IR&lt;/li&gt;
&lt;li&gt;Preserves debug information and verifies the generated code&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LLVM IR to Bitcode&lt;/strong&gt; (&lt;code&gt;llvm_ir_to_bitcode&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Converts textual LLVM IR to binary LLVM bitcode&lt;/li&gt;
&lt;li&gt;Prepares the code for final execution&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The backend uses temporary files for intermediate representations and provides debugging capabilities through the &lt;code&gt;debug&lt;/code&gt; flag:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_run_shell&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cmd&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; in_mode&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; out_mode&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; src&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        NamedTemporaryFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mode&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;w&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;in_mode&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; src_file&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        NamedTemporaryFile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mode&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;r&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;out_mode&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; out_file&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Execute compilation command and handle I/O&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the compiler spit out the LLVM IR and we can use the &lt;code&gt;llvmlite&lt;/code&gt; library to load it and execute it inside of the Python process, allowing us to dynamically load the compiled machine code.&lt;/p&gt;
&lt;h3 id=&quot;llvm-runtime-(llvm_runtime.py)&quot; tabindex=&quot;-1&quot;&gt;LLVM Runtime (&lt;code&gt;llvm_runtime.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The LLVM runtime provides the final layer of our compilation pipeline, handling the dynamic loading and execution of compiled LLVM code within Python. This component uses &lt;code&gt;llvmlite&lt;/code&gt; to interface with LLVM and manages the Just-In-Time (JIT) compilation process.&lt;/p&gt;
&lt;h4 id=&quot;llvm-initialization&quot; tabindex=&quot;-1&quot;&gt;LLVM Initialization&lt;/h4&gt;
&lt;p&gt;The runtime begins with a cached initialization of LLVM components:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@cache&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init_llvm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_all_targets&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_all_asmprinters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This initialization is cached to ensure it occurs only once per Python session, which sets up the core LLVM functionality, all available target architectures, and the assembly printers necessary for code generation.&lt;/p&gt;
&lt;p&gt;The runtime creates an LLVM execution engine that manages the JIT compilation process:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_execution_engine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    target &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_default_triple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    target_machine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_target_machine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    backing_mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_mcjit_compiler&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;backing_mod&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_machine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; engine
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This setup determines the host machine&#39;s target architecture, creates a target machine instance for code generation, initializes an empty LLVM module as a backing store, and creates an MCJIT compiler instance for optimized code execution.&lt;/p&gt;
&lt;p&gt;The runtime provides two levels of module compilation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Direct Module Compilation&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_mod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mod&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;verify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;                     &lt;span class=&quot;token comment&quot;&gt;# Verify module correctness&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_module&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Add to execution engine&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;finalize_object&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;# Finalize compilation&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_static_constructors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Initialize static data&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; mod
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;IR String Compilation&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_ir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Parse IR text&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; compile_mod&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Compile module&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This runtime layer bridges the gap between LLVM&#39;s low-level compilation infrastructure and Python&#39;s high-level execution environment, allowing our compiled kernels to run efficiently within the Python process.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;binding &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; llvm
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; functools &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cache


&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@cache&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init_llvm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__version__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_all_targets&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_all_asmprinters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_mod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mod&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;verify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_module&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;finalize_object&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_static_constructors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; mod


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_execution_engine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    target &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_default_triple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    target_machine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_target_machine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    backing_mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_mcjit_compiler&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;backing_mod&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_machine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; engine


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_ir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; compile_mod&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;jit-engine-(jit_engine.py)&quot; tabindex=&quot;-1&quot;&gt;JIT Engine (&lt;code&gt;jit_engine.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;jit_engine.py&lt;/code&gt; module is the orchestrator of our compiler pipeline, tying together all the components into a seamless compilation process. It manages the entire lifecycle from Python function to executable machine code, handling optimization, code generation, and runtime execution.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;JITEngine&lt;/code&gt; class provides three main levels of compilation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Frontend Compilation&lt;/strong&gt; (&lt;code&gt;run_frontend&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_frontend&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;run_frontend&lt;/code&gt; method takes a Python function along with optional rewrite rules, applies the egglog optimizer to perform term rewriting, and generates optimized MLIR code.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Backend Compilation&lt;/strong&gt; (&lt;code&gt;run_backend&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_backend&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mlir_src&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mlir_compiler &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MLIRCompiler&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    mlir_omp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mlir_compiler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to_llvm_dialect&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_src&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mlir_compiler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlir_translate_to_llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_omp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;run_backend&lt;/code&gt; method converts MLIR to the LLVM dialect, translates it to LLVM IR, handles platform-specific optimizations, and ultimately returns the address of the compiled function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Full JIT Compilation&lt;/strong&gt; (&lt;code&gt;jit_compile&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;jit_compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mlir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_frontend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrites&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    address &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_backend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; address
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;jit_compile&lt;/code&gt; method combines both frontend and backend compilation, providing a single entry point for the entire compilation process.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In order to use the OpenMP rutnime we need to load the system-specific OpenMP library into the Python process. Which we can do through the &lt;code&gt;ctypes&lt;/code&gt; library once we know the corect path of the shared library.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_omp_path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;startswith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;linux&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;find_library&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;libgomp.so&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;startswith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;darwin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;find_library&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;iomp5&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unsupported platform: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; omppath
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The engine handles several critical LLVM-related tasks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Initialization&lt;/strong&gt;:&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    init_llvm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; find_omp_path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CDLL&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;omppath&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mode&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RTLD_NOW&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ee &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; create_execution_engine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Initializes LLVM infrastructure&lt;/li&gt;
&lt;li&gt;Loads OpenMP runtime&lt;/li&gt;
&lt;li&gt;Creates the execution engine&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The class serves as the glue that binds our compiler&#39;s components together, providing an interface between Python code and optimized machine code execution. It handles the complexity of multi-stage compilation, platform-specific requirements, and runtime optimization.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; __future__ &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; annotations

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; os
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; sys
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; types &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FunctionType
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Ruleset

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;binding &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; llvm

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;llvm_runtime &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    create_execution_engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    init_llvm&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    compile_mod&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlir_gen &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; KERNEL_NAME
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlir_backend &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; MLIRCompiler&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Target
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;egglog_optimizer &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; OPTS


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_omp_path&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;startswith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;linux&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;find_library&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;libgomp.so&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;elif&lt;/span&gt; sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;startswith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;darwin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;find_library&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;iomp5&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Unsupported platform: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;sys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;platform&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; omppath


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;JITEngine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        init_llvm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        omppath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; find_omp_path&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CDLL&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;omppath&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mode&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RTLD_NOW&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ee &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; create_execution_engine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_frontend&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        actual_rewrites &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; rewrites &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; rewrites &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; OPTS
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrites&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;actual_rewrites&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_backend&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mlir_src&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        mlir_compiler &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MLIRCompiler&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        mlir_omp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mlir_compiler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to_llvm_dialect&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_src&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;BASIC_LOOPS&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mlir_compiler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlir_translate_to_llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_omp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Parsing LLVM assembly.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Clean up the LLVM IR by ensuring proper line endings and formatting&lt;/span&gt;
            llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Clean up problematic attribute strings (hack for divergence in modern LLVM IR syntax with old llvmlite)&lt;/span&gt;
            llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;replace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;captures(none)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;replace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;memory(argmem: readwrite)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;replace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;memory(none)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            llvm_ir &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;n&quot;&lt;/span&gt;

            mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; compile_mod&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ee&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Resolve the function address&lt;/span&gt;
            func_name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;_mlir_ciface_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;KERNEL_NAME&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
            address &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ee&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_function_address&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;func_name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; address&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Function must be compiled successfully.&quot;&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; address
        &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; Exception &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error during LLVM IR parsing/compilation: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;LLVM IR that failed to parse:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;jit_compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        fn&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; FunctionType&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Ruleset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        mlir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_frontend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrites&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        address &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run_backend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; address
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;dispatcher-(dispatcher.py)&quot; tabindex=&quot;-1&quot;&gt;Dispatcher (&lt;code&gt;dispatcher.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The Dispatcher serves as the user-facing interface of our compiler, providing a decorator to transform regular Python functions into optimized vectorized kernels. It handles the compilation process and manages the execution of compiled functions.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Dispatcher&lt;/code&gt; class manages the lifecycle of compiled functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dispatcher&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    _compiled_func&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bytes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Compiled function address&lt;/span&gt;
    _compiler&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; JITEngine &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# JIT compilation engine&lt;/span&gt;
    py_func&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; types&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FunctionType       &lt;span class=&quot;token comment&quot;&gt;# Original Python function&lt;/span&gt;
    rewrites&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;RewriteOrRule&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Optimization rules&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compilation process is handled through a simple interface by invoking the compiler&#39;s &lt;code&gt;jit_compile&lt;/code&gt; method.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compiler &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; JITEngine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    binary &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compiler&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;jit_compile&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;py_func&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rewrites&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compiled_func &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; binary
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; binary
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This method creates a new JIT engine instance, compiles the Python function with the specified rewrites, and stores the compiled binary for future execution. The dispatcher implements a cached calling mechanism to avoid recompiling the function on subsequent calls. When we call our compiled function with a numpy array the dispatcher will retreives the underlying input arrays and allocates a new empty output array of the same shape. Then it will convert the numpy array to a memref descriptor and pass it to the compiled function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Get the input array and its shape&lt;/span&gt;
    input_array &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    original_shape &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input_array&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape

    &lt;span class=&quot;token comment&quot;&gt;# Flatten the input array&lt;/span&gt;
    flattened_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input_array&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    flattened_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flattened_input&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert to memrefs&lt;/span&gt;
    memrefs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        as_memref_descriptor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flattened_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_float&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        as_memref_descriptor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;flattened_output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_float&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The dispatcher then lookups the function pointer from the MCJIT compiled memory and calls it with the memref descriptors as arguments.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create function prototype for ctypes&lt;/span&gt;
prototype &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CFUNCTYPE&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; memrefs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Execute compiled function&lt;/span&gt;
cfunc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prototype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_compiled_func&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
cfunc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; memrefs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to use our compiler to compile a function we can use the &lt;code&gt;@kernel&lt;/code&gt; decorator on our own functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;opaque_pointers_enabled &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mlir_egglog &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; kernel

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;float32(float32)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cos&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sin&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now this is a &lt;em&gt;very simple&lt;/em&gt; example compiler, the symbolic interpretation approach is fundamentaly limited because it can only handle flat functions with no control flow operations and requires us to manually specify the argument types, and only supports a limited set of operations. But it&#39;s a good starting point for seeing something that works end to end to use MLIR and e-graphs for optimization.&lt;/p&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cseweb.ucsd.edu/~rtate/publications/eqsat/&quot;&gt;Equality Saturation: A New Approach to Optimization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://effect.systems/doc/egraphs-2023-egglog/paper.pdf&quot;&gt;E-graphs for Efficient Symbolic Compilation with Egglog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=N2RDQGRBrSY&quot;&gt;egglog Tutorial (EGRAPHS 2023) | Next Generation Egraphs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.csl.cornell.edu/~zhiruz/pdfs/dpopt-invited-dac2023.pdf&quot;&gt;DPopt: Differentiable Placement Optimization for Hardware Acceleration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.cl.cam.ac.uk/~ey204/teaching/ACS/R244_2024_2025/presentation/S8/ESMLIR_Timi.pdf&quot;&gt;Equality Saturation for MLIR with Egglog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://goens.org/publications/koehler-popl-24/popl24.pdf&quot;&gt;Guided Equality Saturation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://jianyicheng.github.io/assets/pdf/ChengASPLOS24.pdf&quot;&gt;SEER: Super-Optimization Explorer for High-Level Synthesis using E-graph Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://rosstate.org/publications/eqsat/eqsat_tate_lmcs11.pdf&quot;&gt;Equality Saturation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.chriswarbo.net/blog/2024-02-25-sk_logic_in_egglog_1.html&quot;&gt;Sk Logic in Egglog (1)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://goto.ucsd.edu/~mstepp/peggy/pubs/lmcs2010.pdf&quot;&gt;PEG: Combining Program Analysis with Dynamic Programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.arxiv.org/pdf/2409.03864v2.pdf&quot;&gt;RelBench: A Unified Benchmark for Relational Learning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.philipzucker.com/egraph-chr/&quot;&gt;Egraph-CHR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/3696443.3708957&quot;&gt;DialEgg: Dialect-Agnostic MLIR Optimizer using Equality Saturation with Egglog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.philipzucker.com/egglog-checkpoint/&quot;&gt;Egglog Checkpoint&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.sigplan.org/2021/04/06/equality-saturation-with-egg/&quot;&gt;Fast and Extensible Equality Saturation with egg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://uwplse.org/2023/11/14/Eqsat-theory-i.html&quot;&gt;The Theoretical Aspect of Equality Saturation (Part I)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hL2MARuBCzw&quot;&gt;Equality Saturation: A New Approach to Optimization (YouTube)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://pl.cs.princeton.edu/generals/slides/dh7120.pdf&quot;&gt;Equality Saturation: Term Extraction and an Application to Network Synthesis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://egglog-python.readthedocs.io/latest/explanation/2023_11_09_portland_state.html&quot;&gt;Egglog (for Equality Saturation) - Portland State Verification Seminar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/3696443.3708922&quot;&gt;End-to-End Compilation with Equality Saturation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://egglog-python.readthedocs.io/latest/explanation/pldi_2023_presentation.html&quot;&gt;Egglog (for Equality Saturation) - PLDI Presentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=35593635&quot;&gt;Ask HN: What exciting research or projects are happening with compilers?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Mon, 17 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_egraphs/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_egraphs/</guid>
    </item>
    
    <item>
      <title>MLIR Part 5 - Neural Networks</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/neural_network2.png&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;neural-networks&quot; tabindex=&quot;-1&quot;&gt;Neural Networks&lt;/h1&gt;
&lt;p&gt;In our journey through MLIR and modern compiler technology, we&#39;ve explored how to build efficient systems for numerical computation. Now, we turn our attention to one of the most important applications of these systems: deep learning. Before diving into how MLIR can optimize neural network computations, it&#39;s crucial to understand how neural networks work from first principles.&lt;/p&gt;
&lt;p&gt;This section presents a ground-up implementation of a neural network library in pure Python. While this implementation prioritizes clarity over performance, it serves as an excellent foundation for understanding the optimizations that MLIR can provide. By building everything from scratch, we&#39;ll gain deep insights into:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The mathematical foundations of neural networks&lt;/li&gt;
&lt;li&gt;How automatic differentiation enables learning&lt;/li&gt;
&lt;li&gt;The computational patterns that make neural networks ideal for hardware acceleration&lt;/li&gt;
&lt;li&gt;Why frameworks like PyTorch and TensorFlow are structured the way they are&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In later sections, we&#39;ll transform this simple implementation into highly optimized CUDA kernels using MLIR. This progression from a clear but naive implementation to a high-performance compiled version will demonstrate the power of modern compiler technology in deep learning.&lt;/p&gt;
&lt;p&gt;The complete source code for this section is available in a &lt;a href=&quot;https://github.com/sdiehl/tinynn&quot;&gt;separate repository&lt;/a&gt; for reference.&lt;/p&gt;
&lt;p&gt;Neural networks are everywhere these days, I won&#39;t explain too much about why they&#39;re useful but if you&#39;ve used any of the popular libraries like Jax, PyTorch, or TensorFlow, you know they make it easy to build and train complex models. Understanding how these frameworks work under the hood is invaluable. In this section, we&#39;ll build a very minimal neural network library from first principles, providing insight into the core mechanisms that power modern deep learning.&lt;/p&gt;
&lt;h2 id=&quot;gradients-and-optimization&quot; tabindex=&quot;-1&quot;&gt;Gradients and Optimization&lt;/h2&gt;
&lt;p&gt;At the heart of neural network training lies the concept of gradients. Think of a gradient as a compass that points in the direction of steepest increase for a function. For a function with multiple inputs $f(x_1, x_2, ..., x_n)$, the gradient $&#92;nabla f$ is a vector containing the rate of change in each direction:&lt;/p&gt;
&lt;p&gt;$$&#92;nabla f = &#92;begin{bmatrix}&lt;br /&gt;
&#92;frac{&#92;partial f}{&#92;partial x_1} &#92;&lt;br /&gt;
&#92;frac{&#92;partial f}{&#92;partial x_2} &#92;&lt;br /&gt;
&#92;vdots &#92;&lt;br /&gt;
&#92;frac{&#92;partial f}{&#92;partial x_n}&lt;br /&gt;
&#92;end{bmatrix}$$&lt;/p&gt;
&lt;p&gt;Each component $&#92;frac{&#92;partial f}{&#92;partial x_i}$ tells us how much the function would change if we slightly adjusted that input while keeping all others constant. This information is crucial for training neural networks.&lt;/p&gt;
&lt;p&gt;In neural network training, we want to minimize a loss function $L(&#92;theta)$ that measures how well our model performs. The parameters $&#92;theta$ include all the weights and biases in the network - often thousands or millions of values. The gradient $&#92;nabla L(&#92;theta)$ tells us how to adjust each parameter to reduce the loss:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;theta_{t+1} = &#92;theta_t - &#92;alpha &#92;nabla L(&#92;theta_t)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This is gradient descent in action: we repeatedly take small steps (controlled by the learning rate $&#92;alpha$) in the direction opposite to the gradient, gradually finding parameter values that minimize the loss.&lt;/p&gt;
&lt;p&gt;Computing these gradients efficiently is crucial. While we could use numerical approximations:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial f}{&#92;partial x_i} &#92;approx &#92;frac{f(x + h&#92;mathbf{e}_i) - f(x)}{h}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $h$ is a small number and $&#92;mathbf{e}_i$ is the unit vector in direction $i$, this approach is both slow and numerically unstable. Instead, we use automatic differentiation, which computes exact gradients by systematically applying the chain rule through a computation graph.&lt;/p&gt;
&lt;p&gt;The chain rule states that for composite functions, derivatives multiply:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{d}{dx}(f(g(x))) = f&#39;(g(x)) &#92;cdot g&#39;(x)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In higher dimensions, this becomes:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial f}{&#92;partial x_i} = &#92;sum_j &#92;frac{&#92;partial f}{&#92;partial y_j} &#92;frac{&#92;partial y_j}{&#92;partial x_i}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $y_j$ are intermediate values in our computation. This is the foundation of backpropagation, which we&#39;ll implement in our neural network library.&lt;/p&gt;
&lt;h2 id=&quot;neural-network-theory&quot; tabindex=&quot;-1&quot;&gt;Neural Network Theory&lt;/h2&gt;
&lt;p&gt;A neural network is fundamentally a function that learns to map inputs to outputs through a series of adjustable transformations. The basic building block is the artificial neuron, which mimics how biological neurons process information:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It receives multiple input signals ($x_1, x_2, ..., x_n$)&lt;/li&gt;
&lt;li&gt;Each input is weighted by a strength parameter ($w_1, w_2, ..., w_n$)&lt;/li&gt;
&lt;li&gt;The weighted inputs are summed together with a bias term ($b$)&lt;/li&gt;
&lt;li&gt;The result is passed through an activation function ($&#92;sigma$)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Mathematically, this process is described as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
z = &#92;sum_{i=1}^{n} w_i x_i + b&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
a = &#92;sigma(z)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $z$ is the weighted sum (often called the pre-activation), and $a$ is the neuron&#39;s output (or activation).&lt;/p&gt;
&lt;p&gt;Multiple neurons are organized into layers, and layers are stacked to form a network. In a fully-connected feed-forward neural network, each neuron connects to every neuron in the next layer. For a layer $l$, we can write this compactly in matrix form:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;mathbf{Z}^{[l]} = &#92;mathbf{W}^{[l]} &#92;mathbf{A}^{[l-1]} + &#92;mathbf{b}^{[l]}&lt;br /&gt;
$$&lt;br /&gt;
$$&lt;br /&gt;
&#92;mathbf{A}^{[l]} = &#92;sigma(&#92;mathbf{Z}^{[l]})&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$&#92;mathbf{W}^{[l]}$ is the weight matrix for layer $l$&lt;/li&gt;
&lt;li&gt;$&#92;mathbf{A}^{[l-1]}$ contains the activations from the previous layer&lt;/li&gt;
&lt;li&gt;$&#92;mathbf{b}^{[l]}$ is the bias vector&lt;/li&gt;
&lt;li&gt;$&#92;sigma$ applies the activation function to each element&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The choice of activation function is crucial as it introduces non-linearity that allows the network to learn complex patterns. Without non-linearity, a neural network would behave like a linear model, regardless of the number of layers it has. This is because the composition of linear functions is still a linear function. Non-linear activation functions enable the network to approximate complex functions and capture intricate relationships in the data. They allow the model to learn from errors and adjust weights in a way that can represent a wider variety of functions. Common choices of activation functions are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ReLU (Rectified Linear Unit)&lt;/strong&gt;: $&#92;sigma(z) = &#92;max(0, z)$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and computationally efficient&lt;/li&gt;
&lt;li&gt;Helps prevent vanishing gradients&lt;/li&gt;
&lt;li&gt;Most widely used in modern networks&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sigmoid&lt;/strong&gt;: $&#92;sigma(z) = &#92;frac{1}{1 + e^{-z}}$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Squashes values to range [0,1]&lt;/li&gt;
&lt;li&gt;Historically popular but prone to vanishing gradients&lt;/li&gt;
&lt;li&gt;Still useful for binary classification&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tanh&lt;/strong&gt;: $&#92;sigma(z) = &#92;tanh(z) = &#92;frac{e^z - e^{-z}}{e^z + e^{-z}}$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Similar to sigmoid but ranges [-1,1]&lt;/li&gt;
&lt;li&gt;Often performs better than sigmoid&lt;/li&gt;
&lt;li&gt;Still can suffer from vanishing gradients&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;SwiGLU&lt;/strong&gt;: $&#92;sigma(z) = &#92;text{ReLU}(zW_1 + b_1)V + b_2$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Introduced in the GLU (Gated Linear Unit) family&lt;/li&gt;
&lt;li&gt;Often used in the Transformer architecture&lt;/li&gt;
&lt;li&gt;Provides a balance between non-linearity and computational efficiency&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;SwiGLU has become the default choice in many modern networks. I won&#39;t go into the details of the GLU family here, but if you&#39;re interested in learning more, I recommend checking out the &lt;a href=&quot;https://arxiv.org/abs/2002.05202&quot;&gt;GLU paper&lt;/a&gt;. For our purposes, we&#39;ll use ReLU as our activation function because it&#39;s simpler.&lt;/p&gt;
&lt;h2 id=&quot;automatic-differentiation&quot; tabindex=&quot;-1&quot;&gt;Automatic Differentiation&lt;/h2&gt;
&lt;p&gt;Automatic differentiation is the engine that powers neural network learning. Unlike numerical differentiation (which approximates derivatives) or symbolic differentiation (which manipulates mathematical expressions), automatic differentiation computes exact derivatives by tracking operations in a computation graph.&lt;/p&gt;
&lt;p&gt;Our implementation centers on the &lt;code&gt;Value&lt;/code&gt; class, which wraps a scalar value and records the operations performed on it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _children&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _op&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; data
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;label &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; label
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_backward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_prev &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_children&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_op &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; _op
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each operation (like addition or multiplication) creates a new &lt;code&gt;Value&lt;/code&gt; and defines how gradients should flow backward. For example, here&#39;s how addition works:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__add__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    other &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; other &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_backward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# The gradient flows equally to both inputs&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad
        other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad

    out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_backward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; _backward
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; out
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For multiplication, the gradient follows the product rule - each input&#39;s gradient is scaled by the other input&#39;s value:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__mul__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    other &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; other &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_backward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Each input&#39;s gradient is scaled by the other&#39;s value&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad
        other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad

    out&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_backward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; _backward
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; out
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The backward pass is initiated by calling &lt;code&gt;backward()&lt;/code&gt;, which:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Performs a topological sort of the computation graph&lt;/li&gt;
&lt;li&gt;Sets the gradient of the output to 1.0&lt;/li&gt;
&lt;li&gt;Propagates gradients backward through the graph&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Sort nodes in topological order&lt;/span&gt;
    topo &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    visited &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;topo_sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; visited&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            visited&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; child &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_prev&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                topo_sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;child&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            topo&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    topo_sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Backpropagate gradients&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;topo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        v&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_backward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To see this in action, let&#39;s create a simple computation graph for the expression &lt;code&gt;relu(a * b + c)&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create input values&lt;/span&gt;
a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Build computation graph&lt;/span&gt;
d &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b
d&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;label &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;d&quot;&lt;/span&gt;
e &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; d &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; c
e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;label &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;e&quot;&lt;/span&gt;
f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;label &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;f&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can compute the gradients of the output with respect to each input:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Gradients after backward pass:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;a.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;b.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;c.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;d.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;d&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;e.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;f.grad = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting computation graph is shown in Figure 1. Each node represents a value, and edges show how values are combined through operations. When we call &lt;code&gt;f.backward()&lt;/code&gt;, gradients flow backward through this graph, allowing us to compute how each input affects the final output.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tinynn/basic_operations_graph.svg&quot; width=&quot;300px&quot; alt=&quot;Computation Graph Visualization&quot; /&gt;
    &lt;br /&gt;
    &lt;em&gt;Figure 1: Visualization of a computation graph showing how operations are connected and gradients flow backward.&lt;/em&gt;
&lt;/p&gt;
&lt;h2 id=&quot;backpropagation&quot; tabindex=&quot;-1&quot;&gt;Backpropagation&lt;/h2&gt;
&lt;p&gt;Backpropagation is the algorithm that enables neural networks to learn from data by efficiently computing gradients of the loss function with respect to the network parameters. It applies the chain rule of calculus to propagate error gradients backward through the network, from the output layer to the input layer.&lt;/p&gt;
&lt;p&gt;The process begins with a forward pass, where input data is fed through the network to produce predictions. The loss function $L$ quantifies the difference between these predictions and the true target values. For a mean squared error loss, this is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
L = &#92;frac{1}{n} &#92;sum_{i=1}^{n} (y_i - &#92;hat{y}_i)^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $y_i$ are the true values and $&#92;hat{y}_i$ are the predicted values.&lt;/p&gt;
&lt;p&gt;The goal of training is to minimize this loss by adjusting the network parameters (weights and biases). This requires computing the gradient of the loss with respect to each parameter:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial L}{&#92;partial w_{ij}^{[l]}}&lt;br /&gt;
&#92;text{ and }&lt;br /&gt;
&#92;frac{&#92;partial L}{&#92;partial b_i^{[l]}}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;for each weight $w_{ij}^{[l]}$ and bias $b_i^{[l]}$ in layer $l$.&lt;/p&gt;
&lt;p&gt;Backpropagation computes these gradients efficiently by working backward through the network. For the output layer $L$, we first compute the error term:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;delta^{[L]} = &#92;nabla_a L &#92;odot &#92;sigma&#39;(z^{[L]})&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $&#92;nabla_a L$ is the gradient of the loss with respect to the output activations, $&#92;sigma&#39;(z^{[L]})$ is the derivative of the activation function evaluated at the pre-activation values $z^{[L]}$, and $&#92;odot$ denotes element-wise multiplication.&lt;/p&gt;
&lt;p&gt;For hidden layers, the error term is computed recursively:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;delta^{[l]} = ((w^{[l+1]})^T &#92;delta^{[l+1]}) &#92;odot &#92;sigma&#39;(z^{[l]})&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $(w^{[l+1]})^T$ is the transpose of the weight matrix for layer $l+1$.&lt;/p&gt;
&lt;p&gt;Finally, the gradients of the loss with respect to the parameters are:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial L}{&#92;partial w_{ij}^{[l]}} = a_j^{[l-1]} &#92;delta_i^{[l]}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial L}{&#92;partial b_i^{[l]}} = &#92;delta_i^{[l]}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $a_j^{[l-1]}$ is the activation of neuron $j$ in layer $l-1$.&lt;/p&gt;
&lt;p&gt;In our implementation, these calculations are handled automatically by the &lt;code&gt;Value&lt;/code&gt; class and its operations. When we call &lt;code&gt;loss.backward()&lt;/code&gt;, the gradients are computed and stored in the &lt;code&gt;grad&lt;/code&gt; attribute of each parameter.&lt;/p&gt;
&lt;h2 id=&quot;building-neural-network-components&quot; tabindex=&quot;-1&quot;&gt;Building Neural Network Components&lt;/h2&gt;
&lt;p&gt;With our automatic differentiation engine in place, we can build neural network components. The &lt;code&gt;Module&lt;/code&gt; class serves as the base for all neural network modules:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Base class for all neural network modules.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Sets gradients of all parameters to zero.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Returns a list of all parameters in the module.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Neuron&lt;/code&gt; class implements a single neuron that computes a weighted sum of inputs plus a bias, optionally followed by a non-linear activation function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Neuron&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;A single neuron with multiple inputs and one output.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nonlin&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;uniform&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nin&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nonlin &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nonlin

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        act &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;wi &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; xi &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; wi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; xi &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; act&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nonlin &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; act

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Layer&lt;/code&gt; class groups neurons together to process inputs in parallel:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Layer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;A layer of neurons, where each neuron has the same number of inputs.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nout&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neurons &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Neuron&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neurons&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; out

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neurons &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, the &lt;code&gt;MLP&lt;/code&gt; (Multi-Layer Perceptron) class combines layers into a complete network:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MLP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Module&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Multi-layer perceptron (fully connected feed-forward neural network).&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nouts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        sz &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;nin&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; nouts
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;layers &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            Layer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sz&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sz&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nonlin&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nouts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nouts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; layer &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;layers&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; layer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; layer &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;layers &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; layer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This modular design makes it easy to create networks of any size. For example, a small network for binary classification might look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create a network with:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - 2 input features&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - 8 neurons in first hidden layer&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - 8 neurons in second hidden layer&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# - 1 output neuron&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MLP&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nin&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nouts&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The architecture is visualized in Figure 2, showing how neurons are organized into layers and layers are connected to form the complete network.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tinynn/model_architecture_2.png&quot; width=&quot;700px&quot; alt=&quot;Neural Network Architecture&quot; /&gt;
    &lt;br /&gt;
    &lt;em&gt;Figure 2: Visualization of our neural network architecture showing multiple layers of neurons.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;We can create deeper networks by adding more layers, as shown in Figure 3. Each additional layer allows the network to learn more complex features. Early layers typically learn simple patterns (like edges in images), while deeper layers combine these to recognize more complex patterns (like objects or concepts).&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tinynn/network_architecture.png&quot; width=&quot;700px&quot; alt=&quot;Neural Network Architecture&quot; /&gt;
    &lt;br /&gt;
    &lt;em&gt;Figure 3: A deeper neural network with multiple hidden layers, showing how layers can be stacked to increase model capacity.&lt;/em&gt;
&lt;/p&gt;
&lt;h2 id=&quot;optimization-and-training&quot; tabindex=&quot;-1&quot;&gt;Optimization and Training&lt;/h2&gt;
&lt;p&gt;To train our neural network, we need an optimizer that updates parameters based on gradients. Our &lt;code&gt;SGD&lt;/code&gt; (Stochastic Gradient Descent) optimizer is remarkably simple:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SGD&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
    Stochastic Gradient Descent optimizer.
    &quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; parameters&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; parameters
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; lr

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Set all parameter gradients to zero.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Updates each parameter as: p = p - lr * p.grad
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The optimizer stores references to model parameters, provides a method to zero gradients before each forward pass, and updates parameters using the gradient descent rule:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;theta_{t+1} = &#92;theta_t - &#92;alpha &#92;nabla_&#92;theta L(&#92;theta_t)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $&#92;theta$ represents the parameters, $&#92;alpha$ is the learning rate, and $&#92;nabla_&#92;theta L(&#92;theta_t)$ is the gradient of the loss function with respect to the parameters.&lt;/p&gt;
&lt;p&gt;While more sophisticated optimizers like Adam or RMSProp offer better convergence properties by adapting the learning rate for each parameter based on historical gradient information, SGD illustrates the core principle of gradient-based optimization.&lt;/p&gt;
&lt;p&gt;The training process is encapsulated in the &lt;code&gt;Trainer&lt;/code&gt; class, which handles data batching, forward and backward passes, parameter updates, and metric tracking:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; random
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matplotlib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pyplot &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; plt
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tinynn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;engine &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Value


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Trainer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; loss_fn&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;optimizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; optimizer
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;loss_fn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; loss_fn &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_mse_loss

        &lt;span class=&quot;token comment&quot;&gt;# Training history&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;losses &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;accuracies &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_mse_loss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; pred&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Mean squared error loss function.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; verbose&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; early_stopping&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Train the model on the given dataset.

        Args:
            X: Features (list or numpy array)
            y: Target values (list or numpy array)
            n_epochs: Number of training epochs
            batch_size: Mini-batch size
            verbose: Whether to print progress
            early_stopping: Whether to stop early on perfect accuracy

        Returns:
            Dictionary containing training history
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Training neural network...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; epoch &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_epochs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Track metrics for this epoch&lt;/span&gt;
            total_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;
            correct &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Shuffle the data&lt;/span&gt;
            indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shuffle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Mini-batch training&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; start_idx &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; batch_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                end_idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_idx &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; batch_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                batch_indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;start_idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;end_idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Zero gradients&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;optimizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zero_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Accumulate loss and accuracy over the batch&lt;/span&gt;
                batch_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; idx &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; batch_indices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token comment&quot;&gt;# Convert numpy features to Value objects&lt;/span&gt;
                    x_vals &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

                    &lt;span class=&quot;token comment&quot;&gt;# Forward pass&lt;/span&gt;
                    pred &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_vals&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    pred_value &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pred&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; pred

                    &lt;span class=&quot;token comment&quot;&gt;# Binary cross-entropy loss with tanh activation&lt;/span&gt;
                    target &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Use -1/1 targets for tanh&lt;/span&gt;

                    &lt;span class=&quot;token comment&quot;&gt;# Calculate loss&lt;/span&gt;
                    loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;loss_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred_value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                    &lt;span class=&quot;token comment&quot;&gt;# Accumulate loss&lt;/span&gt;
                    batch_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; batch_loss &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; loss

                    &lt;span class=&quot;token comment&quot;&gt;# Check accuracy&lt;/span&gt;
                    pred_class &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; pred_value&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; pred_class &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        correct &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Scale loss by batch size&lt;/span&gt;
                batch_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; batch_loss &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Backward pass&lt;/span&gt;
                batch_loss&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;backward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Update parameters&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;optimizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;step&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Track total loss&lt;/span&gt;
                total_loss &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; batch_loss&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data

            &lt;span class=&quot;token comment&quot;&gt;# Record metrics&lt;/span&gt;
            avg_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; total_loss &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; batch_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            accuracy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; correct &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;losses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;avg_loss&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;accuracies&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;accuracy&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Print progress every 10 epochs&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; verbose &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;epoch &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Epoch &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;epoch&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;n_epochs&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: Loss=&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;avg_loss&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.4f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, Accuracy=&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;accuracy&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.4f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Early stopping if we reach perfect accuracy&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; early_stopping &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; accuracy &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; avg_loss &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Early stopping at epoch &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;epoch&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; with 100% accuracy!&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Training complete!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Final accuracy: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;accuracies&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.4f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;losses&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;losses&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;accuracies&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;accuracies&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;plot_training_progress&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_path&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output/training_progress.png&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Plot and save the training progress.&quot;&quot;&quot;&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;figure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;figsize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subplot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;plot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;losses&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Training Loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;xlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Epoch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ylabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subplot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;plot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;accuracies&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Training Accuracy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;xlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Epoch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ylabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Accuracy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tight_layout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;savefig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;save_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;close&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each epoch, the trainer shuffles the data, processes mini-batches, computes loss and gradients, updates parameters, and tracks metrics like loss and accuracy. This structured approach to training makes it easy to experiment with different models and datasets while maintaining a consistent training procedure.&lt;/p&gt;
&lt;h2 id=&quot;l2-regularization&quot; tabindex=&quot;-1&quot;&gt;L2 Regularization&lt;/h2&gt;
&lt;p&gt;Regularization techniques are essential in machine learning to prevent overfitting, where a model performs well on training data but poorly on unseen data. L2 regularization, also known as &lt;strong&gt;weight decay&lt;/strong&gt;, is one of the most common regularization methods for neural networks.&lt;/p&gt;
&lt;p&gt;The core idea of L2 regularization is to add a penalty term to the loss function that discourages large weights:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
L_{regularized} = L_{original} + &#92;lambda &#92;sum_{w &#92;in &#92;text{weights}} w^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where $&#92;lambda$ is the regularization strength, a hyperparameter that controls the trade-off between fitting the training data and keeping the weights small.&lt;/p&gt;
&lt;p&gt;The gradient of this regularization term with respect to each weight is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;frac{&#92;partial}{&#92;partial w} &#92;left( &#92;lambda w^2 &#92;right) = 2&#92;lambda w&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This means that during the weight update step, each weight is not only adjusted based on the gradient of the original loss but also shrunk proportionally to its magnitude:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
w_{t+1} = w_t - &#92;alpha &#92;left( &#92;frac{&#92;partial L_{original}}{&#92;partial w_t} + 2&#92;lambda w_t &#92;right)&lt;br /&gt;
= (1 - 2&#92;alpha&#92;lambda)w_t - &#92;alpha&#92;frac{&#92;partial L_{original}}{&#92;partial w_t}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This has the effect of pushing weights toward zero, unless there is strong evidence from the data that they should be large. The result is a simpler model that is less likely to overfit.&lt;/p&gt;
&lt;p&gt;Implementing L2 regularization in our framework is straightforward. We can modify our loss function to include the regularization term:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_mse_loss_with_l2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; pred&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lambda_reg&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Mean squared error loss function with L2 regularization.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Original MSE loss&lt;/span&gt;
    mse_loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# L2 regularization term&lt;/span&gt;
    l2_reg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        l2_reg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; l2_reg &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; p &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p
    l2_reg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; l2_reg &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; lambda_reg
    
    &lt;span class=&quot;token comment&quot;&gt;# Combined loss&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; mse_loss &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; l2_reg
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively, we can implement L2 regularization directly in the optimizer by modifying the weight update rule:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lambda_reg&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Updates each parameter with L2 regularization.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# L2 regularization: weight decay&lt;/span&gt;
        p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grad &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; lambda_reg &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;L2 regularization has several benefits:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It helps prevent overfitting by penalizing large weights&lt;/li&gt;
&lt;li&gt;It makes the model more robust to noise in the training data&lt;/li&gt;
&lt;li&gt;It can improve generalization to unseen data&lt;/li&gt;
&lt;li&gt;It can help with numerical stability during training&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The regularization strength $&#92;lambda$ is a hyperparameter that needs to be tuned. If $&#92;lambda$ is too small, the regularization effect will be negligible; if it&#39;s too large, the model may underfit the data. Cross-validation is typically used to find an optimal value for $&#92;lambda$.&lt;/p&gt;
&lt;h2 id=&quot;end-to-end-usage&quot; tabindex=&quot;-1&quot;&gt;End-to-End Usage&lt;/h2&gt;
&lt;p&gt;To demonstrate our library in action, we train a neural network on a simple classification task: the &amp;quot;moons&amp;quot; dataset from scikit-learn. This dataset consists of two interleaving half-circles, requiring a non-linear decision boundary to separate the classes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; sklearn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; make_moons
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matplotlib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pyplot &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; plt

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tinynn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;engine &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Value
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tinynn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nn &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; MLP
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tinynn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;optim &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SGD
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tinynn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;trainer &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Trainer

&lt;span class=&quot;token comment&quot;&gt;# Generate the dataset&lt;/span&gt;
X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; make_moons&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_samples&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; noise&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; random_state&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create a neural network: 2 inputs -&gt; 32 -&gt; 32 -&gt; 16 -&gt; 1 output&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MLP&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nin&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nouts&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create an optimizer&lt;/span&gt;
optimizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SGD&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lr&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.005&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create trainer&lt;/span&gt;
trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Trainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; optimizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Train the model&lt;/span&gt;
trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; verbose&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Key training concepts:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Batch Size&lt;/strong&gt;: Determines how many training examples are processed together in each forward/backward pass. Smaller batches introduce more noise in gradient updates (which can help escape local minima) but converge more slowly. Larger batches provide more stable updates but may get stuck in suboptimal solutions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Epochs&lt;/strong&gt;: One complete pass through the training dataset. Multiple epochs are needed for the model to learn effectively. The optimal number depends on factors like dataset size, model complexity, and optimization parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Training Loss&lt;/strong&gt;: Measures how well the model&#39;s predictions match the true values. We use Mean Squared Error (MSE):&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
MSE = &#92;frac{1}{n}&#92;sum_{i=1}^n(y_{pred_i} - y_{target_i})^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;MSE is particularly suitable for our binary classification task because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It heavily penalizes large errors through the squared term&lt;/li&gt;
&lt;li&gt;It provides smooth gradients for optimization&lt;/li&gt;
&lt;li&gt;It&#39;s simple to implement and understand&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: The proportion of correct predictions. While intuitive, accuracy should be considered alongside other metrics, as it can be misleading with imbalanced datasets.&lt;/p&gt;
&lt;p&gt;The training progress is shown in Figure 4, where we can see both the loss decreasing and accuracy improving over time:&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tinynn/training_progress.png&quot; width=&quot;700px&quot; alt=&quot;Training Progress&quot; /&gt;
    &lt;br /&gt;
    &lt;em&gt;Figure 4: Training progress showing how loss decreases and accuracy improves as the model learns.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;Our simple model learns to separate the two classes effectively, as shown by the decision boundary visualization in Figure 5:&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tinynn/decision_boundary.png&quot; width=&quot;700px&quot; alt=&quot;Neural Network Decision Boundary&quot; /&gt;
    &lt;br /&gt;
    &lt;em&gt;Figure 5: The learned decision boundary shows how our model separates the two classes with a non-linear boundary.&lt;/em&gt;
&lt;/p&gt;
&lt;h2 id=&quot;optimization-opportunities&quot; tabindex=&quot;-1&quot;&gt;Optimization Opportunities&lt;/h2&gt;
&lt;p&gt;Neural network training presents numerous opportunities for parallelization across multiple dimensions. The most obvious is batch parallelism, where multiple training examples can be processed simultaneously through the network. Each example in a batch can be computed independently during the forward pass, and their gradients can be accumulated in parallel during backpropagation. Additionally, within each layer, matrix multiplications and element-wise operations can be parallelized across neurons and feature dimensions, creating a rich hierarchy of parallel computation that modern hardware can exploit.&lt;/p&gt;
&lt;p&gt;This inherent parallelism modern GPUS ideal for deep learning workloads. Unlike CPUs, which are optimized for sequential processing with complex control flow, GPUs contain thousands of simpler cores designed specifically for parallel arithmetic operations. This architecture aligns perfectly with neural network computations—matrix multiplications, convolutions, and element-wise operations can all be distributed across these cores, leading to orders of magnitude speedup compared to CPU implementations. Modern GPUs also feature specialized tensor cores that accelerate mixed-precision matrix operations commonly found in deep learning.&lt;/p&gt;
&lt;p&gt;The memory hierarchy of GPUs is another crucial factor in their deep learning performance. High-bandwidth memory and sophisticated caching mechanisms allow GPUs to feed data to their numerous processing cores efficiently, while shared memory enables fast communication between threads working on the same computation. This is particularly important for deep learning, where memory access patterns are predictable and regular, allowing for optimal utilization of the memory subsystem. Combined with their massive parallel processing capability, these memory characteristics make GPUs the backbone of modern deep learning infrastructure, enabling the training of increasingly large and complex models. We&#39;ll move onto that next.&lt;/p&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.ibm.com/think/topics/backpropagation&quot;&gt;Backpropagation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://pytorch.org/tutorials/recipes/recipes/defining_a_neural_network.html&quot;&gt;PyTorch Neural Networks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GNLOa4riys8&quot;&gt;JAX and Flax: A Simple Neural Network &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.datacamp.com/tutorial/mastering-backpropagation&quot;&gt;Mastering Backpropagation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VMj-3S1tku0&quot;&gt;The spelled-out intro to neural networks and backpropagation: building micrograd&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://uvadlc-notebooks.readthedocs.io/en/latest/tutorial_notebooks/JAX/tutorial2/Introduction_to_JAX.html&quot;&gt;Introduction to JAX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://monicaspisar.com/posts/micrograd/&quot;&gt;Micrograd&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tomasbeuzen.com/deep-learning-with-pytorch/chapters/chapter3_pytorch-neural-networks-pt1.html&quot;&gt;Deep Learning with PyTorch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://uvadlc-notebooks.readthedocs.io/en/latest/tutorial_notebooks/JAX/tutorial4/Optimization_and_Initialization.html&quot;&gt;Optimization and Initialization&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description>
      <pubDate>Sat, 15 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_neural_networks/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_neural_networks/</guid>
    </item>
    
    <item>
      <title>MLIR Part 4 - Linear Algebra in MLIR</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/linalg.jpeg&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;linear-algebra-in-mlir&quot; tabindex=&quot;-1&quot;&gt;Linear Algebra in MLIR&lt;/h1&gt;
&lt;p&gt;The Linalg dialect is a core idea in the MLIR ecosystem, it was built to make high-level tensor computations easier and more efficient. Essentially, it&#39;s all about simplifying the way we handle complex mathematical operations in a way that can be translated into fast, optimized code.  At its core, the Linalg dialect uses a straightforward, declarative approach. This means it lays out operations and transformations in a way that keeps the math intact while also allowing for a variety of optimizations. It provides high-level linear algebra operations like pointwise calculations, matrix multiplications, and convolutions as single operations.&lt;/p&gt;
&lt;h2 id=&quot;linalg.generic&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;linalg.generic&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The most generic operation in the Linalg dialect is &lt;code&gt;linalg.generic&lt;/code&gt;. This operation allows us to define a generic tensor manipulation operations over indices. It is the most generic operation and all other operations are defined as specializations of this operation. You probably won&#39;t use this directly much, but it&#39;s useful to discuss first because it&#39;s the most fundamental operation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token attribute&quot;&gt;#map_1d_identity&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@add_tensors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.generic&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// attributes&lt;/span&gt;
      indexing_maps &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token attribute&quot;&gt;#map_1d_identity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token attribute&quot;&gt;#map_1d_identity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token attribute&quot;&gt;#map_1d_identity&lt;/span&gt;
       &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      iterator_types &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;parallel&quot;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;token comment&quot;&gt;// input tensors&lt;/span&gt;
    ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;token comment&quot;&gt;// output tensor&lt;/span&gt;
    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;token comment&quot;&gt;// loop region&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s break down each component of this &lt;code&gt;linalg.generic&lt;/code&gt; operation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;indexing_maps&lt;/code&gt;: This attribute defines how input and output tensors are accessed. Here we have 3 maps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First map for first input tensor (&lt;code&gt;%arg0&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Second map for second input tensor (&lt;code&gt;%arg1&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Third map for output tensor (&lt;code&gt;%result&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All use &lt;code&gt;#map_1d_identity&lt;/code&gt; which is defined as &lt;code&gt;affine_map&amp;lt;(d0) -&amp;gt; (d0)&amp;gt;&lt;/code&gt;, meaning each element maps directly to the corresponding element (no coordinate transformations).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;iterator_types&lt;/code&gt;: Specifies the type of iteration for each dimension. Here we have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[&amp;quot;parallel&amp;quot;]&lt;/code&gt;: Single dimension marked as parallel, meaning iterations can be executed in any order or concurrently.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are three possible iterator types in linalg:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;parallel&lt;/code&gt;: Iterations can be executed in any order or concurrently (like element-wise operations)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reduction&lt;/code&gt;: Iterations contribute to a single result value (like summing along an axis)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;window&lt;/code&gt;: Iterations access a sliding window of elements (like convolutions)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ins(%arg0, %arg1 : tensor&amp;lt;10xf32&amp;gt;, tensor&amp;lt;10xf32&amp;gt;)&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Specifies the input tensors and their types&lt;/li&gt;
&lt;li&gt;Both are 1D tensors of 10 float32 elements&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;outs(%result : tensor&amp;lt;10xf32&amp;gt;)&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Specifies the output tensor and its type&lt;/li&gt;
&lt;li&gt;Also a 1D tensor of 10 float32 elements&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The region (loop body):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Takes scalar values from input tensors (%arg2, %arg3) and output tensor (%arg4)&lt;/li&gt;
&lt;li&gt;Performs floating point addition&lt;/li&gt;
&lt;li&gt;Yields result back using &lt;code&gt;linalg.yield&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This would perform the standard vectorized addition of two tensors element-wise, just like in NumPy:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# array([3, 5, 7])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If this seems like a lot, don&#39;t worry it is. There are much simpler ways to write this that compile down to the same thing.&lt;/p&gt;
&lt;h2 id=&quot;linalg.map&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;linalg.map&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The same operation can be written is a generic &lt;code&gt;linalg.map&lt;/code&gt; operation which abstracts away the precise details of the loop nest and index manipulations, instead directly applying the &lt;code&gt;arith.addf&lt;/code&gt; operation to the input tensors.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@addv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;l &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;bufferization.to_memref&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.copy &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are also named operations for common arithmetic map operations that are named specializations of the &lt;code&gt;linalg.map&lt;/code&gt; operation.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;linalg.add&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linalg.mul&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linalg.div&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linalg.sub&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;linalg.reduce&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;linalg.reduce&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;linalg.reduce&lt;/code&gt; operation is a powerful primitive that can be used to implement various reduction operations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@add_tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;_ssa_0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf64&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;reduce &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.reduce&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;_ssa_0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf64&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; dimensions &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.extract &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;reduce &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f64&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In NumPy this operation can be written as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;multiply&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;linalg.matmul&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;linalg.matmul&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;We can also define a generic matrix multiplication operation in terms of direct tensor contractions. The matrix multiplication operation shown in the MLIR code can be written in conventional tensor notation as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
C_{ij} = &#92;sum_{k=1}^{10} A_{ik} B_{kj}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$A$ is the 8×10 input matrix (&lt;code&gt;memref&amp;lt;8x10xf32&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;$B$ is the 10×16 input matrix (&lt;code&gt;memref&amp;lt;10x16xf32&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;$C$ is the 8×16 output matrix (&lt;code&gt;memref&amp;lt;8x16xf32&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;$i$ ranges from 1 to 8&lt;/li&gt;
&lt;li&gt;$j$ ranges from 1 to 16&lt;/li&gt;
&lt;li&gt;$k$ is the summation index ranging from 1 to 10&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This matches the MLIR implementation where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;parallel&lt;/code&gt; iterator types correspond to the free indices $i$ and $j$&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;reduction&lt;/code&gt; iterator type corresponds to the summation over $k$&lt;/li&gt;
&lt;li&gt;The indexing maps &lt;code&gt;(i, j, k) -&amp;gt; (i, k)&lt;/code&gt;, &lt;code&gt;(i, j, k) -&amp;gt; (k, j)&lt;/code&gt;, and &lt;code&gt;(i, j, k) -&amp;gt; (i, j)&lt;/code&gt; correspond to accessing $A_{ik}$, $B_{kj}$, and $C_{ij}$ respectively&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.generic&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        indexing_maps &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        iterator_types &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;parallel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;parallel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;reduction&quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lhs_one&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;rhs_one&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init_one&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tmp0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lhs_one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;rhs_one &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tmp1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init_one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tmp0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
            &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tmp1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or in the much more succinct form using &lt;code&gt;linalg.matmul&lt;/code&gt; operation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.matmul&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The tensor operations are then lowered into a sequence of loops using either the &lt;code&gt;affine.for&lt;/code&gt; or &lt;code&gt;scf.for&lt;/code&gt; operations using the &lt;code&gt;convert-linalg-to-loops&lt;/code&gt; or &lt;code&gt;convert-linalg-to-affine-loops&lt;/code&gt; passes.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c8 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c16 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c8 step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c16 step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;broadcasting&quot; tabindex=&quot;-1&quot;&gt;Broadcasting&lt;/h2&gt;
&lt;p&gt;Broadcasting in MLIR&#39;s Linalg dialect allows tensors of different shapes to be combined in operations, similar to NumPy&#39;s broadcasting rules. The smaller tensor is implicitly expanded to match the shape of the larger tensor along broadcast dimensions. This is particularly useful for operations like adding a vector to each row/column of a matrix or applying scalar operations to tensors.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at some common broadcasting patterns:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@broadcast_1d_to_2d&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x3xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Create an empty output tensor&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x3xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Broadcast the input tensor along dimension 1 (columns)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.broadcast&lt;/span&gt; 
    ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x3xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    dimensions &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x3xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In NumPy this operation can be written as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;arg0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;broadcast_to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# array([[1, 2, 3],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#        [1, 2, 3],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#        [1, 2, 3]])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also combine broadcasting with other operations. For example, adding a vector to each row of a matrix:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@add_vector_to_matrix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;matrix&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// First broadcast the vector&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;broadcasted &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.broadcast&lt;/span&gt;
    ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    dimensions &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
  &lt;span class=&quot;token comment&quot;&gt;// Then add the broadcasted result to the matrix&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;broadcasted&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the equivalent to the common NumPy operation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;matrix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
vector &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; vector
&lt;span class=&quot;token comment&quot;&gt;# array([[2., 3., 4., 5.],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#        [2., 3., 4., 5.],&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;#        [2., 3., 4., 5.]])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;kernel-fusion&quot; tabindex=&quot;-1&quot;&gt;Kernel Fusion&lt;/h2&gt;
&lt;p&gt;Kernel fusion is an important optimization technique that combines multiple operations into a single operation, reducing memory bandwidth requirements and improving performance. The Linalg dialect provides built-in support for fusing elementwise operations through its optimization passes.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at a simple example where we have two operations - addition followed by multiplication - that we want to fuse together. First, we&#39;ll write these as separate operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@addmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// First create an empty tensor for intermediate results&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Add operation&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.add&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Create another empty tensor for the multiplication result&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Multiply operation&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.mul&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we have two distinct operations: &lt;code&gt;linalg.add&lt;/code&gt; followed by &lt;code&gt;linalg.mul&lt;/code&gt;. Each operation requires reading from memory and writing back to memory, which can be inefficient.&lt;/p&gt;
&lt;p&gt;Through MLIR&#39;s optimization passes, we can fuse these operations into a single kernel that performs both operations in one pass over the data:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@addmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.generic&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;indexing_maps &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        iterator_types &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;parallel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in_0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in_1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in_0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;in_1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get some intuition for how this works, let&#39;s look at the equivalent Python code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Unfused version - two separate loops&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unfused_addmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# First loop - addition&lt;/span&gt;
    temp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        temp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Second loop - multiplication&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result

&lt;span class=&quot;token comment&quot;&gt;# Fused version - single loop&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fused_addmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Combine add and multiply in one loop iteration&lt;/span&gt;
        result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result

a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Both produce same result but fused version only loops once&lt;/span&gt;
result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fused_addmul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# [(1+1)*2, (2+1)*2, (3+1)*2, ...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The fused version combines both operations into a single &lt;code&gt;linalg.generic&lt;/code&gt; operation that reads each input only once and writes the final result directly. This eliminates the intermediate tensor allocation and memory accesses.&lt;/p&gt;
&lt;p&gt;To perform this fusion, we need to apply a specific sequence of optimization passes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;--canonicalize&lt;/code&gt;: Performs general cleanups and simplifications&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--linalg-fuse-elementwise-ops&lt;/code&gt;: Fuse elementwise operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--cse&lt;/code&gt;: Common Subexpression Elimination to clean up redundant operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--linalg-generalize-named-ops&lt;/code&gt;: Converts named ops (like &lt;code&gt;linalg.add&lt;/code&gt;) to generic form&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--convert-linalg-to-loops&lt;/code&gt;: Converts the fused operations to explicit loops&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can run these passes and output the fused &lt;code&gt;addmul&lt;/code&gt; operation to &lt;code&gt;fused_ops.mlir&lt;/code&gt;, reducing the time complexity from two loops over $n$ to a single loop over $n$.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-opt separate_ops.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--canonicalize&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --linalg-fuse-elementwise-ops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--cse&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --linalg-generalize-named-ops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --linalg-fuse-elementwise-ops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-linalg-to-loops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; fused_ops.mlir
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This sequence of passes ensures that operations are properly fused and then lowered to a form that can be efficiently executed. The fusion optimization is particularly important in deep learning workloads where chains of elementwise operations are common.&lt;/p&gt;
&lt;h2 id=&quot;tiling&quot; tabindex=&quot;-1&quot;&gt;Tiling&lt;/h2&gt;
&lt;p&gt;Loop tiling is a crucial optimization technique that improves memory access patterns and cache utilization. In modern computer architectures, memory access is often the bottleneck rather than computation. When processing large matrices or tensors, if we access memory in a linear fashion, we may constantly need to fetch new data from main memory, which is much slower than cache memory.&lt;/p&gt;
&lt;p&gt;Tiling breaks down large computations into smaller blocks that better fit into cache memory. For example, in matrix multiplication, instead of computing the entire row-column dot product at once (which might exceed cache size), we break it into smaller tiles that can reside in cache memory. This reduces cache misses and improves performance.&lt;/p&gt;
&lt;p&gt;This optimization is particularly important for GPU architectures, where memory hierarchy and access patterns significantly impact performance. GPUs have a complex memory hierarchy including global memory, shared memory (similar to cache), and registers. By tiling computations to match the GPU&#39;s hardware characteristics—like shared memory size and warp size—we can dramatically improve performance. For instance, in CUDA programming, a common pattern is to load a tile of data into shared memory, synchronize threads, and then have each thread work on elements within that tile. More on this in the later section on GPU programming.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/tiling.png&quot; width=&quot;500px&quot; alt=&quot;Affine Tiling&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Here&#39;s an example of using MLIR to perform loop tiling on a matrix multiplication operation. We&#39;ll start with a simple tensor-based matmul and transform it into tiled affine loops.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;linalg.matmul&lt;/span&gt; 
           ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
           outs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To transform this into tiled loops, we need several transformation passes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;--convert-tensor-to-linalg&lt;/code&gt;: Converts tensor operations to linalg dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--linalg-generalize-named-ops&lt;/code&gt;: Converts named operations (like matmul) into generic linalg operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--one-shot-bufferize=&amp;quot;bufferize-function-boundaries&amp;quot;&lt;/code&gt;: Converts tensor operations to use buffers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--buffer-deallocation-pipeline&lt;/code&gt;: Handles buffer deallocation&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--convert-bufferization-to-memref&lt;/code&gt;: Converts buffer operations to memref operations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--convert-linalg-to-affine-loops&lt;/code&gt;: Lowers linalg operations to affine loops&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--affine-loop-tile=&amp;quot;tile-size=5&amp;quot;&lt;/code&gt;: Tiles the affine loops with size 5&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--canonicalize --cse&lt;/code&gt;: Cleanup passes&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&#39;s the complete transformation pipeline:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-opt tiling.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-tensor-to-linalg &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --linalg-generalize-named-ops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --one-shot-bufferize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bufferize-function-boundaries&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --buffer-deallocation-pipeline &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-bufferization-to-memref &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-linalg-to-affine-loops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --affine-loop-tile&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tile-size=5&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--canonicalize&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--cse&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; tiling-transformed.mlir
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This produces the following tiled implementation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token attribute&quot;&gt;#map1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg6 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token attribute&quot;&gt;#map1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg7 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token attribute&quot;&gt;#map1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg8 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token attribute&quot;&gt;#map1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg5&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg6&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg8&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg8&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg7&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg6&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg7&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
                affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg6&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg7&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
              &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;cast &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.copy &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;cast &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The tiled output shows the original 10x10 matrix multiplication broken into smaller 5x5 tiles, which can help improve cache utilization and locality of memory accesses.&lt;/p&gt;
&lt;h2 id=&quot;external-references&quot; tabindex=&quot;-1&quot;&gt;External References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/Linalg/&quot;&gt;MLIR: Linalg Dialect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://youtu.be/A805W2KSCxQ?si=UElNauFz86VonSBN&quot;&gt;MLIR Open Meeting 2022-01-27: Introduction to Linalg.generic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7-xAzlda0F8&quot;&gt;2024 EuroLLVM - MLIR Linalg Op Fusion - Theory &amp;amp; Practice&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://numpy.org/doc/stable/user/basics.broadcasting.html&quot;&gt;NumPy Broadcasting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.lei.chat/posts/mlir-codegen-dialects-for-machine-learning-compilers/&quot;&gt;MLIR Codegen Dialects for Machine Learning Compilers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.lei.chat/posts/mlir-linalg-dialect-and-patterns/&quot;&gt;MLIR Linalg Dialect and Patterns&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Rationale/RationaleLinalgDialect/&quot;&gt;Linalg Dialect Rationale: The Case For Compiler-Friendly Custom Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/OpenMeetings/2022-01-27-Intro-to-Linalg.pdf&quot;&gt;Anatomy of Linalg.generic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://gist.github.com/bjacob/2e662b3d2259d99aec15a43bf0e7b325&quot;&gt;IREE/MLIR/Linalg tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Fri, 14 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_linear_algebra/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_linear_algebra/</guid>
    </item>
    
    <item>
      <title>MLIR Part 3 - Affine Dialect and OpenMP</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/parallel.jpeg&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;affine-dialect-and-openmp&quot; tabindex=&quot;-1&quot;&gt;Affine Dialect and OpenMP&lt;/h1&gt;
&lt;p&gt;The affine dialect in MLIR provides abstractions for expressing loops and array accesses that are amenable to parallel execution. Unlike the &lt;code&gt;scf&lt;/code&gt; dialect we&#39;ve seen before, the affine dialect enables advanced loop transformations and automatic parallelization. It&#39;s esesntially &amp;quot;first class loops&amp;quot; which is something we always used to layer on top of our LLVM.&lt;/p&gt;
&lt;p&gt;The affine dialect many advantages, including automatic parallelization opportunities, built-in vectorization capabilities, sophisticated loop optimizations, a natural expression of array computations, and integration with OpenMP.&lt;/p&gt;
&lt;h2 id=&quot;polyhedral-optimization-model&quot; tabindex=&quot;-1&quot;&gt;Polyhedral Optimization Model&lt;/h2&gt;
&lt;p&gt;The polyhedral model is a mathematical framework that represents loop nests and their computations as geometric objects in multi-dimensional spaces.&lt;/p&gt;
&lt;p&gt;At its core, the polyhedral model represents the execution of statements inside loop nests as points in a multi-dimensional space. Each point corresponds to a specific instance of a statement in the execution, defined by its iteration coordinates. This representation enables program analysis and transformation techniques by converting complex loop optimization problems into geometric operations on polyhedra.&lt;/p&gt;
&lt;p&gt;The key components of the polyhedral model include iteration domains, access relations, and scheduling functions. Iteration domains define the set of all iterations executed by a loop nest, represented as a set of integer points bounded by affine constraints. Access relations map iteration points to the memory locations accessed by each statement. Scheduling functions determine the order of execution for iteration points, enabling transformations that preserve program semantics while improving performance characteristics.&lt;/p&gt;
&lt;p&gt;MLIR provides the &lt;code&gt;affine&lt;/code&gt; that incorporates polyhedral concepts as first-class citizens. This hybrid design optimizes the representation, analysis, and transformation of high-level dataflow graphs and target-specific code for high-performance data-parallel systems.&lt;/p&gt;
&lt;p&gt;There are three core concepts we have to understand:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Affine maps&lt;/strong&gt;: Multi-dimensional quasi-linear functions that map dimensions and symbols to results. For example, &lt;code&gt;(d0, d1, d2, s0) → (d0 + d1, s0 * d2)&lt;/code&gt; represents a two-dimensional affine map that maps dimension arguments &lt;code&gt;d0&lt;/code&gt;, &lt;code&gt;d1&lt;/code&gt;, &lt;code&gt;d2&lt;/code&gt; and symbol argument &lt;code&gt;s0&lt;/code&gt; to two results.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Integer sets&lt;/strong&gt;: Constraints on dimensions and symbols represented as affine inequalities and equalities. For instance, &lt;code&gt;(i)[N, M] : (i &amp;gt;= 0, -i + N &amp;gt;= 0, N - 5 == 0, -i + M + 1 &amp;gt;= 0)&lt;/code&gt; represents the set of values &lt;code&gt;i&lt;/code&gt; such that &lt;code&gt;0 &amp;lt;= i &amp;lt; N&lt;/code&gt;, &lt;code&gt;N = 5&lt;/code&gt;, and &lt;code&gt;i &amp;lt;= M + 1&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Affine operations&lt;/strong&gt;: Operations such as &lt;code&gt;affine.for&lt;/code&gt;, &lt;code&gt;affine.if&lt;/code&gt;, and &lt;code&gt;affine.parallel&lt;/code&gt; that leverage affine maps and integer sets to represent loop nests and conditionals with affine constraints.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Rather than front-load the theory, let&#39;s just look at some examples.&lt;/p&gt;
&lt;h2 id=&quot;matrix-multiplication&quot; tabindex=&quot;-1&quot;&gt;Matrix Multiplication&lt;/h2&gt;
&lt;p&gt;The classic matrix product of two matrices $A_{ij}$ and $B_{jk}$ is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
C_{ik} = (A B)_{ik} = &#92;sum_{j=1}^{N} A_{ij}B_{jk}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Expressing this in MLIR as an explicit loop nest would look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;K &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Sequential implementation&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;K &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that the two loops over &lt;code&gt;%i&lt;/code&gt; and &lt;code&gt;%j&lt;/code&gt; have no dependencies between different iterations of these loops. Using the &lt;code&gt;-affine-parallelize&lt;/code&gt; pass, this can be transformed into:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@matmul_parallel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;K &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.dim &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Parallel implementation&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.parallel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;K &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;affine.parallel&lt;/code&gt; operation represents a way to express that multiple iterations of a loop can be executed independently and potentially simultaneously. Think of it like saying &amp;quot;these calculations can happen in any order, or all at once&amp;quot; - similar to how you might divide up work among multiple workers who don&#39;t need to communicate with each other.&lt;/p&gt;
&lt;p&gt;More precisely, &lt;code&gt;affine.parallel&lt;/code&gt; defines a set of nested loops where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each iteration is independent of all other iterations&lt;/li&gt;
&lt;li&gt;The iterations can be executed in any order or simultaneously&lt;/li&gt;
&lt;li&gt;The operation produces results through reductions (like summing up values) where the order of combining results doesn&#39;t matter&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Technically, it creates a &amp;quot;hyper-rectangular parallel band,&amp;quot; which means it can represent multiple nested parallel loops, each characterized by its own induction variables (loop counters), lower and upper bounds, step sizes (indicating how much to increment each iteration), and reduction operations (defining how to combine results from different iterations).&lt;/p&gt;
&lt;p&gt;The body of the parallel region must end with &lt;code&gt;affine.yield&lt;/code&gt; which specifies how to combine results from different iterations using operations like addition or multiplication. For loops that execute zero times, the result is the identity value for that operation (0 for addition, 1 for multiplication).&lt;/p&gt;
&lt;h2 id=&quot;affine-maps-(affine.apply)&quot; tabindex=&quot;-1&quot;&gt;Affine Maps (&lt;code&gt;affine.apply&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;Affine maps are a key component of the affine dialect, representing multi-dimensional affine transformations. They provides us a way to describe the mapping of loop indices to memory access patterns, which is essential for optimizing memory layouts and access patterns in high-performance computing.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at a comprehensive example that demonstrates various uses of &lt;code&gt;affine.apply&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Define some reusable affine maps&lt;/span&gt;
&lt;span class=&quot;token attribute&quot;&gt;#tile_map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0 floordiv &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token attribute&quot;&gt;#offset_map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;s0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; s0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token attribute&quot;&gt;#complex_map&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;s0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; d1 floordiv &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; s0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@affine_apply_examples&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Create some test indices&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c128 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Example 1: Simple tiling calculation&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Calculate which tile a given index belongs to (tile size = 32)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tile_idx &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#tile_map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Example 2: Inline affine map for offset calculation&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;offset &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Example 3: Using a symbol parameter&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;shifted &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#offset_map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c128&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Example 4: Multiple dimensions and a symbol&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;complex &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#complex_map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c128&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  
  &lt;span class=&quot;token comment&quot;&gt;// Example 5: Composition of affine applies&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;temp &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c42&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;final &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Example showing how affine.apply can be used in a practical loop context&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@tiled_loop&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c256 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c32 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Outer loop iterates over tiles&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Inner loop processes elements within the tile&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; d1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token comment&quot;&gt;// Process value...&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;tiled_loop&lt;/code&gt; function shows how &lt;code&gt;affine.apply&lt;/code&gt; can be used in a real-world scenario to implement tiled processing of an array. The outer loop steps by the tile size, and &lt;code&gt;affine.apply&lt;/code&gt; is used to calculate the bounds for the inner loop that processes each tile.&lt;/p&gt;
&lt;p&gt;Some key points about affine maps include that &lt;code&gt;floordiv&lt;/code&gt; performs integer division rounding down, and the syntax &lt;code&gt;(d0)[s0]&lt;/code&gt; indicates one dimension parameter &lt;code&gt;d0&lt;/code&gt; and one symbol parameter &lt;code&gt;s0&lt;/code&gt;. Additionally, affine maps can only contain addition, subtraction, multiplication by constants, division by constants, modulo by constants, and floordiv/ceildiv by constants. The result type is always &lt;code&gt;index&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;affine-optimization-passes&quot; tabindex=&quot;-1&quot;&gt;Affine Optimization Passes&lt;/h2&gt;
&lt;p&gt;The MLIR optimizer provides a rich set of passes specifically designed for the affine dialect. These passes can automatically transform code to improve performance through various optimization techniques. Let&#39;s explore some of the most important ones:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Loop invariant code motion&lt;/strong&gt;: This transformation moves loop invariant code (code that does not depend on the loop index) outside the loop. This can help expose parallelism by moving code that is not dependent on the loop index to the outer loop.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;42.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// This is loop invariant!&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@after&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
  
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;42.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Moved outside the loop&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Loop skewing&lt;/strong&gt;: This transformation changes the iteration space by shifting iterations based on the values of other loop indices. This can help expose parallelism by transforming dependencies.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before - Dependencies prevent parallelization&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After skewing - Inner loop can now be parallelized&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@after&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i_new &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j_new &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.apply&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d1 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; d0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i_new&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j_new&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j_new&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j_new&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;x10xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Loop interchange&lt;/strong&gt;: This transformation changes the nesting order of loops, which can improve cache locality and expose parallelism.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before - Column-major access pattern&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After - Row-major access pattern (better locality)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@after&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Loop fusion&lt;/strong&gt;: This transformation combines adjacent loops to improve locality and reduce loop overhead.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before fusion&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v4 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v3 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After fusion&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@after&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v2 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Loop tiling&lt;/strong&gt;: This transformation breaks a loop into smaller chunks to improve cache locality.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before tiling&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@before&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token comment&quot;&gt;// ... computation ...&lt;/span&gt;
      affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After tiling (tile size 8x8)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@after&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ti &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tj &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt; step &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ti &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tj&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token attribute&quot;&gt;#map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;tj &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
          &lt;span class=&quot;token comment&quot;&gt;// ... computation ...&lt;/span&gt;
          affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;x32xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These optimizations are available in the &lt;code&gt;mlir-opt&lt;/code&gt; tool:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-coalescing&lt;/code&gt; - Merge consecutive loops into a single loop.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-fusion&lt;/code&gt; - Merge loops with the same bounds and steps.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-invariant-code-motion&lt;/code&gt; - Move loop invariant code outside the loop.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-normalize&lt;/code&gt; - Normalize loop bounds and steps.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-tile&lt;/code&gt; - Tile loops.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-unroll&lt;/code&gt; - Unroll loops.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-loop-unroll-jam&lt;/code&gt; - Unroll loops and jam them together.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-parallelize&lt;/code&gt; - Parallelize loops.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-pipeline-data-transfer&lt;/code&gt; - Pipeline non-blocking data transfers between explicitly managed levels of the memory hierarchy&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-scalrep&lt;/code&gt; - Replace affine memref accesses by scalars by forwarding stores to loads and eliminating redundant loads&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-simplify-structures&lt;/code&gt; - Simplify affine expressions in maps/sets and normalize memrefs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-affine-super-vectorize&lt;/code&gt; - Vectorize to a target independent n-D vector abstraction&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is also the &lt;code&gt;-convert-affine-for-to-gpu&lt;/code&gt;, which we&#39;ll use later, which converts affine loops to GPU kernels which can target multiple GPU architectures.&lt;/p&gt;
&lt;h2 id=&quot;convolution-kernels&quot; tabindex=&quot;-1&quot;&gt;Convolution Kernels&lt;/h2&gt;
&lt;p&gt;Now that we understand the basic optimizations available in the affine dialect, let&#39;s look at a more practical example: implementing a 2D convolution. This operation is fundamental in image processing and deep learning, where it&#39;s used for feature extraction and pattern recognition.&lt;/p&gt;
&lt;p&gt;The mathematical formula for 2D convolution is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(A * K)_{ij} = &#92;sum_{u} &#92;sum_{v} K_{uv} A_{(i+u)(j+v)}&lt;br /&gt;
$$&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@conv_2d&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;x128xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;113&lt;/span&gt;x113xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Loop over the output matrix dimensions (113x113)&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;113&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;113&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// Use affine.parallel to accumulate values into %acc using iter_args&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;zero &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fi &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; iter_args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;zero&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc_inner &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fj &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; iter_args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc_inner &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;// Load filter value&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;filter_val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fj&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;x16xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;// Load corresponding input value from the input matrix&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; affine.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fj&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;x128xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;// Multiply input value with filter value&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;filter_val &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;// Add product to the accumulator&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;new_acc &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc_inner&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;prod &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
            &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;new_acc &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
          &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;affine.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc_inner &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;// Store the accumulated result in the output matrix&lt;/span&gt;
        affine.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;113&lt;/span&gt;x113xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To properly optimize and parallelize affine operations, we need a specific sequence of passes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-opt conv_2d.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --affine-loop-normalize &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --affine-parallelize &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -lower-affine &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-scf-to-cf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-cf-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-math-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-arith-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --finalize-memref-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-func-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --reconcile-unrealized-casts &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; conv_2d_opt.mlir

&lt;span class=&quot;token comment&quot;&gt;# Translate to LLVM IR&lt;/span&gt;
mlir-translate conv_2d_opt.mlir -mlir-to-llvmir &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; conv_2d.ll

&lt;span class=&quot;token comment&quot;&gt;# Compile to object file&lt;/span&gt;
llc &lt;span class=&quot;token parameter variable&quot;&gt;-filetype&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;obj --relocation-model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;pic conv_2d.ll &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; conv_2d.o

&lt;span class=&quot;token comment&quot;&gt;# Compile to shared library&lt;/span&gt;
clang &lt;span class=&quot;token parameter variable&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fopenmp&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; libconv2d.dylib conv_2d.o
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the &lt;code&gt;-lower-affine&lt;/code&gt; pass lowers the affine dialect to combinations of the &lt;code&gt;scf&lt;/code&gt; and &lt;code&gt;arith&lt;/code&gt; dialects.&lt;/p&gt;
&lt;p&gt;To use our parallelized code from Python, we&#39;ve created a helper module to handle 2D array conversions between NumPy and MLIR MemRef descriptors. The module provides utilities for converting NumPy arrays and running the compiled MLIR code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ctypes &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Structure


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MemRef2DDescriptor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Structure&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Structure matching MLIR&#39;s 2D MemRef descriptor&quot;&quot;&quot;&lt;/span&gt;

    _fields_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allocated&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Allocated pointer&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;aligned&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Aligned pointer&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;offset&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Offset in elements&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;shape&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Array shape (2D)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;stride&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Strides in elements&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;numpy_to_memref2d&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Convert a 2D NumPy array to a MemRef descriptor&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flags&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;C_CONTIGUOUS&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        arr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ascontiguousarray&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MemRef2DDescriptor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;allocated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;aligned &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;allocated
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;offset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stride&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strides&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;itemsize
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stride&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strides&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;itemsize

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; desc


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_conv2d&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Run 2D convolution using MLIR compiled module&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Create input arrays&lt;/span&gt;
    input_matrix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    conv_filter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;arange&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Load compiled module&lt;/span&gt;
    module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CDLL&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./libconv2d.dylib&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare MemRef descriptors&lt;/span&gt;
    input_memref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref2d&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_matrix&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    filter_memref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref2d&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;conv_filter&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    result_memref &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref2d&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Set function argument types&lt;/span&gt;
    module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;conv_2d&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argtypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; input_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; filter_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; result_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Call the function&lt;/span&gt;
    module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;conv_2d&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_memref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; field &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; input_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;filter_memref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; field &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; filter_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result_memref&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; field &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; result_memref&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_fields_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; result


&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; run_conv2d&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;openmp&quot; tabindex=&quot;-1&quot;&gt;OpenMP&lt;/h2&gt;
&lt;p&gt;While the affine dialect provides abstractions for parallel execution, sometimes we want more direct control over parallelization. This is where OpenMP comes in. OpenMP is a directive-based model that provides explicit control over parallel execution, making it a popular choice in high-performance computing.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at a simple example in C that demonstrates OpenMP parallelization:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdlib.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;stdio.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;omp.h&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;omp parallel&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;pragma&lt;/span&gt; &lt;span class=&quot;token expression&quot;&gt;omp &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%f &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When compiled with OpenMP support (&lt;code&gt;clang -fopenmp&lt;/code&gt;), this program will parallelize the loop across multiple threads. The same computation can be expressed in MLIR with the OpenMP dialect:&lt;/p&gt;
&lt;h3 id=&quot;parallel-regions-(omp.parallel)&quot; tabindex=&quot;-1&quot;&gt;Parallel Regions (omp.parallel)&lt;/h3&gt;
&lt;p&gt;OpenMP organizes parallel execution through the concept of parallel regions. In MLIR, these are represented by the &lt;code&gt;omp.parallel&lt;/code&gt; construct, which creates a team of threads that can execute code concurrently. This provides a more explicit way to control parallelization compared to the automatic parallelization we saw with the affine dialect.&lt;/p&gt;
&lt;p&gt;The region is terminated with an &lt;code&gt;omp.terminator&lt;/code&gt; operation, which is required for all OpenMP regions.&lt;/p&gt;
&lt;h3 id=&quot;worksharing-loops-(omp.wsloop)&quot; tabindex=&quot;-1&quot;&gt;Worksharing Loops (omp.wsloop)&lt;/h3&gt;
&lt;p&gt;For parallel loops, the OpenMP dialect provides the &lt;code&gt;omp.wsloop&lt;/code&gt; operation, which specifies that loop iterations will be executed in parallel by threads in the current context. Worksharing loops are typically used within parallel regions to distribute loop iterations across available threads:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;omp.wsloop&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.loop_nest&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c10&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; step &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arrA&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arrB&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arrC&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.yield&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This example shows a nested loop being parallelized to add two matrices. The &lt;code&gt;omp.loop_nest&lt;/code&gt; operation inside the &lt;code&gt;omp.wsloop&lt;/code&gt; represents the nested loop structure with iteration variables, bounds, and steps.&lt;/p&gt;
&lt;h3 id=&quot;synchronization-with-barriers-(omp.barrier)&quot; tabindex=&quot;-1&quot;&gt;Synchronization with Barriers (omp.barrier)&lt;/h3&gt;
&lt;p&gt;For thread synchronization, the OpenMP dialect provides the &lt;code&gt;omp.barrier&lt;/code&gt; operation, which specifies an explicit barrier at the point where it appears. This ensures that all threads in a team reach the barrier before any thread proceeds:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Code executed by all threads&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;omp.barrier&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Code executed after all threads reach the barrier&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;omp.barrier&lt;/code&gt; operation is a simple operation without inputs or outputs, and its assembly format just includes the name and any attributes.&lt;/p&gt;
&lt;h3 id=&quot;omp-dialect&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;omp&lt;/code&gt; Dialect&lt;/h3&gt;
&lt;p&gt;Let&#39;s translate the above C code of a parallel loop to MLIR:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;

  &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.parallel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.wsloop&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.loop_nest&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_lb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_ub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; inclusive step &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.index_cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;two &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;two &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
          &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.yield&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.barrier&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.terminator&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we can have a main function that calls the kernel over a static defined input:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.global constant &lt;span class=&quot;token function&quot;&gt;@input&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; dense&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define external references to printf and kernel&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.get_global &lt;span class=&quot;token function&quot;&gt;@input&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

  func.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; 
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.addressof&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;

  &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; vararg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.func&amp;lt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define a constant string for the format specifier&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.global&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; constant &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%f&#92;0A&#92;00&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;addr_space &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As an aside, variadic functions in MLIR allow for flexible argument passing, similar to C. A common pattern we&#39;ll use is calling functions like &lt;code&gt;printf&lt;/code&gt;, which can take a variable number of arguments. In the main function, we&#39;re calling the &lt;code&gt;printf&lt;/code&gt; function (which is variadic) to print the result. The following is equivalent to &lt;code&gt;printf(&amp;quot;%f&#92;n&amp;quot;, el)&lt;/code&gt; in C, demonstrating how to use &lt;code&gt;printf&lt;/code&gt; in MLIR.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Define external printf function from stdio.h&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define a constant string for the format specifier&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.global&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; constant &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%f&#92;0A&#92;00&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;addr_space &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Call the variadic printf function with the format specifier and type specialized arguments&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.addressof&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
llvm.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; vararg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.func&amp;lt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s the full example with the parallel loop and the main function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.global constant &lt;span class=&quot;token function&quot;&gt;@input&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; dense&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.global&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; constant &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;%f&#92;0A&#92;00&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;addr_space &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;

  &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.parallel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.wsloop&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.loop_nest&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_lb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_ub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; inclusive step &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;loop_step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.index_cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;two &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
          &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input_val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;two &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
          &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ix&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
          &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.yield&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.barrier&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.terminator&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.get_global &lt;span class=&quot;token function&quot;&gt;@input&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@kernel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; 
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.addressof&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@fmt&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;

  &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@printf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;fs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;el&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; vararg&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.func&amp;lt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ...&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;scf-to-openmp-conversion-pass&quot; tabindex=&quot;-1&quot;&gt;SCF to OpenMP Conversion Pass&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;-convert-scf-to-openmp&lt;/code&gt; pass automatically converts parallel loops from the SCF dialect into equivalent OpenMP operations. This pass is particularly useful when you want to target OpenMP execution without manually writing OpenMP dialect code.&lt;/p&gt;
&lt;p&gt;For example, an SCF parallel loop like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;scf.parallel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c100&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; step &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.yield&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Will be converted to OpenMP operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;omp.parallel&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.wsloop&lt;/span&gt; for &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; to &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c100&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; step &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;val &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.yield&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;omp.terminator&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pass handles several important conversions, including the transformation of &lt;code&gt;scf.parallel&lt;/code&gt; operations into &lt;code&gt;omp.parallel&lt;/code&gt; regions that contain &lt;code&gt;omp.wsloop&lt;/code&gt;, while preserving loop bounds and step sizes. Additionally, reduction operations in SCF are mapped to OpenMP reductions, and nested parallel loops are properly managed. This conversion is typically utilized as part of a larger lowering pipeline, which is often followed by &lt;code&gt;-convert-openmp-to-llvm&lt;/code&gt; to generate LLVM IR that can be executed with OpenMP runtime support.&lt;/p&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/Affine/&quot;&gt;MLIR: Affine Dialect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/Affine/#polyhedral-structures&quot;&gt;MLIR: Polyhedral Structures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/OpenMP/&quot;&gt;MLIR: OpenMP Dialect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://discourse.llvm.org/t/rfc-openmp-dialect-in-mlir/397&quot;&gt;RFC: OpenMP dialect in MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.openmp.org/wp-content/uploads/openmp_flang.pdf&quot;&gt;OpenMP: OpenMP 5.0 Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://llvm.org/devmtg/2024-04/slides/QuickTalks/Martinez-ParallelizingApplicationsWithMLIR.pdf&quot;&gt;Parallelizing Applications with MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hps.vi4io.org/_media/events/2020/llvm-cth20_chandramohan.pdf&quot;&gt;OpenMP in Flang using MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td&quot;&gt;OpenMPOps.td&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/doxygen/OpenMPToLLVMIRTranslation_8cpp.html&quot;&gt;OpenMPToLLVMIRTranslation.cpp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://easychair.org/publications/preprint/Kp3L&quot;&gt;Extending Polygeist to Generate OpenMP SIMD and GPU MLIR Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://tildesites.bowdoin.edu/~ltoma/teaching/cs3225-GIS/fall17/Lectures/openmp.html&quot;&gt;OpenMP in a Nutshell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://users.cs.utah.edu/~mhall/mlir4hpc/pienaar-MLIR-Tutorial.pdf&quot;&gt;MLIR Tutorial: Building a Compiler with MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=hIt6J1_E21c&quot;&gt;2023 EuroLLVM - MLIR Dialect Design and Composition for Front-End Compilers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://reviews.llvm.org/D119743&quot;&gt;Add conversion from SCF parallel loops to OpenMP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lOK6JPuVFYg&quot;&gt;Open MLIR Meeting 2-15-2024: OpenMP GPU Target Offloading&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://reviews.llvm.org/D119743&quot;&gt;Mark parallel regions as allocation scopes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Thu, 13 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_affine/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_affine/</guid>
    </item>
    
    <item>
      <title>MLIR Part 2 - Memory in MLIR</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/memory.jpeg&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;memory-in-mlir&quot; tabindex=&quot;-1&quot;&gt;Memory in MLIR&lt;/h1&gt;
&lt;p&gt;MLIR&#39;s memory architecture represents a balance between high-level abstractions and low-level efficiency. At its core, MLIR provides three primary ways to represent data: &lt;strong&gt;Tensors&lt;/strong&gt; for immutable, abstract operations; &lt;strong&gt;MemRefs&lt;/strong&gt; for concrete memory buffers; and LLVM-level constructs for fine-grained control. This three-layer approach allows developers to work at their preferred level of abstraction while maintaining the ability to precisely control memory when needed.&lt;/p&gt;
&lt;p&gt;While Tensors enable clean, mathematical expressions of computations, MemRefs provide a bridge to actual memory operations, and LLVM constructs offer direct control over memory layout and access patterns. This design is based on MLIR&#39;s philosophy of progressive lowering, where high-level concepts can be systematically transformed into more concrete representations as needed for optimization and execution.&lt;/p&gt;
&lt;p&gt;In short, Tensors are pure mathematical objects, MemRefs are concrete memory buffers, and LLVM constructs are low-level memory operations, and vectors are refrences to multiple register values that we can manipulate simultaneously. Let&#39;s start with the highest level abstraction and work our way down.&lt;/p&gt;
&lt;h2 id=&quot;tensors&quot; tabindex=&quot;-1&quot;&gt;Tensors&lt;/h2&gt;
&lt;p&gt;Tensors in MLIR are immutable, high-level data structures without explicit memory locations. They represent structured data in a computation-oriented manner desigend for use in matrix-style computations often found in machine learning and linear algebra.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// 4-D static tensor with 10x10x10x10 elements of type f32&lt;/span&gt;
&lt;span class=&quot;token type&quot;&gt;!TensorType1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// 4-D dynamic tensor with N x M x 10 x 10 elements of type f32&lt;/span&gt;
&lt;span class=&quot;token type&quot;&gt;!TensorType2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// dummy example constants&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;X &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;42.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// allocate a tensor with the shape of the last 2 dimensions&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// allocate a zero tensor with the shape of the last 2 dimensions&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;B &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// create a tensor with the given values&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.from_elements &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;X  &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Common tensor operations&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;D &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.extract &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!TensorType2&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;E &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.insert &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;X into &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;N&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!TensorType2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tensor operations facilitate mathematical transformations in a clean, side-effect-free manner that makes analysis and optimization straightforward. For example, the identity matrix can be represented as:&lt;/p&gt;
&lt;p&gt;&#92;[&lt;br /&gt;
A_{ij} = &#92;begin{cases}&lt;br /&gt;
1 &amp;amp; &#92;text{if } i = j &#92;&#92;&lt;br /&gt;
0 &amp;amp; &#92;text{otherwise}&lt;br /&gt;
&#92;end{cases}&lt;br /&gt;
&#92;]&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@identity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;m &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;n &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.generate &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;m &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ni &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.index_cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;nj &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.index_cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;elem &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.cmpi eq&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ni&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;nj &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;elem_i32 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.extui &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;elem &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.yield &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;elem_i32 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;xi32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;memrefs&quot; tabindex=&quot;-1&quot;&gt;MemRefs&lt;/h2&gt;
&lt;p&gt;MemRefs are MLIR&#39;s primary abstraction for representing memory buffers. They serve as a bridge between high-level array concepts (like NumPy arrays) and low-level memory operations. A MemRef essentially represents a pointer to a region of memory with additional metadata about its structure.&lt;/p&gt;
&lt;p&gt;A MemRef consists of several core components, these are divided into &lt;em&gt;metadata&lt;/em&gt; and &lt;em&gt;data&lt;/em&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Allocated Pointer&lt;/strong&gt;: Points to the data buffer (used for deallocation)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aligned Pointer&lt;/strong&gt;: Points to properly aligned data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Offset&lt;/strong&gt;: Distance (in elements) between aligned pointer start and first accessible element&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shape&lt;/strong&gt;: Array of integers describing dimensions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strides&lt;/strong&gt;: Array of integers describing memory layout&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In MLIR, a MemRef type is declared using syntax like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 1D array of 1024 float32 elements&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;x4xf64&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// 2D array of 4x4 float64 elements&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;creating-a-simple-array-addition-kernel&quot; tabindex=&quot;-1&quot;&gt;Creating a Simple Array Addition Kernel&lt;/h2&gt;
&lt;p&gt;Let&#39;s create a simple example that adds two arrays using MLIR MemRefs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@array_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
      attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.emit_c_interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1024 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    
    &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c0 to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1024 step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c1 &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to compile the above MLIR code to an executable we can use the following commands:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;mlir-opt array_add.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -finalize-memref-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -convert-scf-to-cf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -convert-cf-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -convert-arith-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -convert-func-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; array_add_opt.mlir

mlir-translate array_add_opt.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  -mlir-to-llvmir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; array_add_opt.ll

llc &lt;span class=&quot;token parameter variable&quot;&gt;-filetype&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;obj --relocation-model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;pic arary_add.ll &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; arary_add.o
clang &lt;span class=&quot;token parameter variable&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fPIC&lt;/span&gt; arary_add.o &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; array_add.so
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;numpy-array-memory-layout&quot; tabindex=&quot;-1&quot;&gt;NumPy Array Memory Layout&lt;/h2&gt;
&lt;p&gt;Before we integrate with Python, it&#39;s important to understand how NumPy arrays are laid out in memory:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Data Buffer&lt;/strong&gt;: Contiguous memory block holding the actual values&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shape&lt;/strong&gt;: Tuple describing array dimensions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strides&lt;/strong&gt;: Tuple describing steps (in bytes) to move in each dimension&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dtype&lt;/strong&gt;: Data type of elements&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When you allocate a NumPy array it essentially creates a PyObject wrapper around a raw memory buffer with metadata about the shape, strides, and dtype of the array. We can move this memory back and forth between Python and the JIT compiled LLVM logic without copying the data.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np

a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# (2, 3)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strides&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# (12, 4)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# float32&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;itemsize&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# 4&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# 0x7f926d6e2b20 (base pointer to the memory buffer)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Get a ctypes pointer&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_float&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By default numpy arrays are C-contiguous, which means that the memory is laid out in a row-major order. This is the most common memory layout and is the default for most NumPy operations. To calculate the memory address of the &lt;code&gt;[i, j]&lt;/code&gt; element in a matrix we can use the following formula:&lt;/p&gt;
&lt;p&gt;&#92;[&lt;br /&gt;
&#92;text{address} = &#92;text{base_address} + (i &#92;times &#92;text{strides}[0] + j &#92;times &#92;text{strides}[1])&lt;br /&gt;
&#92;]&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/array.png&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/center&gt;
&lt;p&gt;For example, a row-major strided layout for &lt;code&gt;memref&amp;lt;3x4xf32&amp;gt;&lt;/code&gt; is the same as both the following fully expanded memrefs (which are identical):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strided&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; affine_map&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;converting-numpy-arrays-to-memrefs&quot; tabindex=&quot;-1&quot;&gt;Converting NumPy Arrays to MemRefs&lt;/h3&gt;
&lt;p&gt;To use NumPy arrays with our MLIR code, we need to create a bridge between NumPy&#39;s memory layout and MLIR&#39;s MemRef structure. We can do this by creating a Python class that matches MLIR&#39;s MemRef descriptor layout:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ctypes &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Structure


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MemRefDescriptor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Structure&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Structure matching MLIR&#39;s MemRef descriptor&quot;&quot;&quot;&lt;/span&gt;

    _fields_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allocated&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Allocated pointer&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;aligned&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Aligned pointer&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;offset&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Offset in elements&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;shape&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Array shape (1D in this case)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;stride&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_longlong &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Strides in elements&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;numpy_to_memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Convert a NumPy array to a MemRef descriptor&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flags&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;C_CONTIGUOUS&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        arr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ascontiguousarray&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;allocated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data_as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c_void_p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;aligned &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;allocated
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;offset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; arr&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    desc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stride&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; desc
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;MemRefDescriptor&lt;/code&gt; class mirrors the memory layout that MLIR expects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;allocated&lt;/code&gt;: Points to the start of the allocated memory&lt;/li&gt;
&lt;li&gt;&lt;code&gt;aligned&lt;/code&gt;: Points to aligned memory (often same as allocated)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;offset&lt;/code&gt;: Number of elements to offset from the start&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shape&lt;/code&gt;: Array dimensions (1D array in our example)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stride&lt;/code&gt;: Steps between elements in each dimension&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;numpy_to_memref&lt;/code&gt; function handles the conversion by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensuring the array is contiguous in memory&lt;/li&gt;
&lt;li&gt;Creating a MemRef descriptor&lt;/li&gt;
&lt;li&gt;Setting up the pointers to the NumPy array&#39;s data&lt;/li&gt;
&lt;li&gt;Configuring the shape and stride information&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This conversion allows us to pass NumPy arrays to MLIR functions without copying the underlying data. Let&#39;s see how to use this in practice with our array addition example.&lt;/p&gt;
&lt;h2 id=&quot;integrating-mlir-with-python&quot; tabindex=&quot;-1&quot;&gt;Integrating MLIR with Python&lt;/h2&gt;
&lt;p&gt;There are two main approaches to using MLIR code from Python: compiling to a shared library and loading it, or JIT-compiling the MLIR code at runtime. Let&#39;s explore both approaches using our array addition example.&lt;/p&gt;
&lt;h3 id=&quot;loading-pre-compiled-mlir-code&quot; tabindex=&quot;-1&quot;&gt;Loading Pre-compiled MLIR Code&lt;/h3&gt;
&lt;p&gt;The first approach is to compile our MLIR code into a shared library that can be loaded from Python using ctypes. This requires compiling the MLIR code ahead of time:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Compile MLIR to shared library&lt;/span&gt;
mlir-opt array_add.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-tensor-to-linalg &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-linalg-to-loops &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-scf-to-cf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-cf-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-math-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-arith-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-func-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-index-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --finalize-memref-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --reconcile-unrealized-casts &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; array_add_opt.mlir

mlir-translate --mlir-to-llvmir array_add_opt.mlir &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; array_add.ll
llc &lt;span class=&quot;token parameter variable&quot;&gt;-filetype&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;obj array_add.ll &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; array_add.o
clang &lt;span class=&quot;token parameter variable&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fPIC&lt;/span&gt; array_add.o &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; libarray_add.dylib
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can load and use the compiled library from Python:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; np_memref &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numpy_to_memref


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Load the shared library&lt;/span&gt;
    lib &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CDLL&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./libarray_add.dylib&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get the C interface function&lt;/span&gt;
    array_add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; lib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_mlir_ciface_array_add
    array_add&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argtypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# input1, input2, output&lt;/span&gt;
    array_add&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;restype &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create sample input arrays&lt;/span&gt;
    size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
    c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert arrays to MemRef descriptors&lt;/span&gt;
    a_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    c_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Call the function&lt;/span&gt;
    array_add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a_desc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b_desc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;byref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c_desc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Verify results&lt;/span&gt;
    expected &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
    np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;assert_array_almost_equal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expected&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Array addition successful!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;First few elements: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;5]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Should show [3.0, 3.0, 3.0, 3.0, 3.0]&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach is simpler and more suitable for production use since the compilation is done ahead of time. The compiled library can be distributed with your Python package and loaded at runtime.&lt;/p&gt;
&lt;h3 id=&quot;jit-compiling-mlir-at-runtime&quot; tabindex=&quot;-1&quot;&gt;JIT Compiling MLIR at Runtime&lt;/h3&gt;
&lt;p&gt;The second approach is to JIT-compile the MLIR code at runtime using &lt;code&gt;llvmlite&lt;/code&gt; (Python bindings for LLVM). This gives more flexibility since the MLIR code can be generated or modified at runtime:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; os
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; subprocess
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ctypes &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; CFUNCTYPE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; POINTER
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; np_memref &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numpy_to_memref

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite

&lt;span class=&quot;token comment&quot;&gt;# Disable typed pointers for LLVM 15+ compatibility&lt;/span&gt;
llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;opaque_pointers_enabled &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; llvmlite&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;binding &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; llvm  &lt;span class=&quot;token comment&quot;&gt;# noqa: E402&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize LLVM&lt;/span&gt;
llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_native_target&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;initialize_native_asmprinter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_mlir_to_llvm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_file_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Compile MLIR code to LLVM IR using mlir-opt and mlir-translate&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Run mlir-opt to lower to LLVM dialect&lt;/span&gt;
    opt_cmd &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;mlir-opt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        mlir_file_path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-tensor-to-linalg&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-linalg-to-loops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-scf-to-cf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-cf-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-math-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-arith-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-func-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--convert-index-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--finalize-memref-to-llvm&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;--reconcile-unrealized-casts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        opt_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;opt_cmd&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; capture_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; text&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; check&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CalledProcessError &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Error running mlir-opt:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;STDOUT:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;STDERR:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stderr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Run mlir-translate to convert to LLVM IR&lt;/span&gt;
    translate_cmd &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mlir-translate&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;--mlir-to-llvmir&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        translate_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            translate_cmd&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;opt_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            capture_output&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            text&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            check&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; subprocess&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CalledProcessError &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Error running mlir-translate:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;STDOUT:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;STDERR:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stderr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; translate_result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stdout


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_execution_engine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Create an ExecutionEngine suitable for JIT&quot;&quot;&quot;&lt;/span&gt;
    target &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_default_triple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    target_machine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_target_machine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    backing_mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_mcjit_compiler&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;backing_mod&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_machine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; engine


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compile_and_load_mlir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_file_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Compile MLIR code and load it into a JIT engine&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Convert MLIR to LLVM IR&lt;/span&gt;
    llvm_ir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; compile_mlir_to_llvm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_file_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create module from LLVM IR&lt;/span&gt;
    mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llvm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parse_assembly&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;llvm_ir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    mod&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;verify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create execution engine and add module&lt;/span&gt;
    engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; create_execution_engine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_module&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;finalize_object&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Get path to MLIR file&lt;/span&gt;
    current_dir &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dirname&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;abspath&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;__file__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    mlir_file &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_dir&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;array_add.mlir&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Compile and load the MLIR code&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; compile_and_load_mlir&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mlir_file&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get function pointer to the compiled function&lt;/span&gt;
    func_ptr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_function_address&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;_mlir_ciface_array_add&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create the ctypes function wrapper&lt;/span&gt;
    array_add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; CFUNCTYPE&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        POINTER&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MemRefDescriptor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;func_ptr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create test arrays&lt;/span&gt;
    size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
    c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert arrays to MemRef descriptors&lt;/span&gt;
    a_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    c_desc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numpy_to_memref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Call the JIT-compiled function&lt;/span&gt;
    array_add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a_desc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b_desc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c_desc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Verify results&lt;/span&gt;
    expected &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
    np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;testing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;assert_array_almost_equal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expected&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Array addition successful!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;First few elements: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;5]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Should show [3.0, 3.0, 3.0, 3.0, 3.0]&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The JIT compilation approach offers several advantages. First, MLIR code can be generated or modified at runtime. Second, there is no need to manage separate compiled libraries. Finally, it allows for optimization for the specific CPU architecture at runtime. However, this approach also has some drawbacks. There is compilation overhead at startup, a more complex setup with dependencies on MLIR tools, and the need to handle compilation errors at runtime.&lt;/p&gt;
&lt;h2 id=&quot;low-level-llvm-structures&quot; tabindex=&quot;-1&quot;&gt;Low-level LLVM Structures&lt;/h2&gt;
&lt;p&gt;In addition to the higher-level MLIR constructs, MLIR also provides a low-level LLVM dialect that allows for more direct control over memory layout and operations. Structs from LLVM dialect can be used to create custom types in MLIR. For example in C we would write:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Pair&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In MLIR we can define the same struct as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token type&quot;&gt;!Pair&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(i32,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.zero&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!Pair&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates a new type alias &lt;code&gt;!Pair&lt;/code&gt; which is a struct with two fields: an &lt;code&gt;i32&lt;/code&gt; and a &lt;code&gt;f32&lt;/code&gt;. The &lt;code&gt;%1&lt;/code&gt; variable is then an instance of this struct with all fields initialized to zero.&lt;/p&gt;
&lt;p&gt;LLVM arrays provide fixed-size sequential storage of elements. They can be created using the following syntax:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Array of 10 integers&lt;/span&gt;
&lt;span class=&quot;token type&quot;&gt;!IntArray&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.array&amp;lt;10&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Array of 4 pairs (using the struct defined above)&lt;/span&gt;
&lt;span class=&quot;token type&quot;&gt;!PairArray&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.array&amp;lt;4&lt;/span&gt; x &lt;span class=&quot;token type&quot;&gt;!Pair&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Create and initialize an array&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arr &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dense&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xi32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.array&amp;lt;4&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Access array elements&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;element &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.array&amp;lt;4&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Update array elements&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;new_arr &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;element&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arr&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.array&amp;lt;4&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unlike higher-level MLIR constructs like MemRefs, LLVM arrays are more rigid and closely mirror the behavior of arrays in C. They have a fixed size known at compile time and don&#39;t carry any metadata about their structure.&lt;/p&gt;
&lt;h2 id=&quot;vectors&quot; tabindex=&quot;-1&quot;&gt;Vectors&lt;/h2&gt;
&lt;p&gt;MLIR&#39;s vector dialect provides a low-level abstraction for SIMD (Single Instruction, Multiple Data) operations. While it&#39;s closely aligned with hardware vector capabilities, it&#39;s not strictly limited to what the target architecture supports, thanks to LLVM&#39;s vector operation lowering and optimization capabilities.&lt;/p&gt;
&lt;p&gt;Vectors in MLIR can be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Single-dimensional: representing traditional SIMD registers (usually operating over 2, 4, 8 elements)&lt;/li&gt;
&lt;li&gt;Multi-dimensional: representing higher-order vector operations&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Single-dimensional vectors&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;// 4-element vector of float32&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xi32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;// 8-element vector of int32&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Multi-dimensional vectors&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// 2x4 matrix of float32 as vectors&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;x2x4xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 3D vector arrangement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;MLIR provides vector operations that map efficiently to hardware instructions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Vector arithmetic&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;.add &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;d &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;.mul &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Vector shuffles and extracts&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;e &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;.extract &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;f &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;.shuffle &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;d &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Vector fold/reduction operations&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;.reduction &lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; into &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While MLIR&#39;s vector dialect provides operations that may not directly correspond to hardware capabilities, LLVM handles the lowering process by directly mapping to hardware vectors (e.g., AVX, NEON), splitting into smaller vectors, and scalarization when necessary.&lt;/p&gt;
&lt;p&gt;For example, a &lt;code&gt;vector&amp;lt;8xf32&amp;gt;&lt;/code&gt; operation might be mapped directly to AVX on x86, split into two NEON operations on ARM, or scalarized on architectures without vector support.&lt;/p&gt;
&lt;h2 id=&quot;bufferization&quot; tabindex=&quot;-1&quot;&gt;Bufferization&lt;/h2&gt;
&lt;p&gt;Bufferization in MLIR is the process of converting ops with tensor semantics to ops with memref semantics. The process of converting computations on the mathematical tensor construct to computations on physical memory buffers.&lt;/p&gt;
&lt;p&gt;There are two low-level operations that are used to convert tensors to memrefs. They do what they say on the tin.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bufferization.to_memref&lt;/code&gt; - Convert a tensor to a memref&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bufferization.to_tensor&lt;/code&gt; - Convert a memref to a tensor&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out1 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;bufferization.to_memref&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out2 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;bufferization.to_tensor&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;bufferization.materialize_in_destination&lt;/code&gt; operation is used to convert a tensor to a memref in the destination buffer. This is equivalent to converting the tensor to a memref and then storing the result in the destination buffer but without the intermediate step. This is often the final step of a kernel that uses an externally allocated buffer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;bufferization.materialize_in_destination&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; in restrict writable &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;C &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now there are also passes that can do this conversion automatically. The &lt;code&gt;one-shot-bufferize&lt;/code&gt; pass is a pass that can be used to convert all tensor operations to memref operations. Importantly this will not by default bufferize function boundaries (i.e. the input and output of a function are not bufferized), so we need to add the &lt;code&gt;bufferize-function-boundaries&lt;/code&gt; flag to the pass.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-opt -one-shot-bufferize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bufferize-function-boundaries&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A simple example demonstrates the transformation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Before bufferization&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;f &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;5.000000&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;00&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;.insert &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;f into &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// After bufferization&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;f &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;5.000000&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;00&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, the tensor insert operation was able to be bufferized in-place, reusing the input buffer for the output.&lt;/p&gt;
&lt;h2 id=&quot;c-compatible-wrappers&quot; tabindex=&quot;-1&quot;&gt;C-compatible wrappers&lt;/h2&gt;
&lt;p&gt;When we want to call a function from C, we need to emit a C-compatible wrapper. This is done using the &lt;code&gt;llvm.emit_c_interface&lt;/code&gt; attribute. Normally when we bufferize a function we unroll all the memref fields into a single arguments. This is not desirable when we want to call the function from C where we would typically pass the memrefs as void pointers to structs.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@add_vector_to_matrix&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.emit_c_interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;A &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;memref&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;xf32&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will emit a C-compatible wrapper for the function which is expanded into two functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@fun&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.emit_c_interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.poison&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.insertvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@_mlir_ciface_fun&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr)&lt;/span&gt; attributes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.emit_c_interface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; llvm.&lt;span class=&quot;token keyword&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.extractvalue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; llvm.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@fun&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;store&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.struct&amp;lt;(ptr,&lt;/span&gt; ptr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token type&quot;&gt;!llvm.ptr&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead now we call the &lt;code&gt;@_mlir_ciface_fun&lt;/code&gt; function which is the C-compatible wrapper for the &lt;code&gt;@fun&lt;/code&gt; function.  Notice how it&#39;s given use two arguments, one for the input (&lt;code&gt;%arg0&lt;/code&gt;) and one for the output (&lt;code&gt;%arg1&lt;/code&gt;). Now we can pass our own memory buffers to the function and it will write the result back to our buffer with the code generation extracting the metadata from the memref.&lt;/p&gt;
&lt;h2 id=&quot;external-references&quot; tabindex=&quot;-1&quot;&gt;External References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Bufferization/&quot;&gt;MLIR Bufferization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/OpenMeetings/2022-01-13-One-Shot-Bufferization.pdf&quot;&gt;One-Shot Function Bufferization of Tensor Programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=TXEo59CYS9A&quot;&gt;MLIR Open Meeting 2022-01-13: One-Shot Function Bufferization of Tensor Programs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://m-sp.org/downloads/llvm_dev_2023.pdf&quot;&gt;MLIR Bufferization: From Tensors to MemRefs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.lewuathe.com/2021-04-23-how-to-init-memref-in-mlir/&quot;&gt;How to init MemRef in MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.tenstorrent.com/tt-mlir/specs/tensor-layout.html&quot;&gt;Tensor Layout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/TensorOps/&quot;&gt;Tensor Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.jeremykun.com/2023/10/23/mlir-dialect-conversion/&quot;&gt;MLIR Dialect Conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://numpy.org/devdocs/reference/c-api/data_memory.html&quot;&gt;NumPy Memory Layout&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Wed, 12 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_memory/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_memory/</guid>
    </item>
    
    <item>
      <title>MLIR Part 1 - Introduction to MLIR</title>
      <description>&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/mlir.jpeg&quot; width=&quot;500px&quot; alt=&quot;mlir-egglog&quot; /&gt;
&lt;/p&gt;
&lt;h1 id=&quot;introduction-to-mlir&quot; tabindex=&quot;-1&quot;&gt;Introduction to MLIR&lt;/h1&gt;
&lt;p&gt;LLVM is a powerful compiler infrastructure project that has revolutionized how we build programming languages and tools. At its core, LLVM provides a collection of modular compiler and toolchain technologies that can be used to develop frontend compilers for any programming language and generate optimized code for many target architectures.&lt;/p&gt;
&lt;p&gt;MLIR is a newer project within the LLVM ecosystem, designed to address the challenges of modern hardware accelerators and machine learning frameworks. While LLVM IR is powerful, it operates at a relatively low level. MLIR extends these concepts to support multiple levels of abstraction.&lt;/p&gt;
&lt;p&gt;Unfortunately, the modern compiler stack is often something that one has to learn from reading RFCs, papers, and mailing lists. With this tutorial, I&#39;ll try to give a concise overview of the modern compiler stack, how MLIR fits into it, and how to integrate it into the Python machine learning ecosystem.&lt;/p&gt;
&lt;p&gt;Throughout this series we&#39;ll cover the following topics eventually leading up to compiling a small version of the GPT2 transformer model down to efficient GPU kernels.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_introduction&quot;&gt;Part 1 - What is MLIR?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_memory&quot;&gt;Part 2 - Memory in MLIR&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_affine&quot;&gt;Part 3 - Affine Dialect and OpenMP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_linalg&quot;&gt;Part 4 - Linear Algebra and Linalg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_neural_networks&quot;&gt;Part 5 - Neural Networks and Tensors&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_egraphs&quot;&gt;Part 6 - e-graphs and Term Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Part 7 - NVIDIA GPU Execution&lt;/li&gt;
&lt;li&gt;Part 8 - Transformer Architecture&lt;/li&gt;
&lt;li&gt;Part 9 - Superoptimizing Deep Learning&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tutorial assumes some knowledge of C++ and advanced Python, along with passing familiarity with NVIDIA CUDA, but should otherwise be self-contained.&lt;/p&gt;
&lt;h3 id=&quot;why-should-i-care%3F&quot; tabindex=&quot;-1&quot;&gt;Why should I care?&lt;/h3&gt;
&lt;p&gt;You probably shouldn&#39;t. Writing optimizing compilers is a very niche topic. Although it&#39;s one that now with LLVM + MLIR requires &lt;em&gt;a lot less&lt;/em&gt; knowledge than it used to even a few years ago. So if you want to write a new AI framework or experimental programming language, it&#39;s a good time. It&#39;s not strictly neccessary to learn, but then again so few things in life are.&lt;/p&gt;
&lt;h3 id=&quot;why-mlir%3F&quot; tabindex=&quot;-1&quot;&gt;Why MLIR?&lt;/h3&gt;
&lt;p&gt;MLIR was designed to address the limitations of traditional compilers like LLVM and GCC, particularly when dealing with modern hardware accelerators and AI workloads. While LLVM excels at traditional CPU targets, it struggles with the diverse specialized architectures emerging in AI and machine learning.&lt;/p&gt;
&lt;p&gt;Modern AI systems typically involve a mix of CPUs, GPUs, TPUs, and custom ASICs. MLIR&#39;s flexible architecture makes it easier to work across these different platforms, providing robust support for heterogeneous hardware.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;dialect&lt;/strong&gt; system in MLIR can represent and optimize operations at various levels, from high-level ML tasks down to hardware-specific instructions. This means we can implement optimizations tailored to specific domains, making AI workloads run more efficiently.&lt;/p&gt;
&lt;p&gt;Instead of needing separate compilers for each type of accelerator or framework, MLIR offers a unified infrastructure that can be extended for different use cases. Traditional compilers weren’t built with AI in mind, but MLIR has first-class support for tensor operations, neural networks, transformers, and other ML constructs.&lt;/p&gt;
&lt;p&gt;MLIR has become the go-to tech for specialized machine learning accelerators, finding applications in signal processing, quantum computing, homomorphic encryption, FPGAs, and custom silicon. Its ability to create domain-specific compilers is a game-changer for those &amp;quot;weird domains&amp;quot; that don’t fit the traditional CPU and GPU mold. Plus, it can be easily embedded in other languages, allowing us to build domain-specific languages tailored for specific tasks—something we’ll dive into in this blog.&lt;/p&gt;
&lt;h3 id=&quot;key-components-of-the-modern-compiler-stack&quot; tabindex=&quot;-1&quot;&gt;Key Components of the Modern Compiler Stack&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LLVM&lt;/strong&gt;: The heart of LLVM is its intermediate representation - a low-level programming language similar to assembly but with important high-level information preserved. This IR serves as a bridge between source languages and target architectures.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MLIR&lt;/strong&gt;: MLIR is a new &amp;quot;multi-level&amp;quot; intermediate representation that is more expressive and can represent higher-level concepts like control flow, dataflow, and parallelism and be lowered into LLVM IR.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimization Passes&lt;/strong&gt;: Both MLIR and LLVM provide a vast set of optimization passes that work on the IR level, allowing any language using LLVM to benefit from sophisticated optimizations. Most are written by grad students and PhDs on the latest and greatest research in compiler optimization. This is what makes LLVM such a powerful tool.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;E-Graphs&lt;/strong&gt; Equality saturation is a technique for building optimizing compilers using e-graphs, which originated from the automated theorem proving world; it operates by applying a set of rewrites using e-matching until the e-graph is saturated, continuing the saturation process until one of several halting conditions is met. After the rewriting phase, an optimal term is extracted from the e-graph based on a cost function, typically related to the size of the syntax tree or performance considerations, allowing for more efficient optimizations by exploring a broader set of potential transformations and selecting the best one based on defined criteria.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A modern compiler then weaves these components together to make up a pipeline.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Source code is parsed into a surface language.&lt;/li&gt;
&lt;li&gt;Surface language is translated to a core language.&lt;/li&gt;
&lt;li&gt;Core langauge can be optimized by core-to-core transformations (optionally using tools like e-graphs).&lt;/li&gt;
&lt;li&gt;Core language is lowered to MLIR.&lt;/li&gt;
&lt;li&gt;MLIR performs high-level optimizations&lt;/li&gt;
&lt;li&gt;MLIR is lowered to LLVM IR&lt;/li&gt;
&lt;li&gt;LLVM performs low-level optimizations and code generation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Many programmming langauges implement the &amp;quot;mid-level&amp;quot; IR as a way to bridge the gap between the source language and LLVM. For example, Rust, Swift, Tensorflow all follow this pattern. With MLIR we have can have a new generation of programming languages and frameworks that can reuse the same infrastructure and perhaps become more interoperable, in much the same way that LLVM has become the de facto backend for many programming languages.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/targets.png&quot; alt=&quot;Modern Compilers&quot; /&gt;
&lt;/p&gt;
&lt;h3 id=&quot;installing-mlir&quot; tabindex=&quot;-1&quot;&gt;Installing MLIR&lt;/h3&gt;
&lt;p&gt;Installing MLIR is a bit painful, it requires a few dependencies to be installed first.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# MacOS&lt;/span&gt;
brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; cmake ninja ccache

&lt;span class=&quot;token comment&quot;&gt;# Linux&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; cmake ninja-build ccache
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we can clone the LLVM repository and build MLIR from source. This can take a &lt;em&gt;long&lt;/em&gt; time to build, I recommend having a cup of tea while you add entropy to the universe.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/llvm/llvm-project
&lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; llvm-project/build
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; llvm-project/build
cmake &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_PROJECTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_BUILD_EXAMPLES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_TARGETS_TO_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native;ARM;X86&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_BUILD_TYPE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Release &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_ASSERTIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--target&lt;/span&gt; check-mlir
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--target&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check the version of MLIR installed. This tutorial is using version 21.0.0.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;mlir-opt &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
LLVM &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;http://llvm.org/&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;:
  LLVM version &lt;span class=&quot;token number&quot;&gt;21.0&lt;/span&gt;.0git
  Optimized build with assertions.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At some point in the near future the Homebrew version of LLVM will be updated to include MLIR, at which point this will be a lot easier. If you&#39;re reading this at some point in a better future you can just try this and it might work.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; llvm
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mlir-concepts&quot; tabindex=&quot;-1&quot;&gt;MLIR Concepts&lt;/h3&gt;
&lt;p&gt;MLIR is a programming language in its own right, it&#39;s very low-level but it contains many of the core primitives you might find in other languages. First some identifier conventions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;%&lt;/code&gt; prefix: SSA values (e.g. &lt;code&gt;%result&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@&lt;/code&gt; prefix: Functions (e.g. &lt;code&gt;@fun&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;^&lt;/code&gt; prefix: Basic blocks (e.g. &lt;code&gt;^bb0&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#&lt;/code&gt; prefix: Attribute aliases (e.g. &lt;code&gt;#map_1d_identity&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x&lt;/code&gt; delimiter: Used in shapes and between shape and type (e.g. &lt;code&gt;10xf32&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:&lt;/code&gt; and &lt;code&gt;-&amp;gt;&lt;/code&gt; are used to indicate the type of an operation or value (e.g. &lt;code&gt;%result: i32&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;!&lt;/code&gt; prefix: Type aliases (e.g. &lt;code&gt;!avx_m128 = vector&amp;lt;4 x f32&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;()&lt;/code&gt;  are used to represent arguments (e.g. &lt;code&gt;(%arg0, %arg1)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{}&lt;/code&gt; are used to represent regions&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//&lt;/code&gt; is used for comments&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; are used to indicate type parameters (e.g. &lt;code&gt;tensor&amp;lt;10xf32&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Modules&lt;/strong&gt;: The top-level container for MLIR operations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Operations&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Functions&lt;/strong&gt;: A function is a collection of operations that are executed in a specific order.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@my_function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Operations&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dialects&lt;/strong&gt;: Domain-specific sets of operations and types in MLIR.&lt;/p&gt;
&lt;p&gt;The high-level ones well use are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;tensor&lt;/code&gt;: This dialect provides operations for creating and manipulating multi-dimensional arrays (tensors), enabling high-level mathematical expressions and transformations in a side-effect-free manner.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linalg&lt;/code&gt;: The linear algebra (linalg) dialect offers a set of operations specifically designed for linear algebra computations, facilitating efficient implementations of matrix and vector operations.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;omp&lt;/code&gt;: The OpenMP (omp) dialect includes operations that support parallel programming models, allowing developers to express parallelism in a way that is compatible with OpenMP standards.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;affine&lt;/code&gt;: The affine dialect provides a framework for expressing affine operations and analyses, enabling optimizations that leverage the predictable behavior of affine expressions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gpu&lt;/code&gt;: The GPU dialect is specialized for GPU programming, providing operations for parallel execution on heterogeneous architectures.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The lower-level ones are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;scf&lt;/code&gt;: The structured control flow (scf) dialect provides operations that represent structured control flow constructs, such as loops and conditionals, ensuring a clear and maintainable flow of execution.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;func&lt;/code&gt;: This dialect encompasses operations related to function definitions and calls, facilitating the organization and modularization of code through high-level function abstractions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;memref&lt;/code&gt;: The memref dialect is dedicated to memory reference operations, allowing for efficient management and manipulation of memory buffers, essential for performance-critical applications.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index&lt;/code&gt;: The index dialect specializes in operations for handling index computations, which are crucial for addressing elements in arrays and tensors, particularly in loop iterations and data access patterns.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;arith&lt;/code&gt;: The arithmetic (arith) dialect contains fundamental mathematical operations for both integer and floating-point types, including basic arithmetic, bitwise operations, and comparisons, applicable to scalars, vectors, and tensors.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Additionally we&#39;ll use some hardware specific dialects for targeting NVidia GPUs, but more on that later.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Operations&lt;/strong&gt;: The basic unit of work in MLIR, similar to LLVM instructions but with an additional dialect namespace and possible type annotations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Example of an MLIR operation&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;my_dialect.my_operation&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, &lt;code&gt;my_dialect.my_operation&lt;/code&gt; is an operation defined in the &lt;code&gt;my_dialect&lt;/code&gt; dialect, and &lt;code&gt;%arg0&lt;/code&gt; and &lt;code&gt;%arg1&lt;/code&gt; are arguments of type &lt;code&gt;i32&lt;/code&gt;. It returns a result of type &lt;code&gt;i32&lt;/code&gt; bound to the variable &lt;code&gt;%0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A concrete example would be the &amp;quot;arith.addf&amp;quot; operation, which adds two floating point numbers.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg0&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;arg1 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Basic Blocks&lt;/strong&gt;: Basic blocks in MLIR are sequences of operations that execute in a linear fashion without any branching. Each basic block has a single entry point and can have multiple exit points, typically through control flow operations like branches or returns. Basic blocks are essential for structuring the flow of control in a program, allowing for clear and maintainable code organization. They enable the compiler to optimize the execution path and facilitate transformations such as inlining and loop unrolling. In MLIR, basic blocks are defined within functions and can be manipulated through various dialects to represent complex control flow constructs.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Label for the then block&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;then_result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.muli &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;then_result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unike in LLVM, basic blocks can now also take arguments, which are passed in through the &lt;code&gt;^bb1(%result : i32)&lt;/code&gt; syntax.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regions&lt;/strong&gt;: Regions are a way to group operations together. They are used to represent control flow constructs like loops and conditionals. Regions are grouped together in &lt;code&gt;{}&lt;/code&gt; blocks and can take arguments.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;then_result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.muli &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;then_result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Types&lt;/strong&gt;: A type is a classification that specifies which kind of value a variable can hold and what operations can be performed on it. Types help to enforce constraints on data, ensuring that operations are performed on compatible values.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;result &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also defined type synonyms for convenience.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token type&quot;&gt;!avx_m128&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Passes&lt;/strong&gt;: Transformations that operate on the MLIR dialects, optimizing and lowering them into simpler constructs.&lt;/p&gt;
&lt;p&gt;These are arguments that can be passed to &lt;code&gt;mlir-opt&lt;/code&gt; to transform the MLIR, the most common ones are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;convert-func-to-llvm&lt;/code&gt;: Convert function-like operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-math-to-llvm&lt;/code&gt;: Convert math operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-index-to-llvm&lt;/code&gt;: Convert index operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-scf-to-cf&lt;/code&gt;: Convert structured control flow to CF dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-cf-to-llvm&lt;/code&gt;: Convert control flow to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-arith-to-llvm&lt;/code&gt;: Convert arithmetic operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reconcile-unrealized-casts&lt;/code&gt;: Reconcile unrealized casts&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-memref-to-llvm&lt;/code&gt;: Convert memref operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-tensor-to-llvm&lt;/code&gt;: Convert tensor operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-linalg-to-scf&lt;/code&gt;: Convert linalg operations to &lt;code&gt;scf.for&lt;/code&gt; loops&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-linalg-to-affine-loops&lt;/code&gt;: Convert linalg operations to &lt;code&gt;affine.for&lt;/code&gt; loops&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-omp-to-llvm&lt;/code&gt;: Convert OpenMP operations to LLVM dialect&lt;/li&gt;
&lt;li&gt;&lt;code&gt;convert-vector-to-llvm&lt;/code&gt;: Convert vector operations to LLVM dialect&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is also a generic &lt;code&gt;-convert-to-llvm&lt;/code&gt; pass that will convert anything that can be converted from MLRI to LLVM IR. In practice we&#39;ll use more granular passes to convert specific dialects to LLVM.&lt;/p&gt;
&lt;p&gt;If you need more granular control over the passes you can also specify the pass names in a comma-separated list in a &lt;code&gt;--pass-pipeline&lt;/code&gt; string, e.g., &lt;code&gt;--pass-pipeline=&amp;quot;builtin.module(pass1,pass2)&amp;quot;&lt;/code&gt;. The passes will be run sequentially in one group.&lt;/p&gt;
&lt;p&gt;Additionally there are several use flags for debugging pass transformation. The &lt;code&gt;--mlir-print-ir-after-all&lt;/code&gt; flag prints the IR after each pass, while &lt;code&gt;--mlir-print-ir-after-change&lt;/code&gt; and &lt;code&gt;--mlir-print-ir-after-failure&lt;/code&gt; provide more specific output. When using any of these print-ir flags, including &lt;code&gt;--mlir-print-ir-tree-dir&lt;/code&gt;, the IRs are written to files in a directory tree if you don&#39;t want to parse through the terminal stdout.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note: The order of the passes can be important, for example &lt;code&gt;convert-scf-to-cf&lt;/code&gt; must come before &lt;code&gt;convert-cf-to-llvm&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;creating-and-using-llvm-modules&quot; tabindex=&quot;-1&quot;&gt;Creating and Using LLVM Modules&lt;/h2&gt;
&lt;p&gt;Let&#39;s create a simple LLVM module that returns 42 as an exit code, compile it to a shared library, and use it from Python. This example demonstrates the complete pipeline from LLVM IR to usable code.&lt;/p&gt;
&lt;h3 id=&quot;1.-writing-llvm-ir&quot; tabindex=&quot;-1&quot;&gt;1. Writing LLVM IR&lt;/h3&gt;
&lt;p&gt;First, create a file named &lt;code&gt;simple.ll&lt;/code&gt; with this LLVM IR:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;define i32 @main() {
    ret i32 42
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As an analogue this program is equivalent to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2.-compiling-to-a-shared-object&quot; tabindex=&quot;-1&quot;&gt;2. Compiling to a Shared Object&lt;/h3&gt;
&lt;p&gt;We can compile this LLVM IR to a shared object using &lt;code&gt;clang&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;llc &lt;span class=&quot;token parameter variable&quot;&gt;-filetype&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;obj --relocation-model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;pic simple.ll &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; simple.o
clang &lt;span class=&quot;token parameter variable&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fPIC&lt;/span&gt; simple.o &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; libsimple.so
clang simple.o &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; simple &lt;span class=&quot;token comment&quot;&gt;# optionally create an executable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now if you run the executable you should see the output &lt;code&gt;42&lt;/code&gt; returned as the exit code to the process.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ ./simple
$ &lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$?&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3.-using-from-python&quot; tabindex=&quot;-1&quot;&gt;3. Using from Python&lt;/h3&gt;
&lt;p&gt;Now we can use this shared library &lt;code&gt;libsimple.so&lt;/code&gt; from Python using &lt;code&gt;ctypes&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ctypes

module &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CDLL&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./libsimple.so&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;main&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argtypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;main&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;restype &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ctypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_int

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running this you should see the Python logic successfully call the shared library and print the result.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;$ python simple.py
&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;mlir-universe&quot; tabindex=&quot;-1&quot;&gt;MLIR Universe&lt;/h2&gt;
&lt;p&gt;Now for a function that returns 42, it&#39;s simple enough to write in LLVM. But non-trivial programs in LLVM is not so simple, it is indeed often too &amp;quot;low-level&amp;quot; for many applications and over the years many people have constructed higher-level abstractions on top of LLVM.&lt;/p&gt;
&lt;p&gt;For example writing loops in LLVM is actually a bit annoying as you have to manually handle blocks, phi nodes, all for a basic loop. MLIR allows us to write high-level constructs directly and have them lowered down into LLVM in a single pass.&lt;/p&gt;
&lt;h3 id=&quot;4.-writing-mlir&quot; tabindex=&quot;-1&quot;&gt;4. Writing MLIR&lt;/h3&gt;
&lt;p&gt;First, create a file named &lt;code&gt;simple.mlir&lt;/code&gt; with this MLIR:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step iter_args&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum_next &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;acc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
        &lt;span class=&quot;token dialect-operation function&quot;&gt;scf.yield&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum_next &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;sum &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out_i32 &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.index_cast &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
    func.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;out_i32 &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In C this is equivalent to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; lb&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; ub&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; sum_0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sum_0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; iv &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; lb&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; iv &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; ub&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; iv &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    sum_next &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; iv&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    sum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sum_next&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; out &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; out&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4.-lowering-to-llvm-dialect&quot; tabindex=&quot;-1&quot;&gt;4. Lowering to LLVM Dialect&lt;/h3&gt;
&lt;p&gt;LLVM IR is based on Static Single Assignment, which means that every variable is assigned exactly once and every use of a variable must be dominated by its definition. In SSA, instead of variables being reassigned multiple times, each new &amp;quot;assignment&amp;quot; creates a new version of the variable, typically denoted with a numbered suffix (like &lt;code&gt;%1&lt;/code&gt;, &lt;code&gt;%2&lt;/code&gt;, etc.). This property makes many compiler optimizations simpler and more effective because the relationships between definitions and uses are explicit and unambiguous.&lt;/p&gt;
&lt;p&gt;For example, in LLVM IR, you might see code like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;%1 = add i32 %a, %b
%2 = mul i32 %1, %c
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;%1&lt;/code&gt; and &lt;code&gt;%2&lt;/code&gt; are SSA values that can never be reassigned. When control flow requires a variable to have different values depending on the path taken (like after an if-statement), LLVM uses special &amp;quot;phi&amp;quot; nodes to merge the different possible values, maintaining the SSA property while handling dynamic program behavior.&lt;/p&gt;
&lt;p&gt;In LLVM basic blocks are the basic unit of control flow, they are a sequence of instructions that are executed sequentially. A basic block has one entry and one exit point.&lt;/p&gt;
&lt;p&gt;Phi nodes are special instructions in SSA-based intermediate representations that resolve multiple potential values from different control flow paths into a single value. They&#39;re essentially control-flow-aware multiplexers that select the appropriate value based on which path the program took to reach that point.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-llvm&quot;&gt;entry:
  br i1 %cond, label %then, label %else

then:
  %x.1 = add i32 1, 2
  br label %merge

else:
  %x.2 = mul i32 3, 4
  br label %merge

merge:
  %x.3 = phi i32 [ %x.1, %then ], [ %x.2, %else ]
  ret i32 %x.3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the phi node &lt;code&gt;%x.3&lt;/code&gt; will take the value of &lt;code&gt;%x.1&lt;/code&gt; (which is 3) if control flow came from the &lt;code&gt;then&lt;/code&gt; block, or &lt;code&gt;%x.2&lt;/code&gt; (which is 12) if it came from the &lt;code&gt;else&lt;/code&gt; block. The syntax &lt;code&gt;[ value, predecessor_block ]&lt;/code&gt; specifies which value to use depending on which block was previously executed.&lt;/p&gt;
&lt;p&gt;We can lower the high-level MLIR to LLVM dialect using &lt;code&gt;mlir-opt&lt;/code&gt;. Essentially reducing the loop constructs to the branches over blocks statements.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-opt example.mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-func-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-math-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-index-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-scf-to-cf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-cf-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --convert-arith-to-llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --reconcile-unrealized-casts &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; example_opt.mlir
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will produce the following MLIR:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.mlir.constant&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 2 preds: ^bb0, ^bb2&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.icmp&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;slt&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;cond_br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb3
  &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// pred: ^bb1&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb3&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// pred: ^bb1&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; llvm.&lt;span class=&quot;token keyword&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@loop_add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;llvm.trunc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i64&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
    llvm.&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use the &lt;code&gt;mlir-cpu-runner&lt;/code&gt; to run the &lt;code&gt;main&lt;/code&gt; function directly from MLIR code.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-cpu-runner &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; main -entry-point-result&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;i32 simple_opt.mlir

&lt;span class=&quot;token comment&quot;&gt;# To use the runner utils (e.g. for debug printing)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# mlir-cpu-runner -e main -entry-point-result=i32 -shared-libs=/opt/homebrew/opt/llvm/lib/libmlir_runner_utils.dylib simple_opt.mlir&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;5.-compiling-to-a-shared-object&quot; tabindex=&quot;-1&quot;&gt;5. Compiling to a Shared Object&lt;/h3&gt;
&lt;p&gt;From there is a direct translation of the MLIR to LLVM IR using &lt;code&gt;mlir-translate&lt;/code&gt;. We can compile this MLIR to a shared object using &lt;code&gt;mlir-translate&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mlir-translate simple_opt.mlir -mlir-to-llvmir &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; simple.ll
llc &lt;span class=&quot;token parameter variable&quot;&gt;-filetype&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;obj --relocation-model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;pic simple.ll &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; simple.o
clang &lt;span class=&quot;token parameter variable&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fPIC&lt;/span&gt; simple.o &lt;span class=&quot;token parameter variable&quot;&gt;-o&lt;/span&gt; libsimple.so
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The LLVM them compiles down into assembly (in this case ARM64 assembly).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-armasm&quot;&gt;_loop_add:
sub     sp, sp, #0x30
mov     x8, #0x0
mov     x9, x8
str     x9, [sp, #0x20]
str     x8, [sp, #0x28]
b       0x100003f10
ldr     x9, [sp, #0x20]
ldr     x8, [sp, #0x28]
str     x8, [sp, #0x8]
str     x9, [sp, #0x10]
subs    x9, x9, #0xa
str     x8, [sp, #0x18]
b.ge    0x100003f4c
b       0x100003f30
ldr     x9, [sp, #0x10]
ldr     x8, [sp, #0x8]
add     x8, x8, x9
add     x9, x9, #0x1
str     x9, [sp, #0x20]
str     x8, [sp, #0x28]
b       0x100003f10
ldr     x0, [sp, #0x18]
add     sp, sp, #0x30
ret
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;standard-mlir-dialects&quot; tabindex=&quot;-1&quot;&gt;Standard MLIR Dialects&lt;/h2&gt;
&lt;h3 id=&quot;llvm&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;llvm&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;llvm&lt;/code&gt; dialect is the dialect that represents LLVM IR. It is the lowest level dialect in the MLIR hierarchy, it directly passes through to LLVM IR.&lt;/p&gt;
&lt;h3 id=&quot;scf-and-cf&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;scf&lt;/code&gt; and &lt;code&gt;cf&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;All higher-level &#39;scf&#39; structures compile into &#39;cf&#39; constructs.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cf.br&lt;/code&gt; - A branch to a basic block&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cf.cond_br&lt;/code&gt; - A conditional branch to a basic block&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cf.switch&lt;/code&gt; - A switch to a basic block&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An unconditonal branch is a branch that always goes to the same basic block.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// A basic block&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  cf.&lt;span class=&quot;token keyword&quot;&gt;br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A conditional branch is a branch that goes to a different basic block depending on a condition variable (here &lt;code&gt;%c&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// A conditional branch to a basic block&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  cf.&lt;span class=&quot;token keyword&quot;&gt;cond_br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A switch is a branch that goes to a different basic block depending on a switch variable (here &lt;code&gt;%i&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// A switch to a basic block&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb0&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token dialect-operation function&quot;&gt;cf.switch&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb3
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example we could put these constructs together to create a function that selects between two values based on a boolean flag returning one of the given arguments.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@select&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  cf.&lt;span class=&quot;token keyword&quot;&gt;cond_br&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;flag&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt;bb1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most basic structured control flow construct is the &lt;code&gt;scf.if&lt;/code&gt; construct.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token dialect-operation function&quot;&gt;scf.if&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// true region&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; else &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// false region&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Like we did a above a for loop is a structured control flow construct, it has a single entry and a single exit point.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;token dialect-operation function&quot;&gt;scf.for&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;iv &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;lb to &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;ub step &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;step &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// loop region&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would be lowered with the &lt;code&gt;convert-scf-to-cf&lt;/code&gt; pass into a sequence of &lt;code&gt;cf.br&lt;/code&gt; and &lt;code&gt;cf.cond_br&lt;/code&gt; operations.&lt;/p&gt;
&lt;h3 id=&quot;arith-and-math&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;arith&lt;/code&gt; and &lt;code&gt;math&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;arith&lt;/code&gt; dialect includes basic operations like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Integer arithmetic: &lt;code&gt;addi&lt;/code&gt;, &lt;code&gt;subi&lt;/code&gt;, &lt;code&gt;muli&lt;/code&gt;, &lt;code&gt;divsi&lt;/code&gt; (signed), &lt;code&gt;divui&lt;/code&gt; (unsigned)&lt;/li&gt;
&lt;li&gt;Floating point arithmetic: &lt;code&gt;addf&lt;/code&gt;, &lt;code&gt;subf&lt;/code&gt;, &lt;code&gt;mulf&lt;/code&gt;, &lt;code&gt;divf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Comparisons: &lt;code&gt;cmpi&lt;/code&gt; (integer), &lt;code&gt;cmpf&lt;/code&gt; (float)&lt;/li&gt;
&lt;li&gt;Conversions: &lt;code&gt;extsi&lt;/code&gt; (sign extend), &lt;code&gt;extui&lt;/code&gt; (zero extend), &lt;code&gt;trunci&lt;/code&gt;, &lt;code&gt;fptoui&lt;/code&gt;, &lt;code&gt;fptosi&lt;/code&gt;, &lt;code&gt;uitofp&lt;/code&gt;, &lt;code&gt;sitofp&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Bitwise operations: &lt;code&gt;andi&lt;/code&gt;, &lt;code&gt;ori&lt;/code&gt;, &lt;code&gt;xori&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example of arithmetic operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@arithmetic_example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Integer addition&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.constant &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Float to integer conversion&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.fptosi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt; to &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Final addition&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.addi &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;math&lt;/code&gt; dialect provides more complex mathematical operations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Trigonometric: &lt;code&gt;sin&lt;/code&gt;, &lt;code&gt;cos&lt;/code&gt;, &lt;code&gt;tan&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Exponential: &lt;code&gt;exp&lt;/code&gt;, &lt;code&gt;exp2&lt;/code&gt;, &lt;code&gt;log&lt;/code&gt;, &lt;code&gt;log2&lt;/code&gt;, &lt;code&gt;log10&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Power functions: &lt;code&gt;pow&lt;/code&gt;, &lt;code&gt;sqrt&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Special functions: &lt;code&gt;erf&lt;/code&gt;, &lt;code&gt;atan2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Constants: &lt;code&gt;constant&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example using math operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@math_example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Calculate sin(x) * sqrt(x)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;math.sin&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token dialect-operation function&quot;&gt;math.sqrt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;arith&lt;/span&gt;.mulf &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;f32&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The passes &lt;code&gt;-convert-math-to-llvm&lt;/code&gt; and &lt;code&gt;-convert-arith-to-llvm&lt;/code&gt; will convert the math and arithmetic operations to basic LLVM operation equivalents.&lt;/p&gt;
&lt;h3 id=&quot;index&quot; tabindex=&quot;-1&quot;&gt;&lt;code&gt;index&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;index&lt;/code&gt; dialect is specialized for handling index computations and is particularly important for array/tensor operations. An index type in MLIR is a platform-specific sized integer used for addressing and loop induction variables.&lt;/p&gt;
&lt;p&gt;Key index operations include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;index.constant&lt;/code&gt;: Create an index constant&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.add&lt;/code&gt;: Add two indices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.sub&lt;/code&gt;: Subtract two indices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.mul&lt;/code&gt;: Multiply two indices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.cmp&lt;/code&gt;: Compare two indices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.divs&lt;/code&gt;: Signed division of indices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.rems&lt;/code&gt;: Signed remainder of indices&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Index operations are used to compute the offset of an element in an array. For example, the following function computes the offset of an element in a 2D array. We can treat them as natural number operations as they are always non-negative.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mlir&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;func.func&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;@compute_offset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;stride&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Compute row-major array offset: i * stride + j&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.mul &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;stride
    &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;.add &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;j
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;index&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pass &lt;code&gt;-convert-index-to-llvm&lt;/code&gt; will convert the index dialect to the LLVM dialect.&lt;/p&gt;
&lt;p&gt;The index type is particularly useful when working with tensors and memory layouts, as it provides a natural way to express array indices and dimensions. It&#39;s used in conjunction with the &lt;code&gt;memref&lt;/code&gt; and &lt;code&gt;tensor&lt;/code&gt; dialects for array access patterns. Which we&#39;ll cover next.&lt;/p&gt;
&lt;h2 id=&quot;external-resources&quot; tabindex=&quot;-1&quot;&gt;External Resources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://mlir.llvm.org/docs/Dialects/Builtin/&quot;&gt;MLIR Builtin Dialect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2002.11054&quot;&gt;MLIR: A Compiler Infrastructure for the End of Moore&#39;s Law&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://ieeexplore.ieee.org/abstract/document/9370308&quot;&gt;MLIR: Scaling Compiler Infrastructure for Domain Specific Computation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lXAp6ZAWyBY&quot;&gt;2023 LLVM Dev Mtg - MLIR Is Not an ML Compiler, and Other Common Misconceptions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2101.11365&quot;&gt;A MLIR Dialect for Quantum Assembly Languages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://llvm.org/devmtg/2022-11/slides/QuickTalk15-BuildingAnEnd-to-EndToolchainForFullyHomomorphicEncryptionWithMLIR.pdf&quot;&gt;Building an End-to-End Toolchain for Fully Homomorphic Encryption with MLIR&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Mon, 10 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_introduction/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_introduction/</guid>
    </item>
    
    <item>
      <title>MLIR Part 0 - Installing MLIR</title>
      <description>&lt;h1 id=&quot;mlir-part-0---installing-mlir&quot; tabindex=&quot;-1&quot;&gt;MLIR Part 0 - Installing MLIR&lt;/h1&gt;
&lt;p&gt;Installing MLIR can be a royal pain, so here are some notes on how to do it via various methods.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-homebrew-(macos)&quot;&gt;From Homebrew (macOS)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#using-apt-(ubuntu-22.04)&quot;&gt;Using Apt (Ubuntu 22.04)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#using-llvm.sh&quot;&gt;Using &lt;code&gt;llvm.sh&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#precompiled-packages&quot;&gt;Precompiled Packages&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-source-(macos)&quot;&gt;From Source (macOS)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-source-(ubuntu-22.04)&quot;&gt;From Source (Ubuntu 22.04)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-wheel-(pip)&quot;&gt;From Wheel (pip)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-wheel-(poetry)&quot;&gt;From Wheel (Poetry)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-wheel-(uv)&quot;&gt;From Wheel (uv)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#using-conda&quot;&gt;Using Conda&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#using-docker&quot;&gt;Using Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#installing-mlir-python-bindings&quot;&gt;Installing MLIR Python Bindings&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;from-homebrew-(macos)&quot; tabindex=&quot;-1&quot;&gt;From Homebrew (macOS)&lt;/h2&gt;
&lt;p&gt;The simplest way to install MLIR on macOS is through Homebrew. This will install the latest stable version of LLVM which includes MLIR tools and libraries.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; llvm@20
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-apt-(ubuntu-22.04)&quot; tabindex=&quot;-1&quot;&gt;Using Apt (Ubuntu 22.04)&lt;/h2&gt;
&lt;p&gt;For Ubuntu users, the official LLVM APT repository provides pre-built packages for MLIR. This method ensures you get a properly configured build with all necessary dependencies.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; -qO- https://apt.llvm.org/llvm-snapshot.gpg.key &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tee&lt;/span&gt; /etc/apt/trusted.gpg.d/apt.llvm.org.asc
add-apt-repository &lt;span class=&quot;token string&quot;&gt;&quot;deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-20 main&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; update
&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; libmlir-20-dev mlir-20-tools
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-llvm.sh&quot; tabindex=&quot;-1&quot;&gt;Using &lt;code&gt;llvm.sh&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;The LLVM project provides an installation script that automatically sets up the APT repository and installs LLVM/MLIR. This is a convenient method for Ubuntu/Debian systems that handles repository setup automatically.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-O&lt;/span&gt; - https://apt.llvm.org/llvm.sh&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To install a specific version of LLVM:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; https://apt.llvm.org/llvm.sh
&lt;span class=&quot;token function&quot;&gt;chmod&lt;/span&gt; +x llvm.sh
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; ./llvm.sh &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;precompiled-packages&quot; tabindex=&quot;-1&quot;&gt;Precompiled Packages&lt;/h2&gt;
&lt;p&gt;For users who prefer to download pre-compiled Linux binaries, the LLVM project provides official release packages. These are useful when you need a specific version or want to avoid compilation time. In general these packages are best effort and may not work with your distribution / glibc setup.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Download the appropriate precompiled release package from the &lt;a href=&quot;https://github.com/llvm/llvm-project/releases&quot;&gt;LLVM releases page&lt;/a&gt;. Look for files named:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;mlir-&amp;lt;version&amp;gt;.src.tar.xz&lt;/code&gt; (e.g., &lt;code&gt;mlir-20.1.4.src.tar.xz&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Extract the archive to your desired location:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;tar&lt;/span&gt; xf mlir-&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;version&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;.src.tar.xz
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;
&lt;p&gt;Add the binaries to your PATH. You can either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Move the extracted directory to a system location:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mv&lt;/span&gt; mlir-&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;version&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;.src /usr/local/mlir
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;export PATH=/usr/local/mlir/bin:$PATH&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Or add the current location to your PATH:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;export PATH=/path/to/mlir-&amp;lt;version&gt;.src/bin:$PATH&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Verify the installation:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;mlir-opt &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: While precompiled packages save build time, they may not include all optimizations that would be available in a custom build from source.&lt;/p&gt;
&lt;h2 id=&quot;from-source-(macos)&quot; tabindex=&quot;-1&quot;&gt;From Source (macOS)&lt;/h2&gt;
&lt;p&gt;Building from source on macOS gives you the most control over the build configuration and ensures you have the latest features. This method requires more time and disk space but provides the most flexibility.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; cmake cache ninja
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/llvm/llvm-project.git
&lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; llvm-project/build
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; llvm-project/build
cmake &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_PROJECTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_BUILD_EXAMPLES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_TARGETS_TO_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_BUILD_TYPE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Release &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_ASSERTIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; mlir-opt mlir-translate mlir-transform-opt mlir-runner
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;from-source-(ubuntu-22.04)&quot; tabindex=&quot;-1&quot;&gt;From Source (Ubuntu 22.04)&lt;/h2&gt;
&lt;p&gt;Building from source on Ubuntu allows for complete customization of the MLIR build. This method is recommended for developers who need to modify MLIR or want to ensure specific optimizations are enabled.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; clang lld
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/llvm/llvm-project.git
&lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; llvm-project/build
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; llvm-project/build
cmake &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_PROJECTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;mlir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_BUILD_EXAMPLES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_TARGETS_TO_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_BUILD_TYPE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;Release &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_ASSERTIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
   &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ON
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; mlir-opt mlir-translate mlir-transform-opt mlir-runner
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;from-wheel-(pip)&quot; tabindex=&quot;-1&quot;&gt;From Wheel (pip)&lt;/h2&gt;
&lt;p&gt;For Python users, pre-built wheels are available through a custom repository. This is the easiest way to get started with MLIR in Python projects.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; mlir &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt; https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;from-wheel-(poetry)&quot; tabindex=&quot;-1&quot;&gt;From Wheel (Poetry)&lt;/h2&gt;
&lt;p&gt;Poetry users can install MLIR through a custom source repository. This method integrates well with Python dependency management and provides version control capabilities.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-yaml&quot;&gt;[tool.poetry.dependencies]
python = &amp;quot;^3.12&amp;quot;
mlir = { version = &amp;quot;latest&amp;quot;, source = &amp;quot;mlir-wheels&amp;quot;}

[[tool.poetry.source]]
name = &amp;quot;mlir-wheels&amp;quot;
url = &amp;quot;https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest&amp;quot;
priority = &amp;quot;supplemental&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to check that it works:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;poetry run mlir-opt &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;from-wheel-(uv)&quot; tabindex=&quot;-1&quot;&gt;From Wheel (uv)&lt;/h2&gt;
&lt;p&gt;The uv package manager provides a modern alternative for installing MLIR wheels. This method is particularly fast and efficient for Python projects.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;uv &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; mlir &lt;span class=&quot;token parameter variable&quot;&gt;--index&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;mlir&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For uv users, you can also specify the dependencies in a &lt;code&gt;pyproject.toml&lt;/code&gt; file. This approach provides a declarative way to manage your project&#39;s dependencies. Add the following to your &lt;code&gt;pyproject.toml&lt;/code&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-yaml&quot;&gt;[project]
dependencies = [&amp;quot;mlir&amp;quot;]

[tool.uv.sources]
mlir = { index = &amp;quot;mlir-wheels&amp;quot; }

[[tool.uv.index]]
name = &amp;quot;mlir-wheels&amp;quot;
url = &amp;quot;https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to check that it works:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;uv run mlir-opt &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-conda&quot; tabindex=&quot;-1&quot;&gt;Using Conda&lt;/h2&gt;
&lt;p&gt;Conda can be used to install both MLIR and the Python bindings.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;conda &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; conda-forge::mlir
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-docker&quot; tabindex=&quot;-1&quot;&gt;Using Docker&lt;/h2&gt;
&lt;p&gt;For users who prefer containerized environments, pre-built Docker images are available with MLIR pre-installed that I maintain here: &lt;a href=&quot;https://github.com/sdiehl/docker-mlir-cuda&quot;&gt;https://github.com/sdiehl/docker-mlir-cuda&lt;/a&gt;. These images come in both CUDA and non-CUDA variants, supporting both Ubuntu 22.04 and 24.04.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Ubuntu 24.04 Images&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# With CUDA&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir20-cuda-ubuntu24.04
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir19-cuda-ubuntu24.04

&lt;span class=&quot;token comment&quot;&gt;# Without CUDA&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir20-ubuntu24.04
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir19-ubuntu24.04

&lt;span class=&quot;token comment&quot;&gt;# Ubuntu 22.04 Images&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# With CUDA&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir20-cuda-ubuntu22.04
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir19-cuda-ubuntu22.04

&lt;span class=&quot;token comment&quot;&gt;# Without CUDA&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir20-ubuntu22.04
&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir19-ubuntu22.04
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To run any of these images interactively:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; ghcr.io/sdiehl/docker-mlir-cuda:&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;tag&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, to run the CUDA-enabled MLIR 20.1.4 for Ubuntu 24.04:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; ghcr.io/sdiehl/docker-mlir-cuda:mlir20-cuda-ubuntu24.04 &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also use these images as a base in your own Dockerfile:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-Dockerfile&quot;&gt;FROM ghcr.io/sdiehl/docker-mlir-cuda:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Replace &lt;code&gt;&amp;lt;tag&amp;gt;&lt;/code&gt; with your desired version (e.g., &lt;code&gt;mlir20-cuda-ubuntu24.04&lt;/code&gt;). For example, to pull CUDA-enabled MLIR 20.1.4 for Ubuntu 24.04:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull ghcr.io/sdiehl/docker-mlir-cuda:mlir20-cuda-ubuntu24.04
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;installing-mlir-python-bindings&quot; tabindex=&quot;-1&quot;&gt;Installing MLIR Python Bindings&lt;/h2&gt;
&lt;p&gt;The MLIR Python bindings are a separate package that can be installed using your favorite Python package manager. Once you&#39;ve installed the &lt;code&gt;mlir-wheels&lt;/code&gt; index (&lt;a href=&quot;https://www.stephendiehl.com/posts/mlir_install/#from-wheel-(poetry)&quot;&gt;as above&lt;/a&gt;), you can install the Python bindings with one of the following commands:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; mlir-python-bindings &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt; https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;poetry &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; mlir-python-bindings
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;uv &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; mlir &lt;span class=&quot;token parameter variable&quot;&gt;--index&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;mlir&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;https://github.com/makslevental/mlir-wheels/releases/expanded_assets/latest
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;conda &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; conda-forge::mlir-python-bindings
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If that doesn&#39;t work then you&#39;ll need to build them inside of the LLVM source tree from source with the &lt;code&gt;DMLIR_ENABLE_BINDINGS_PYTHON&lt;/code&gt; flag.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  bash-completion &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  ca-certificates &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  ccache &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  clang &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  cmake &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  cmake-curses-gui &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  lld &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  man-db &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  ninja-build &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  pybind11-dev &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  python3 &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  python3-numpy &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  python3-pybind11 &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  python3-yaml &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;unzip&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  xz-utils

&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/wjakob/nanobind &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; nanobind &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; submodule update &lt;span class=&quot;token parameter variable&quot;&gt;--init&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--recursive&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  cmake &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-B&lt;/span&gt; build &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_INSTALL_PREFIX&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token environment constant&quot;&gt;$HOME&lt;/span&gt;/usr &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;--target&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;

cmake llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-G&lt;/span&gt; Ninja &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-B&lt;/span&gt; build &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_BUILD_TYPE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;RelWithDebInfo &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_CXX_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang++ &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_C_COMPILER&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;clang &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DCMAKE_PREFIX_PATH&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token environment constant&quot;&gt;$HOME&lt;/span&gt;/usr &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_BUILD_EXAMPLES&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_TARGETS_TO_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_BUILD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_CCACHE_DIR&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token environment constant&quot;&gt;$HOME&lt;/span&gt;/ccache &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_ASSERTIONS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_LLD&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_ENABLE_PROJECTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mlir;clang;clang-tools-extra&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DLLVM_USE_SPLIT_DWARF&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DMLIR_ENABLE_BINDINGS_PYTHON&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DPython3_EXECUTABLE&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/bin/python3 &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# change this to your python executable&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-DMLIR_INCLUDE_INTEGRATION_TESTS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;On

cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; mlir-opt mlir-translate mlir-transform-opt mlir-runner
cmake &lt;span class=&quot;token parameter variable&quot;&gt;--build&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The binding are then built to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;build/tools/mlir/python_packages/mlir_core/mlir
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can then package them up with the following &lt;a href=&quot;https://gist.github.com/sdiehl/96e014c6287ce6bb627ffd97a7027792&quot;&gt;setup.py&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; build/tools/mlir/python_packages/mlir_core
python setup.py bdist_wheel
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And install them with:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; dist/mlir_core-*.whl
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or you can add them to your PYTHONPATH manually. Adjust the &lt;code&gt;$HOME&lt;/code&gt; to be the place where you built the MLIR source tree.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;PYTHONPATH&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$PYTHONPATH&lt;/span&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token environment constant&quot;&gt;$HOME&lt;/span&gt;/build/tools/mlir/python_packages/mlir_core
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Fri, 07 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mlir_install/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mlir_install/</guid>
    </item>
    
    <item>
      <title>SGLang on AMD MI300X</title>
      <description>&lt;h1 id=&quot;sglang-on-amd-mi300x&quot; tabindex=&quot;-1&quot;&gt;SGLang on AMD MI300X&lt;/h1&gt;
&lt;p&gt;The best kept secret these days is that new AMD GPUs are &lt;em&gt;incredibly&lt;/em&gt; cost efficient for doing inference. If you want the cliff-notes on getting an extremely fast inference server running on a fleet of AMD MI300X, here you go:&lt;/p&gt;
&lt;p&gt;If you&#39;re serving high-throughput inference, your options for inference servers are essentially:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/NVIDIA/TensorRT-LLM&quot;&gt;TensorRT-LLM&lt;/a&gt;: NVIDIA&#39;s high-performance deep learning inference library that optimizes models for deployment on GPUs, focusing on low-latency and high-throughput.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/vllm-project/vllm&quot;&gt;vLLM&lt;/a&gt;: A versatile framework for serving large language models, particularly well-suited for Kubernetes environments and high-throughput inference scenarios.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sgl-project/sglang&quot;&gt;&lt;strong&gt;SGLang&lt;/strong&gt;&lt;/a&gt;: A framework designed for efficient inference with advanced features like zero overhead batch scheduling and optimized attention kernels.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In my experience, SGLang is the easiest to get running. Although if you&#39;re using kserve to manage your Kubernetes inference service then you&#39;ll probably end up using vLLM because of network effects and lock-in.&lt;/p&gt;
&lt;p&gt;SGLang perfmance is extremely good though, with &lt;a href=&quot;https://lmsys.org/blog/2024-12-04-sglang-v0-4/&quot;&gt;zero overhead batch scheduling&lt;/a&gt; with &lt;a href=&quot;https://flashinfer.ai/2024/02/02/introduce-flashinfer.html&quot;&gt;FlashInfer&lt;/a&gt; attention kernels and &lt;a href=&quot;https://arxiv.org/abs/2312.07104&quot;&gt;RadixAttention&lt;/a&gt; for automatic KV cache reuse there&#39;s a lot of optimizations that can yield performance gains. SGLang also has an &lt;a href=&quot;https://lmsys.org/blog/2024-02-05-compressed-fsm/&quot;&gt;extremely fast constrained decoding algorithm&lt;/a&gt; if you&#39;re handlings lots of JSON output.&lt;/p&gt;
&lt;h2 id=&quot;setup&quot; tabindex=&quot;-1&quot;&gt;Setup&lt;/h2&gt;
&lt;p&gt;To get started, pull the latest SGLang image:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; pull rocm/sglang-staging:latest
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run the container.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--ipc&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;host &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--network&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;host &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--privileged&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--device&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/dev/kfd &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--device&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/dev/dri &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--device&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/dev/mem &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --group-add render &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --security-opt &lt;span class=&quot;token assign-left variable&quot;&gt;seccomp&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;unconfined &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; /home:/workspace &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  rocm/sglang-staging:latest
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then get the container id and run the following command to get a shell into the container.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$CONTAINER_ID&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then login to Hugging Face.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;huggingface-cli login
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run the following command to start the server, we&#39;ll use the DeepSeek-R1 model and quantize it to FP8.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token assign-left variable&quot;&gt;HSA_NO_SCRATCH_RECLAIM&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; python &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; sglang.launch_server &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--model&lt;/span&gt; deepseek-ai/DeepSeek-R1 &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--tp&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--quant&lt;/span&gt; fp8 &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  --trust-remote-code &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
  &lt;span class=&quot;token parameter variable&quot;&gt;--port&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run the following command to start the client.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; openai

port &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8000&lt;/span&gt;
client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; openai&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Client&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base_url&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;http://127.0.0.1:&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;port&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;/v1&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; api_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;None&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;completions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;deepseek-ai/DeepSeek-R1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Who wrote the Simarillion?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

print_highlight&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Response: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are also some other tricks you can do to get better performance.  For example, you can disable NUMA balancing.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sh&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;echo 0 &gt; /proc/sys/kernel/numa_balancing&#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more tuning guides, see the AMD documentation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://rocm.docs.amd.com/en/latest/how-to/tuning-guides/mi300x/index.html&quot;&gt;AMD MI300X Tuning Guides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://rocm.docs.amd.com/en/latest/how-to/rocm-for-ai/inference/vllm-benchmark.html&quot;&gt;LLM inference performance validation on AMD Instinct MI300X&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://rocm.docs.amd.com/en/latest/how-to/system-optimization/mi300x.html&quot;&gt;AMD Instinct MI300X System Optimization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://rocm.docs.amd.com/en/latest/how-to/rocm-for-ai/inference-optimization/workload.html&quot;&gt;AMD Instinct MI300X Workload Optimization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Tue, 04 Mar 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/sglang_mI300x/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/sglang_mI300x/</guid>
    </item>
    
    <item>
      <title>How to Make a Universe</title>
      <description>&lt;h1 id=&quot;how-to-make-a-universe&quot; tabindex=&quot;-1&quot;&gt;How to Make a Universe&lt;/h1&gt;
&lt;p&gt;There&#39;s a new paper &lt;a href=&quot;https://arxiv.org/abs/2502.00081&quot;&gt;How to Make a Universe&lt;/a&gt; by Bassani and Magueijo on the quantum cosmology arXiv that proposes a curious and clever new cosmology. They propose that the fundamental constants (&#92;(c, G, &#92;hbar, k_B, e&#92;)) are not fixed numbers, but instead evolve through a process akin to natural selection. Unlike the &lt;a href=&quot;https://en.wikipedia.org/wiki/Cosmological_natural_selection&quot;&gt;cosmological natural selection&lt;/a&gt; proposed by &lt;a href=&quot;https://en.wikipedia.org/wiki/Lee_Smolin&quot;&gt;Smolin&lt;/a&gt;, this paper doesn&#39;t require any kind of anthropic reasoning, multiverses, or black hole assumptions, instead if just presuppositionally requires a universe in which the physical laws are not fixed but evolve according to a deeper mathematical structure. And most interestingly an evolution of the laws of physics and matter creation/destruction are intertwined! I&#39;m far far outside the field anymore, but I&#39;m always interested in clever new ideas, so I thought I&#39;d try to digest it for a lay audience.&lt;/p&gt;
&lt;p&gt;Their core premise is trying to understand the origin of the universe, not as a singular event meticulously preordained, but as the outcome of a process akin to natural selection, with selection happening on the fundamental laws of physics themselves. Essentially order arising from chaos, as many pre-scientific theories have proposed. They challenge the conventional view of immutable laws and constants, proposing a dynamic environment where variations in these parameters play a crucial role in shaping the cosmos we observe. The paper marries extensions of unimodular gravity with absorbing Markov chains, to paint a picture of a universe sculpted by chaos and selection.&lt;/p&gt;
&lt;p&gt;At the heart of their model lies the idea that the &amp;quot;laws&amp;quot; of physics are not fixed entities from the beginning, but rather evolve through a period of extreme variability. This &amp;quot;higgledy-piggledy&amp;quot; phase, reminiscent of Wheeler&#39;s vision of a chaotic cosmic start, allows for the exploration of a vast space of possible physical realities. The paper explores ways to link the degree of the &amp;quot;lawlessness&amp;quot; to matter generation, in what can be seen as an implementation of Noether&#39;s theorem. They suggest this variability is not entirely random; it is driven by a deterministic process (the mechanism isn&#39;t specified) that favors certain configurations over others.&lt;/p&gt;
&lt;p&gt;The authors introduce the distinction between local and global variables. Local variables, such as the position and momentum of particles, describe the properties of spacetime at a given point. Global variables, on the other hand, characterize the universe as a whole. These global variables, representing fundamental constants like the gravitational constant or the cosmological constant, are treated as intensive quantities. They introduce a &amp;quot;preferred foliation&amp;quot; of spacetime, which provides a &amp;quot;matrix&amp;quot; for the evolution and possible freezing of constants.&lt;/p&gt;
&lt;p&gt;They also reinterpret the concept of &amp;quot;time.&amp;quot; The authors propose that time is canonically dual to the constants of nature. Inspired by phenomenological fluid clocks (based on isentropic fluids), time becomes a &amp;quot;chemical potential&amp;quot; of fundamental constants: that is, the time dual to a certain constant (e.g., Newton&#39;s constant) dictates the rate at which the constant can change. This analogy allows them to connect variations in fundamental constants to matter creation or destruction. The rate of flow of the time dual to a constant is determined by the chemical potential of that constant. This means the violation of energy conservation is directly linked to the interaction between a matter parameter and a gravity clock (or vice versa). This provides a theoretical basis for the creation of matter in the early universe.&lt;/p&gt;
&lt;p&gt;The other core idea in their model is the separation of matter and gravity within the chaotic early universe. While intuitively we might assume that no such distinction exists in a state of complete chaos, the authors postulate that even a modest level of separation can provide an evolutionary advantage. This requirement translates into the existence of separate local and global variables for matter and gravity, leading to a separation of the Hamiltonian into matter and gravity components.&lt;/p&gt;
&lt;p&gt;The separation leads to a deterministic mechanism of evolution where constants of nature can evolve with time and, consequently, can lead to a change in the mass-energy of the Universe. The equation they derive for the time evolution of matter depends on the Poisson bracket between matter and gravity parts of the Hamiltonian.&lt;/p&gt;
&lt;p&gt;The key concept in their model is the role of &lt;a href=&quot;https://en.wikipedia.org/wiki/General_covariance&quot;&gt;diffeomorphism invariance&lt;/a&gt;. Diffeomorphism invariance is the expression of general covariance (the laws of physics are independent of the coordinate system). The authors propose that diffeomorphism invariance emerges as a natural outcome of the selection process. By requiring the algebra of the constraints to follow the Dirac hypersurface deformation algebra in the absence of global interactions, they ensure that their framework respects this fundamental symmetry. This ultimately explains how gravity is always present, whereas matter can be optional. The upshot is that nearly diffeomorphism-invariant states are favoured as the end product of the evolutionary process.&lt;/p&gt;
&lt;p&gt;The authors then introduce a stochastic element, inspired by the random mutations that drive biological evolution. They model these mutations using an absorbing Markov chain, where states represent different configurations of the universe with varying physical constants. The absorbing state represents a stable configuration where evolution ceases, and the laws of physics become fixed (i.e. constants become truly constant). The use of an absorbing Markov chain implies that, once the absorbing state is reached, it can no longer be left, meaning the &amp;quot;constants&amp;quot; of Nature really become constant.&lt;/p&gt;
&lt;p&gt;Within this Markov chain, the constants of nature can jump from state to state, either becoming a function of an a variable, or converging to an a variable. This random distribution has no biases to one or other side and the chance to keep positive gains of mass creation requires a local Hamiltonian with a clear separation of matter and gravity.&lt;/p&gt;
&lt;p&gt;The combination of the deterministic process and stochastic mutations provides a theoretical framework for cosmic natural selection. Universes with configurations conducive to matter creation and stability are favored, while those that lead to annihilation or instability are eliminated. Universes containing clear separation between matter and gravity are favoured by the evolution along the Markov chain, as are those with nearly-diffeomorphism symmetry, which is also an absorbing state.&lt;/p&gt;
&lt;p&gt;I&#39;m far outisde my depth as to speak to the validity or falsifiability of this model, but it&#39;s a very clever concept that at the very least is interesting and novel. The idea that we live in a universe where random mutations in physical constants were guided by a deterministic environment which favored stable matter-rich configurations and ultimately led to the nearly diffeomorphism-invariant cosmos we observe.&lt;/p&gt;
</description>
      <pubDate>Sun, 16 Feb 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/how_to_make_a_universe/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/how_to_make_a_universe/</guid>
    </item>
    
    <item>
      <title>The FlashAttention CUDA Kernel Line by Line</title>
      <description>&lt;h1 id=&quot;the-flashattention-cuda-kernel-line-by-line&quot; tabindex=&quot;-1&quot;&gt;The FlashAttention CUDA Kernel Line by Line&lt;/h1&gt;
&lt;p&gt;Flash Attention is a memory-efficient algorithm for computing attention in transformers. Let&#39;s break down the CUDA implementation block by block. The core innovation of Flash Attention is processing attention in blocks while maintaining numerical stability through careful tracking of maximum values and partial sums. The algorithm achieves memory efficiency by never materializing the full attention matrix while still producing mathematically equivalent results to the standard attention mechanism.&lt;/p&gt;
&lt;p&gt;First we need some small macros. The &lt;code&gt;CEIL_DIV&lt;/code&gt; is used to compute the ceiling of the division of two numbers.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;#include &amp;lt;cuda.h&amp;gt;
#include &amp;lt;cuda_runtime.h&amp;gt;

#define CEIL_DIV(x, y) ((x) &amp;gt;= 0 ? (((x) + (y) - 1) / (y)) : ((x) / (y)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need the kernel declaration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;template &amp;lt;const int Br, const int Bc&amp;gt;
__global__ void flash_attn_kernel(float* Q, float* K, float* V, int N, int d, int Tr, int Tc, float scale, float* l, float* m, float* O)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This declares a CUDA kernel with template parameters &lt;code&gt;Br&lt;/code&gt; and &lt;code&gt;Bc&lt;/code&gt; which represent the block sizes for rows and columns. The function takes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Q, K, V: Query, Key, and Value matrices&lt;/li&gt;
&lt;li&gt;N: Sequence length&lt;/li&gt;
&lt;li&gt;d: Head dimension&lt;/li&gt;
&lt;li&gt;Tr, Tc: Number of blocks in rows and columns&lt;/li&gt;
&lt;li&gt;scale: Scaling factor for attention scores&lt;/li&gt;
&lt;li&gt;l, m: Auxiliary arrays for the numerically stable softmax computation&lt;/li&gt;
&lt;li&gt;O: Output matrix&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Thread and Block Index Setup&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;int tx = threadIdx.x;
int bx = blockIdx.x;
int by = blockIdx.y;

int qkv_off = (bx * gridDim.y * N * d) + (by * N * d);
int lm_off = (bx * gridDim.y * N) + (by * N);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This section retrieves the thread and block indices (tx, bx, by) that identify which thread is executing. It then calculates two important offset values: qkv_off for accessing the Q, K, V matrices and lm_off for accessing the auxiliary arrays l and m. These offsets ensure each block processes the correct portion of the input data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shared Memory Setup&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;extern __shared__ float smem[];
float* Qi = smem;
float* Kj = Qi + Br * d;
float* Vj = Kj + Bc * d;
float* Sij = Vj + Bc * d;
float* Oi = Sij + Br * Bc;
float* li = Oi + Br * d;
float* li_new = li + Br;
float* mi = li_new + Br;
float* mi_new = mi + Br;
float* mij_dash = mi_new + Br;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This section partitions the shared memory into different regions for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Qi: Current block of Query matrix&lt;/li&gt;
&lt;li&gt;Kj: Current block of Key matrix&lt;/li&gt;
&lt;li&gt;Vj: Current block of Value matrix&lt;/li&gt;
&lt;li&gt;Sij: Attention scores&lt;/li&gt;
&lt;li&gt;Oi: Output accumulator&lt;/li&gt;
&lt;li&gt;li, li_new, mi, mi_new, mij_dash: Softmax computation variables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Main Processing Loops&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The kernel uses two nested loops:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Outer loop over Key/Value blocks (&lt;code&gt;j&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Inner loop over Query blocks (&lt;code&gt;i&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Key-Value Loading&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;for (int j = 0; j &amp;lt; Tc; j++) {
    int loads_per_thread = CEIL_DIV(d, Br);
    for (int e = 0; e &amp;lt; loads_per_thread; e++) {
        int idx = e * (Br * Bc) + tx;
        if (idx &amp;lt; Bc * d) {
            int row = idx / d;
            int col = idx % d;

            if (j * Bc + row &amp;lt; N) {
                Kj[row * d + col] = K[qkv_off + (j * Bc + row) * d + col];
                Vj[row * d + col] = V[qkv_off + (j * Bc + row) * d + col];
            }
        }
    }
    __syncthreads();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This section loads the current block of K and V matrices into shared memory, with bounds checking to prevent out-of-bounds access.&lt;/p&gt;
&lt;p&gt;Let&#39;s break down the main computation loop which processes each block of queries:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Loading Query and Output Data&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;for (int e = 0; e &amp;lt; loads_per_thread; e++) {
    int idx = e * (Br * Bc) + tx;
    if (idx &amp;lt; Br * d) {
        int row = idx / d;
        int col = idx % d;
        if (i * Br + row &amp;lt; N) {
            Qi[row * d + col] = Q[qkv_off + (i * Br + row) * d + col];
            Oi[row * d + col] = O[qkv_off + (i * Br + row) * d + col];
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This section loads the current block of Query matrix and Output accumulator into shared memory. Each thread may load multiple elements to ensure efficient memory access patterns. The bounds checking ensures we don&#39;t access out-of-bounds memory when N isn&#39;t perfectly divisible by the block size.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Thread Position Calculation and Max/Sum Loading&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;int s_row = tx / Bc;
int s_col = tx % Bc;

if (s_col == 0) {
    mi[s_row] = m[lm_off + (i * Br) + s_row];
    li[s_row] = l[lm_off + (i * Br) + s_row];
}
__syncthreads();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each thread calculates its position in the shared memory block. The first thread in each row (&lt;code&gt;s_col == 0&lt;/code&gt;) loads the running maximum and sum values needed for the numerically stable softmax computation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Computing Attention Scores&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;float acc = 0.f;
for (int k = 0; k &amp;lt; d; k++)
    acc += Qi[s_row * d + k] * Kj[s_col * d + k];

acc *= scale;
Sij[s_row * Bc + s_col] = acc;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This computes the scaled dot product attention scores between the Query and Key matrices. Each thread computes one element of the attention score matrix Sij.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Numerically Stable Softmax Computation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;if (s_col == 0) {
    float row_m = -INFINITY, row_l = 0.f;
    // Find max for numerical stability
    for (int c = 0; c &amp;lt; Bc; c++) {
        float val = Sij[s_row * Bc + c];
        if (val &amp;gt; row_m) {
            row_m = val;
        }
    }
    // Compute exponentials and sum
    for (int c = 0; c &amp;lt; Bc; c++) {
        float exp_val = expf(Sij[s_row * Bc + c] - row_m);
        Sij[s_row * Bc + c] = exp_val;
        row_l += exp_val;
    }

    // Update running max and sum
    mij_dash[s_row] = row_m;
    mi_new[s_row] = max(mi[s_row], row_m);
    li_new[s_row] = expf(mi[s_row] - mi_new[s_row]) * li[s_row] + 
                    expf(row_m - mi_new[s_row]) * row_l;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This implements the numerically stable softmax algorithm. For each row:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Find the maximum value for numerical stability&lt;/li&gt;
&lt;li&gt;Compute exponentials normalized by the maximum&lt;/li&gt;
&lt;li&gt;Sum the normalized exponentials&lt;/li&gt;
&lt;li&gt;Update the running maximum and sum for the online softmax computation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Output Computation and Update&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;for (int col = s_col; col &amp;lt; d; col += Bc) {
    float acc = 0.f;
    for (int c = 0; c &amp;lt; Bc; c++)
        acc += Sij[s_row * Bc + c] * Vj[c * d + col];

    int global_row = (i * Br) + s_row;
    if (global_row &amp;lt; N) {
        Oi[s_row * d + col] = (1 / li_new[s_row]) * 
            ((li[s_row] * expf(mi[s_row] - mi_new[s_row]) * Oi[s_row * d + col]) + 
             (expf(mij_dash[s_row] - mi_new[s_row]) * acc));
        O[qkv_off + global_row * d + col] = Oi[s_row * d + col];
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This final section:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Computes the matrix multiplication between the softmax probabilities and the Value matrix&lt;/li&gt;
&lt;li&gt;Updates the output using the online softmax algorithm&lt;/li&gt;
&lt;li&gt;Each thread may compute multiple output elements (when d &amp;gt; Bc)&lt;/li&gt;
&lt;li&gt;The reason for the complex update formula is to ensure numerical stability while accumulating partial results&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;State Update for Next Iteration&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-c++&quot;&gt;m[lm_off + (i * Br) + s_row] = mi_new[s_row];
l[lm_off + (i * Br) + s_row] = li_new[s_row];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we update the running maximum and sum values in global memory for the next iteration. This is crucial for the online softmax computation across blocks.&lt;/p&gt;
</description>
      <pubDate>Tue, 11 Feb 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/flash_attention/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/flash_attention/</guid>
    </item>
    
    <item>
      <title>Training with GRPOTrainer</title>
      <description>&lt;h1 id=&quot;training-with-grpotrainer&quot; tabindex=&quot;-1&quot;&gt;Training with GRPOTrainer&lt;/h1&gt;
&lt;p&gt;DeepSeek&#39;s impressive new reasoning model is powered by a key innovation called &lt;strong&gt;Group Relative Policy Optimization&lt;/strong&gt; (GRPO). This clever variant of PPO not only improves the model&#39;s training convergence but also uses less memory than traditional approaches. This was one of the core idea that helped DeepSeek&#39;s new R1 model achieve state-of-the-art performance with incredibly low training costs. This article explores the basics of how to use GRPOTrainer, why it works and how it can be used in new model training.&lt;/p&gt;
&lt;p&gt;GRPO is an online learning algorithm that improves iteratively using data generated by the model during training. The key innovation is that it eliminates the need for a critic model by estimating the baseline from group scores, significantly reducing training resources compared to PPO.&lt;/p&gt;
&lt;p&gt;The algorithm works in four main steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Generating completions&lt;/strong&gt;: For each prompt in a batch, generate G different completions using the old policy&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Computing rewards&lt;/strong&gt;: Score each completion using rule-based reward functions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normalizing advantages&lt;/strong&gt;: Calculate the relative advantage by normalizing rewards using the group&#39;s mean and standard deviation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimizing the policy&lt;/strong&gt;: Update the model using a clipped PPO-like objective that keeps the new policy close to the old one&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This approach is particularly efficient because it eliminates the need for a separate critic network, uses group statistics to establish baselines, maintains policy stability through clipping, and reduces memory usage compared to traditional PPO.&lt;/p&gt;
&lt;h2 id=&quot;%22aha-moment%22&quot; tabindex=&quot;-1&quot;&gt;&amp;quot;Aha Moment&amp;quot;&lt;/h2&gt;
&lt;p&gt;During DeepSeek-R1&#39;s training, researchers observed what they called an &amp;quot;aha moment&amp;quot; ... which is the term they use for the emergence of sophisticated multi-step reasoning behavior through GRPO training. The model demonstrated a form of emergent chain-of-thought reasoning where it would:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Generate an initial solution approach&lt;/li&gt;
&lt;li&gt;Automatically verify the correctness of its reasoning&lt;/li&gt;
&lt;li&gt;Identify potential flaws or gaps in logic&lt;/li&gt;
&lt;li&gt;Generate alternative approaches when necessary&lt;/li&gt;
&lt;li&gt;Compare multiple solution paths before settling on a final answer&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This behavior manifested particularly in mathematical and logical reasoning tasks. For example, when solving complex word problems. What made this particularly interesting was that this verification behavior emerged purely through the GRPO reward optimization process, without any explicit chain-of-thought prompting or human feedback. The group-relative advantage computation naturally encouraged the model to develop more thorough reasoning strategies, as solutions that included verification steps consistently achieved higher rewards within their comparison groups.&lt;/p&gt;
&lt;p&gt;Four key emergent behaviors were observed that they noted in the paper:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Self-verification&lt;/strong&gt;: The model learned to automatically check its work and validate its answers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Extended chain-of-thought&lt;/strong&gt;: Solutions became more detailed with explicit step-by-step reasoning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exploratory reasoning&lt;/strong&gt;: The model would try multiple approaches before settling on the best solution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reflection&lt;/strong&gt;: The model developed the ability to question and revise its own reasoning process.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This emergent behavior aligns with theoretical predictions about the benefits of group-relative policy optimization: by comparing multiple solution attempts within the same context group, the training process naturally selects for more robust and self-correcting reasoning patterns. The KL divergence constraint ensures these patterns develop gradually and stably, rather than through sudden policy shifts.&lt;/p&gt;
&lt;p&gt;In simpler terms, it&#39;s like the model learned to &amp;quot;check its work&amp;quot; all on its own, similar to how a careful student might solve a math problem, then go back to make sure their answer makes sense, and try a different approach if something seems off. The training method essentially rewarded this careful, thorough approach by comparing different solution attempts side by side, like a teacher grading multiple versions of the same homework assignment and giving better scores to students who show their work and verify their answers. The model wasn&#39;t explicitly taught to do this - it just naturally developed these good study habits because they consistently led to better outcomes!&lt;/p&gt;
&lt;h2 id=&quot;theory&quot; tabindex=&quot;-1&quot;&gt;Theory&lt;/h2&gt;
&lt;p&gt;GRPO is an online learning algorithm that iteratively improves by using data generated by the model during training. The key innovation of GRPO is its approach to estimating advantages through group-based normalization and its efficient handling of KL divergence constraints.&lt;/p&gt;
&lt;p&gt;At its core, GRPO operates by first generating multiple completions for each input prompt. For each prompt in a batch, the model generates &#92;(G&#92;) different completions using the current policy &#92;(&#92;pi_{&#92;theta}&#92;). These completions form a &amp;quot;group&amp;quot; that will be used for relative comparison, which is fundamental to how GRPO estimates advantages.&lt;/p&gt;
&lt;p&gt;The advantage estimation process is what gives GRPO its name. For each completion, a reward &#92;(r_i&#92;) is computed using a reward model. Instead of using a critic network to estimate advantages, GRPO normalizes rewards within each group:&lt;/p&gt;
&lt;p&gt;$$&#92;hat{A}_{i,t} = &#92;frac{r_i - &#92;text{mean}(&#92;mathbf{r})}{&#92;text{std}(&#92;mathbf{r})}$$&lt;/p&gt;
&lt;p&gt;This group-relative normalization approach is particularly effective as it naturally compares rewards relative to other completions in the same group, providing a built-in baseline for advantage estimation.&lt;/p&gt;
&lt;p&gt;To ensure stable training, GRPO employs KL divergence estimation using Schulman&#39;s approximator. This term helps ensure the policy doesn&#39;t deviate too far from the reference policy during training. The complete GRPO loss function combines advantage maximization with this KL divergence penalty.&lt;/p&gt;
&lt;p&gt;By eliminating the need for a separate critic model it achieves better memory efficiency. The group-relative advantage computation helps reduce variance in training, while the KL divergence term prevents policy collapse. The normalization within groups makes the method robust to reward scaling, and the single update per generation (compared to multiple updates in PPO) simplifies implementation. This design makes GRPO particularly well-suited for integration with existing language model architectures.&lt;/p&gt;
&lt;p&gt;To put it in simpler terms, imagine GRPO as a teacher grading essays in a classroom. Instead of having a separate teaching assistant (critic model) help grade papers, the teacher looks at small groups of essays together. For each group, they rank the essays relative to each other, which ones are better or worse compared to the average of that specific group. This is like normalizing the rewards within groups.&lt;/p&gt;
&lt;p&gt;The teacher also makes sure not to completely change their grading criteria between assignments (KL divergence constraint), they want to stay somewhat consistent with how they&#39;ve graded before. This helps students understand what&#39;s expected and improve gradually rather than facing wildly different standards each time.&lt;/p&gt;
&lt;p&gt;This approach is more efficient than traditional methods because you don&#39;t need that extra teaching assistant, and comparing essays within small groups is more manageable than trying to compare every essay to every other essay. It&#39;s like the difference between grading on a curve for each small discussion section versus trying to curve grades across the entire university!&lt;/p&gt;
&lt;h2 id=&quot;usage&quot; tabindex=&quot;-1&quot;&gt;Usage&lt;/h2&gt;
&lt;p&gt;Here&#39;s how to use GRPO via the TRL framework:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; GRPOConfig&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; GRPOTrainer

&lt;span class=&quot;token comment&quot;&gt;# Load your dataset&lt;/span&gt;
dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;trl-lib/tldr&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Configure training arguments&lt;/span&gt;
training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; GRPOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_generations&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_prompt_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_completion_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    beta&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.04&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# KL penalty coefficient&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize trainer&lt;/span&gt;
trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; GRPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;your/base/model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    reward_funcs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;reward_func&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Start training&lt;/span&gt;
trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key training parameters are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;num_generations&lt;/code&gt;: Number of completions to generate per prompt (default: 8)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;beta&lt;/code&gt;: KL penalty coefficient to control how far the model can deviate from reference policy (default: 0.04)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;temperature&lt;/code&gt;: Sampling temperature for generation (default: 0.9)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_prompt_length&lt;/code&gt;: Maximum length for input prompts (default: 512)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_completion_length&lt;/code&gt;: Maximum length for generated completions (default: 256)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;GRPO logs several important metrics during training:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;completion_length&lt;/code&gt;: Average completion length&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reward/{reward_func_name}&lt;/code&gt;: Reward computed by each reward function&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reward&lt;/code&gt;: Average reward&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reward_std&lt;/code&gt;: Average standard deviation within reward groups&lt;/li&gt;
&lt;li&gt;&lt;code&gt;kl&lt;/code&gt;: Average KL divergence between model and reference model&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To accelerate generation, which is often the main bottleneck in online methods, you can use vLLM:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; GRPOConfig

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; GRPOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... other args ...&lt;/span&gt;
    use_vllm&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    vllm_gpu_memory_utilization&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also define custom reward functions to guide the training. The function must:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Accept &lt;code&gt;prompts&lt;/code&gt; and &lt;code&gt;completions&lt;/code&gt; as keyword arguments&lt;/li&gt;
&lt;li&gt;Return a list of floats representing rewards for each completion&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Example reward function that rewards longer completions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reward_func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;completions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Reward function that evaluates natural text quality based on multiple factors:
    - Length (with diminishing returns after target length)
    - Readability metrics
    - Sentence structure variety
    - Proper punctuation
    &quot;&quot;&quot;&lt;/span&gt;
    rewards &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    target_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Ideal completion length&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; completion &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; completions&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Base score&lt;/span&gt;
        score &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Length reward with diminishing returns&lt;/span&gt;
        length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;completion&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; length &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; target_length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            length_score &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; length &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; target_length
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            length_score &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; target_length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        score &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; length_score
        
        &lt;span class=&quot;token comment&quot;&gt;# Reward sentence variety (look for mix of short and long sentences)&lt;/span&gt;
        sentences &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; completion&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sentences&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            lengths &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; sentences&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; statistics&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;variance&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lengths&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lengths&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
            score &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; variance &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Cap variance reward&lt;/span&gt;
            
        &lt;span class=&quot;token comment&quot;&gt;# Reward proper punctuation&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; completion &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;.&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;!&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;?&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            score &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;
            
        &lt;span class=&quot;token comment&quot;&gt;# Penalize very short sentences or fragments&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; sentences&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            score &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;
            
        rewards&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;score&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; rewards
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;GRPO supports multiple reward functions - just pass them as a list:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; GRPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    reward_funcs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;reward_func1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; reward_func2&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# rest of arguments&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The final reward will be computed as the sum of rewards from each function.&lt;/p&gt;
</description>
      <pubDate>Fri, 07 Feb 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/grpotrainer/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/grpotrainer/</guid>
    </item>
    
    <item>
      <title>The Case Against Crypto in 2025</title>
      <description>&lt;h1 id=&quot;the-case-against-crypto-in-2025&quot; tabindex=&quot;-1&quot;&gt;The Case Against Crypto in 2025&lt;/h1&gt;
&lt;p&gt;In 2020, I &lt;a href=&quot;https://www.stephendiehl.com/posts/against_crypto&quot;&gt;wrote what became&lt;/a&gt; a widely-circulated critique of cryptocurrency and its implications for our financial system. At the time, I desperately hoped to be proven wrong. As a technologist who deeply believes in the potential of technology to strengthen democratic institutions, I took no pleasure in forecasting the corrosive potential of crypto assets. Unfortunately, the intervening years have borne out many of my predictions with shocking prescience.&lt;/p&gt;
&lt;p&gt;In the years since publishing that critique, I&#39;ve had the privilege of engaging with financial authorities and policymakers worldwide—from central banks and regulatory bodies to national governments and think tanks. My work has included coordinating joint letters to legislative bodies across three continents, authoring a book on the subject, and giving interviews at most global media outlets. I&#39;ve done as much as humanly possible for one person to do to raise awareness of the risks posed by crypto assets because as a millennial who lived through the financial crisis I feel a twisted sense of civic responsibility to at least attempt to stave off the dark future I see unfolding.&lt;/p&gt;
&lt;p&gt;However I must admit that despite extensive efforts to sound the alarm, the response has been stubborn resistance in the face of an overwhelming ambient culture of techno-solutionism that pervades modern life. Our democratic institutions remain paralyzed in the face of crypto&#39;s mounting threats, and the ascension of the Trump administration has only accelerated this regulatory abdication and the threat of crypto. I won&#39;t sugar-coat it, we are living through a challenging era and things may get much worse before they get better.&lt;/p&gt;
&lt;p&gt;What originated as an obscure libertarian experiment has metastasized into an existential threat to financial stability and a political machine for far right extremists. The parallel shadow system constructed around crypto assets has reproduced precisely the systemic vulnerabilities that post-2008 reforms sought to eliminate. We have watched in real time as major crypto institutions imploded, retail investors lost their savings, and sophisticated financial instruments emerged to conceal rather than manage risk. And yet governments across the western world have done next to nothing to address these issues.&lt;/p&gt;
&lt;p&gt;After years of persistent advocacy, I recognize that the next phase of this crucial policy debate requires new voices and broader perspectives. People who are smarter and more eloquent than me. The challenges posed by cryptocurrency demand an expanded coalition of experts spanning technology, economics, law enforcement, national security, and public policy.&lt;/p&gt;
&lt;p&gt;For those continuing this important work, I offer this parting counsel of the things I&#39;ve learned over the past four years talking to politicians, regulators, and the public. With a pivotal showdown looming in 2025, Democrats still have a narrow window to strengthen their failed approach and fight against the MAGA party&#39;s extreme anarcho-capitalist agenda. The choices made in the coming years will determine whether we finally bring this shadow financial system under control or surrender our financial stability to unrestrained speculation that may give rise to a new financial crisis or an even darker future.&lt;/p&gt;
&lt;h2 id=&quot;equivocation-has-failed&quot; tabindex=&quot;-1&quot;&gt;Equivocation has Failed&lt;/h2&gt;
&lt;p&gt;Let&#39;s be crystal clear, that while Democrats deserve some criticism for their tepid response on the crypto problem, the Republican party bears the lion&#39;s share of responsibility for our regulatory paralysis. The GOP has been thoroughly corrupted by crypto industry money, with prominent Republican leaders actively championing deregulation while taking millions in campaign contributions from crypto super PACs. Their cynical obstruction tactics in Congress have effectively blocked any meaningful legislation, even modest consumer protection measures with broad bipartisan support. Republicans have created a legislative graveyard for crypto regulation, leaving federal agencies hamstrung and good-faith Democratic efforts dead on arrival. This systematic sabotage reflects the GOP&#39;s broader descent into market fundamentalism, where any attempt to rein in financial speculation is denounced as &amp;quot;stifling innovation&amp;quot;, regardless of the clear public interest in basic oversight.&lt;/p&gt;
&lt;p&gt;The core issue is that the Democratic party&#39;s impotent attempts at a &amp;quot;moderate&amp;quot; crypto policy position have become an embarrassing testament to political cowardice. The mealy-mouthed equivocations—that the technology is neither good nor bad, that we mustn&#39;t regulate software too heavily, that Bitcoin might have legitimate uses for marginalized groups—are intellectual fig leaves covering naked regulatory capture. While these positions might satisfy the party&#39;s donor class and business-aligned policy institutes, they represent an abdication of government&#39;s duty to protect the public.&lt;/p&gt;
&lt;p&gt;As a result, the Democratic party&#39;s tendency toward technocratic incrementalism and regulatory restraint has created a vacuum that bad actors have eagerly filled. By treating crypto as just another financial innovation requiring modest oversight, moderates have enabled the very predatory practices they claim to oppose. Their appeals to &amp;quot;balanced regulation&amp;quot; and &amp;quot;promoting innovation&amp;quot; amount to little more than rhetorical cover for inaction in the face of widespread fraud and market manipulation.&lt;/p&gt;
&lt;p&gt;This failure of nerve among Democratic leadership reflects a deeper rot in American politics—the capture of our regulatory apparatus by monied interests who profit from regulatory inaction. The same politicians who righteously denounce Wall Street excess are perfectly content to let crypto exchanges operate as unregulated casinos, destroying retail investors while enriching venture capitalists and hedge funds. Their studied neutrality serves only to protect the powerful while leaving ordinary citizens exposed to sophisticated financial predation.&lt;/p&gt;
&lt;p&gt;The time for half-measures and careful moderation has long passed. The crypto industry has demonstrated, through repeated cycles of fraud and collapse, that it cannot and will not reform or regulate itself. Democratic policymakers must abandon their tepid both-sidesism and embrace aggressive regulation that treats crypto assets as what they are—unregistered securities and vehicles for financial crime. Anything less is an abdication of government&#39;s fundamental responsibility to protect the publict.&lt;/p&gt;
&lt;p&gt;Yet there are reasons for hope. Leaders like Senator Elizabeth Warren have been steadfast champions of strong regulation and consumer protections, even in the face of fierce industry opposition and pressure from the donor class. Her tireless work to expose crypto&#39;s risks to consumers and financial stability, along with concrete legislative proposals to bring the industry under proper oversight, demonstrates the kind of principled leadership desperately needed. While a sclerotic Congress continues to drag its feet, there are still many Democrats who are willing to fight for a more aggressive regulatory response.&lt;/p&gt;
&lt;h2 id=&quot;shapeshifting-narratives&quot; tabindex=&quot;-1&quot;&gt;Shapeshifting Narratives&lt;/h2&gt;
&lt;p&gt;The crypto industry&#39;s greatest trick has been its ability to shapeshift its narrative to seduce any audience. Like a chameleon changing colors, crypto morphs into whatever its marks most desperately want to see. To libertarians, it&#39;s a weapon against government tyranny. To progressives, it&#39;s financial inclusion for the unbanked. To tech investors, it&#39;s paradigm-shifting innovation. To get-rich-quick dreamers, it&#39;s a ticket to untold wealth. &lt;a href=&quot;https://bettermarkets.org/analysis/the-golden-goose-that-never-laid-golden-eggs-unfulfilled-promises-of-cryptocurrencies/&quot;&gt;The stories are as numerous as they are contradictory&lt;/a&gt;. When talking to regulators, crypto companies &lt;a href=&quot;https://www.theguardian.com/technology/2024/jan/18/coinbase-cryptocurrency-lawsuit-beanie-babies-securities-sec&quot;&gt;argue their tokens are mere collectibles like Beanie Babies&lt;/a&gt;, but to the public they present themselves as revolutionary financial instruments transforming the global economy. The sophistry and inconsistency is truly breathtaking.&lt;/p&gt;
&lt;p&gt;However, this calculated incoherence is no accident—it&#39;s essentially a form of intellectual three-card monte designed to exhaust and confuse critics while attracting true believers of all stripes. The moment you try to pin down and critique one narrative, advocates smoothly slide to another, equally nonsensical position. Challenge Bitcoin&#39;s viability as a currency? It&#39;s actually a store of value. Question its merit as a store of value? It&#39;s really about the underlying technology. Point out the technology&#39;s fundamental limitations? Well, you just don&#39;t understand the transformative potential of web3. When web3 failed, they went back to the start and called it a currency again, and round and round they go.&lt;/p&gt;
&lt;p&gt;The intellectual dishonesty is staggering. Bitcoin cannot simultaneously be a hedge against inflation and a high-growth speculative asset. It cannot be both a currency for daily transactions and digital gold to be hoarded. DeFi cannot replace traditional finance while depending entirely on traditional finance for its value and utility. NFTs cannot democratize art while concentrating wealth in the hands of wealthy speculators and wash traders. These contradictions don&#39;t matter because the real product being sold is the intoxicating promise of easy riches.&lt;/p&gt;
&lt;p&gt;What makes this sophistry so insidious is how it weaponizes complexity and jargon to short-circuit critical thinking. The average person, overwhelmed by technical terminology and afraid of appearing ignorant, becomes susceptible to whatever narrative most appeals to their hopes and fears. Meanwhile, true believers can choose whichever story best fits their ideological preferences, constructing elaborate post-hoc justifications while ignoring the glaring contradictions in their position.&lt;/p&gt;
&lt;p&gt;The end result is a kind of distributed Ponzi scheme where everyone convinces themselves they&#39;re the smart money, not the greater fool. The only consistent thread running through crypto&#39;s shifting narratives is the promise of getting rich through speculation rather than productive work. Everything else—the technology, the philosophy, the grand social vision—is merely elaborate window dressing for humanity&#39;s oldest scam: the promise of something for nothing.&lt;/p&gt;
&lt;h2 id=&quot;weaponized-jargon&quot; tabindex=&quot;-1&quot;&gt;Weaponized Jargon&lt;/h2&gt;
&lt;p&gt;The crypto industry has mastered the dark art of linguistic manipulation, wielding terms like &amp;quot;blockchain&amp;quot; and &amp;quot;tokenization&amp;quot; as magical incantations that mean everything and nothing at once. These words function as a kind of semantic smokescreen, allowing crypto advocates to dance between definitions whenever convenient. One moment they&#39;re describing a so-called &amp;quot;revolutionary&amp;quot; financial technology, the next a mere database—whatever serves their regulatory arbitrage du jour.&lt;/p&gt;
&lt;p&gt;This deliberate ambiguity has proven devastatingly effective in policy circles, where aging legislators and their overwhelmed staffers grasp desperately for understanding. The average member of Congress, already struggling to operate their iPhone, stands no chance against this calculated obscurantism. They&#39;re left nodding along to incomprehensible techno-babble while the industry runs circles around meaningful oversight.&lt;/p&gt;
&lt;p&gt;The crypto lobby has perfected this strategy of strategic ambiguity, shrouding their operations in a fog of technical jargon and shifting definitions. When pressed on specific regulations, they retreat into abstract discussions of cryptography and distributed systems. When questioned about their actual use cases, they pivot to grandiose visions of &amp;quot;financial inclusion&amp;quot; and &amp;quot;democratization.&amp;quot; It&#39;s a shell game played with words instead of peas.&lt;/p&gt;
&lt;p&gt;Almost a decade ago I led engineering teams trying to make blockchain technology work for regulated financial applications, and I can state with absolute technical certainty: the fundamental architecture is incompatible with anything except regulatory evasion and criminal enterprise. The immutable, public, and permissionless nature of these systems—the very features touted as &amp;quot;revolutionary&amp;quot;—make them unsuitable for legitimate financial infrastructure. If you remove these features to make the system work for enterprise needs, you&#39;re left with nothing more than an inefficient, overcomplicated database wrapped in buzzwords and marketing hype that could (and should) always be replaced by a simpler solution. But by keeping the discussion mired in definitional quicksand, the industry has successfully delayed this simple truth from reaching policymakers.&lt;/p&gt;
&lt;p&gt;However let&#39;s be clear: this is a free country and no serious policy proposal involves banning cryptography, linked lists, or the word &amp;quot;blockchain.&amp;quot; The crypto lobby has masterfully constructed this straw man argument to paint any regulation as technologically illiterate overreach. They deliberately conflate basic computer science concepts with their specific applications in unregistered securities and financial schemes.&lt;/p&gt;
&lt;p&gt;The mathematical primitives underlying cryptocurrency—hash functions, public key cryptography, merkle trees—are and will remain perfectly legal. They&#39;re inert tools used across legitimate industries. But this fact is entirely irrelevant to the policy debate around crypto assets. We don&#39;t need to ban the SHA-256 hash function to effectively regulate cryptocurrency any more than we need to ban spreadsheets to prosecute accounting fraud.&lt;/p&gt;
&lt;p&gt;For future advocates taking up this cause: don&#39;t let the industry&#39;s semantic games derail substantive policy discussions. When they retreat into abstract technical descriptions, drag the conversation back to their actual business practices. The problem isn&#39;t the technology—it&#39;s how it&#39;s being weaponized for financial misconduct. Focus relentlessly on the concrete harms: market manipulation, consumer fraud, sanctions evasion, and systemic risk.&lt;/p&gt;
&lt;p&gt;The First Amendment protects speech, not financial schemes. We must understand that regulating crypto assets is about curtailing harmful business practices, not restricting software use. The industry&#39;s supposed constitutional concerns are a calculated distraction from straightforward securities law violations.&lt;/p&gt;
&lt;h2 id=&quot;political-capture&quot; tabindex=&quot;-1&quot;&gt;Political Capture&lt;/h2&gt;
&lt;p&gt;The 2024 election cycle laid bare the extent of crypto&#39;s political influence operations. The industry poured nearly $200 million into campaign advertising, targeting lawmakers who dared question their practices or advocate for consumer protections. Yet tellingly, &lt;a href=&quot;https://www.axios.com/2024/10/29/crypto-election-ad-spend-fairshake-pac&quot;&gt;these attack ads studiously avoided mentioning cryptocurrency itself&lt;/a&gt;—a tacit admission that the American public remains deeply skeptical of an industry associated with ransomware, money laundering, and financial crime.&lt;/p&gt;
&lt;p&gt;This massive spending spree has yielded a cohort of captured politicians who reliably champion industry interests while paying lip service to &amp;quot;innovation&amp;quot; and &amp;quot;financial inclusion.&amp;quot; Representatives like Ritchie Torres, Kirsten Gillibrand, Chuck Schumer, and Rohit Khanna have emerged as particularly captured advocates for the industry. Their support comes as no surprise given the enormous sums flowing into their campaigns and leadership PACs from crypto interests.&lt;/p&gt;
&lt;p&gt;The industry&#39;s primary legislative goal is transparent—they seek to engineer regulatory arbitrage by shifting oversight to the dramatically under-resourced Commodity Futures Trading Commission. With a budget of just $365 million and 700 employees, compared to the SEC&#39;s 2.1 billion and 4,500 staff, the CFTC would be overwhelmed by the task of policing this complex and fraud-prone sector. This is precisely what crypto lobbyists want—the appearance of regulation without meaningful enforcement capability.&lt;/p&gt;
&lt;p&gt;This cynical strategy reflects a broader corruption of American democracy by monied interests. The crypto industry&#39;s lavish spending and sophisticated influence operations eerily echo earlier eras when powerful financial interests captured the regulatory apparatus meant to constrain them. Then as now, the result is a political system that protects predatory business practices while leaving ordinary citizens exposed to sophisticated forms of financial exploitation.&lt;/p&gt;
&lt;p&gt;In the wake of FTX&#39;s collapse and Sam Bankman-Fried&#39;s conviction, the industry has only intensified its influence peddling, raising hundreds of &lt;a href=&quot;https://www.washingtonpost.com/business/interactive/2024/crypto-firms-candidates-house-senate-election-2024/&quot;&gt;millions through Super PACs&lt;/a&gt; to push legislation that would grant it legitimacy while avoiding meaningful oversight. Key Senate races have become battlegrounds, with industry groups specifically targeting independent voices like Senators Sherrod Brown and Elizabeth Warren who have consistently stood against special interests.&lt;/p&gt;
&lt;p&gt;The legislative agenda being advanced is particularly concerning. Both the House Financial Services and House Agriculture Committees have established dedicated digital asset subcommittees, leading to a flood of industry-friendly bills. These proposals consistently favor minimal regulation while enabling an expansion of crypto offerings that could further endanger retail investors and broader financial stability.&lt;/p&gt;
&lt;p&gt;Most troubling is how the industry has successfully cultivated a bipartisan coalition of lawmakers deeply tied to crypto interests. These representatives consistently advance legislation designed to create the mere appearance of oversight while carefully preserving the regulatory gaps that enable predatory practices. This sophisticated capture of the legislative process threatens to permanently enshrine crypto&#39;s shadow financial system in law.&lt;/p&gt;
&lt;h2 id=&quot;american-culture&quot; tabindex=&quot;-1&quot;&gt;American Culture&lt;/h2&gt;
&lt;p&gt;The transformation of American culture over the past few decades reveals a troubling shift from a principle-based society to naked transactionalism, particularly among younger generations. Growing up in the crucible of late-stage capitalism has warped many Americans&#39; fundamental understanding of value and social relationships. Where previous generations at least paid lip service to enlightenment ideals of reason, justice, and the common good, a growing segment of society now views every interaction purely through the lens of personal gain and market logic.&lt;/p&gt;
&lt;p&gt;This mindset is particularly prevalent among millennials (my generation), who have internalized the most extreme forms of market fundamentalism. Having come of age during the 2008 financial crisis, rising inequality, and the gig economy, many have abandoned traditional liberal values in favor of a ruthlessly transactional worldview. Every relationship, every interaction, every moment becomes an opportunity for arbitrage or personal advantage. The language of &amp;quot;optimization&amp;quot; and &amp;quot;value extraction&amp;quot; has escaped the boardroom to colonize our most intimate social spaces.&lt;/p&gt;
&lt;p&gt;The rise of cryptocurrency perfectly embodies this cultural shift. Its most vocal advocates openly reject any notion of collective good or social responsibility in favor of pure financial self-interest. The industry&#39;s marketing specifically targets young men&#39;s anxieties about status and economic security, promising them that unrestrained market speculation is the only path to dignity in an increasingly precarious world. This message resonates precisely because it validates their existing tendency to view everything—friendships, romance, civic participation—as mere transactions to be optimized.&lt;/p&gt;
&lt;p&gt;However, this transformation is neither universal nor inevitable. Many developed nations with market economies have maintained stronger social bonds and collective values, underpinned by political and cultural institutions—hallmarks of robust liberal democracies—that promote civic virtue. Even within America, many communities uphold principles of reason, justice, and the common good, offering a potent alternative to a purely transactional and nihilistic existence.&lt;/p&gt;
&lt;p&gt;For Democrats crafting policy responses to cryptocurrency, understanding this cultural context is crucial. A significant portion of crypto&#39;s support comes not from genuine belief in its technological merits, but from those who have fully embraced market fundamentalism as a worldview and personal identity. Traditional appeals to consumer protection or financial stability may fall flat with voters who see unfettered speculation as a moral right and reject any notion of collective good.&lt;/p&gt;
&lt;p&gt;Yet this reality should strengthen, not weaken, Democrats&#39; resolve to aggressively regulate cryptocurrency. The New Deal succeeded precisely because it offered a compelling alternative to pure market fundamentalism—a vision of shared prosperity and collective purpose that transcended narrow self-interest.&lt;/p&gt;
&lt;p&gt;The paramount threat to the 21st-century body politic lies in the corrosive synthesis of extreme partisanship with anti-system politics—a &#39;burn it all down&#39; mentality—and a deepening societal nihilism. This dangerous trend fosters profound cynicism, rejecting not just specific policies but the very legitimacy of democratic processes and shared societal values, thereby rendering constructive engagement and collective problem-solving nearly impossible. The antidote to this existential challenge is a robust democratic renewal, achieved by making government demonstrably work for its people once more.&lt;/p&gt;
&lt;p&gt;Today&#39;s Democratic party must similarly champion values of reason, justice, and truth against the corrosive effects of extreme transactionalism. The crypto debate isn&#39;t just about financial regulation—it&#39;s about what kind of society we want to be.&lt;/p&gt;
&lt;h2 id=&quot;predatory-inclusion&quot; tabindex=&quot;-1&quot;&gt;Predatory Inclusion&lt;/h2&gt;
&lt;p&gt;The crypto industry&#39;s claims of financial inclusion represent a particularly insidious form of predatory inclusion—one that exploits legitimate concerns about financial discrimination to lure vulnerable communities into risky speculation. This narrative proves especially paralyzing for Democrats, who find themselves disarmed by accusations of &amp;quot;financial privilege&amp;quot; when raising valid regulatory concerns.&lt;/p&gt;
&lt;p&gt;The industry cynically appropriates the language of social justice while targeting the very communities historically harmed by financial discrimination. After generations of redlining, loan denials, and predatory lending, minority communities&#39; legitimate desire to close the wealth gap makes them especially vulnerable to crypto&#39;s false promises of financial empowerment.&lt;/p&gt;
&lt;p&gt;This predatory inclusion manifests in deliberately exploitative practices. The industry strategically places &lt;a href=&quot;https://www.bloomberg.com/news/articles/2024-04-17/bitcoin-atms-flood-black-latino-areas-charging-fees-up-to-22?sref=mQvUqJZj&quot;&gt;Bitcoin ATMs in predominantly Black&lt;/a&gt;, Latino, and low-income neighborhoods, charging exorbitant fees while often only allowing purchases, not sales. They aggressively market get-rich-quick narratives in communities with limited financial resources and resilience, knowing full well that crypto&#39;s extreme volatility and rampant fraud puts their targets at risk of devastating losses.&lt;/p&gt;
&lt;p&gt;What&#39;s marketed as a path to financial liberation instead becomes &lt;a href=&quot;https://bettermarkets.org/wp-content/uploads/2024/10/Better_Markets_Fact_Sheet_Crypto_Impact_Minorities-10.24.24.pdf&quot;&gt;another vehicle for exploitation&lt;/a&gt;. The industry&#39;s targeting of minority communities isn&#39;t inclusion—it&#39;s predation wearing the mask of empowerment. For communities that can least afford losses, crypto&#39;s combination of volatility, fraud, and manipulated markets represents not an opportunity, but a threat to generational wealth building.&lt;/p&gt;
&lt;p&gt;The industry&#39;s co-option of progressive language represents a calculated strategy to deflect criticism and legitimize their practices. Terms like &amp;quot;democratizing finance&amp;quot; and &amp;quot;financial inclusion&amp;quot; are wielded as shields against regulatory scrutiny, while the underlying business model continues to extract wealth from vulnerable communities. This appropriation of social justice terminology to serve profit motives undermines genuine efforts at economic empowerment and makes it harder to address real systemic inequities. The result is a cynical inversion where the language of liberation is used to enable further exploitation.&lt;/p&gt;
&lt;h2 id=&quot;criminality&quot; tabindex=&quot;-1&quot;&gt;Criminality&lt;/h2&gt;
&lt;p&gt;Just as I wrote in 2021, cryptocurrency&#39;s primary real-world use case, beyond pure gambling, is enabling criminal enterprise, political corruption, and sanctions evasion on a massive scale. From North Korean hackers to Russian oligarchs, from narco-terrorists to ransomware gangs, crypto provides the financial infrastructure for modern organized crime.&lt;/p&gt;
&lt;p&gt;The numbers are staggering. According to the &lt;a href=&quot;https://www.ic3.gov/AnnualReport/Reports/2024_IC3Report.pdf&quot;&gt;FBI&#39;s 2024 Internet Crime Report&lt;/a&gt;, cryptocurrency was involved in 56% of the $16.6 billion in total cybercrime losses that year. Crypto-related losses reached 9.3 billion in 2024, a 66% increase from 2023. The FBI received over 140,000 complaints involving cryptocurrency from harmed individuals—more than double the number from the previous year.&lt;/p&gt;
&lt;p&gt;Particularly concerning is the targeting of vulnerable populations. The FBI found that individuals over 60 were disproportionately victimized, accounting for roughly 33,000 complaints and $2.8 billion in losses. Complaints about cryptocurrency ATMs and kiosks nearly doubled, with losses up 31%. The bureau also documented a 59% surge in crypto-enabled extortion schemes, including &amp;quot;sextortion&amp;quot; attacks where criminals manipulate photos and videos to intimidate victims.&lt;/p&gt;
&lt;p&gt;North Korea has turned cryptocurrency theft into a cornerstone of state financing, using armies of hackers to raid exchanges and steal billions in digital assets. These funds directly support the regime&#39;s nuclear weapons program and other illicit activities, demonstrating how crypto undermines global security. Similarly, Iranian state actors exploit cryptocurrency to evade international sanctions, funding terrorist operations across the Middle East.&lt;/p&gt;
&lt;p&gt;Russia&#39;s invasion of Ukraine highlighted cryptocurrency&#39;s role in sanctions evasion. Despite international financial restrictions, Russian oligarchs and state entities have leveraged crypto networks to move billions of dollars, undermining the effectiveness of coordinated Western sanctions. The decentralized nature of cryptocurrency, touted as a feature by advocates, serves in practice as a critical vulnerability in the global anti-money laundering regime.&lt;/p&gt;
&lt;p&gt;The narcotics trade has eagerly embraced cryptocurrency, with cartels using it to launder proceeds and facilitate cross-border payments. Mexican drug organizations alone are estimated to launder billions annually through crypto networks, exploiting the pseudonymous nature of transactions to obscure the flow of illegal profits. This has made the task of disrupting drug trafficking operations significantly more challenging for law enforcement.&lt;/p&gt;
&lt;p&gt;Beyond organized crime, cryptocurrency enables an endless parade of scams targeting retail investors. The playbook is depressingly consistent: unregistered securities offerings, false promises of astronomical returns, wash trading to manipulate prices, and misappropriation of customer funds. The industry&#39;s resistance to basic financial controls and customer protections creates perfect conditions for sophisticated fraud.&lt;/p&gt;
&lt;p&gt;Tax evasion through cryptocurrency has also emerged as a major concern for authorities. The difficulty of tracking crypto transactions, combined with widespread non-reporting of gains, creates a shadow financial system that deprives governments of legitimate revenue. This tax gap ultimately shifts the burden onto compliant citizens while undermining public services.&lt;/p&gt;
&lt;p&gt;Perhaps most troubling is how cryptocurrency enables ransomware attacks on critical infrastructure, healthcare facilities, and local governments. Criminal groups can now easily monetize cyberattacks through untraceable ransom payments, creating an entire criminal industry that threatens public safety and national security. The Colonial Pipeline attack demonstrated how crypto-enabled ransomware can disrupt essential services across entire regions. The FBI&#39;s conclusion is unequivocal: &amp;quot;cryptocurrency has become an enticing means to cheat investors, launder proceeds, and engage in other illicit schemes.&amp;quot; The only place that crypto has truly found product market fit is in enabling criminal enterprise.&lt;/p&gt;
&lt;h2 id=&quot;tech-institutions&quot; tabindex=&quot;-1&quot;&gt;Tech Institutions&lt;/h2&gt;
&lt;p&gt;The technology industry&#39;s institutions have devolved into little more than libertarian think tanks and self-congratulatory social clubs. Organizations like the IEEE, ACM and EFF, which once stood for broader societal good, now serve primarily as echo chambers for techno-utopian fantasies and regulatory resistance. Their leadership, steeped in the misguided idealism of the cypherpunk era, consistently prioritizes an absolutist view of technological freedom over any meaningful engagement with democracy or the public good.&lt;/p&gt;
&lt;p&gt;The rank and file of the tech industry rival Wall Street bankers in their studied indifference to the societal impact of their work. Behind their carefully cultivated image of casual brilliance lies a studied indifference to the societal impact of their work. They build surveillance systems, addictive platforms, and manipulative algorithms while hiding behind the paper-thin shield of &amp;quot;just doing my job.&amp;quot;&lt;/p&gt;
&lt;p&gt;This moral abdication is ultimately a symptom of false consciousness, masked as a pragmatic progressivism that reduces labor to a mere instrument of survival under capitalism. The ruling ideology is seductive in its simplicity: tech workers, compelled to sell their labor for a wage, are absolved of all responsibility for the outcomes they produce. This framework systematically alienates the worker from the consequences of their labor, providing ideological cover for the capitalist class to pursue any profitable venture, no matter how socially destructive with no accountability.&lt;/p&gt;
&lt;p&gt;The public&#39;s faith in tech worker restraint or corporate self-regulation is dangerously misplaced. The rank and file are simply going to keep building crypto scams, because they can, and their peers give them a permission structure that rationalizes it. The industry&#39;s workforce has proven itself entirely willing to implement whatever dystopian features maximize engagement metrics, so long as the compensation packages remain competitive. The occasional protests amount to little more than performative gestures, quickly forgotten when the next round of RSUs vest. This is an industry I am very much a part of, and we deserve every bit of society&#39;s growing contempt—we&#39;ve earned our place alongside predatory lawyers and corporate lobbyists as professionals who consistently choose self-interest over social responsibility.&lt;/p&gt;
&lt;h2 id=&quot;securities-and-exchange-commission&quot; tabindex=&quot;-1&quot;&gt;Securities and Exchange Commission&lt;/h2&gt;
&lt;p&gt;While Gary Gensler&#39;s SEC ultimately arrived at the correct regulatory stance on cryptocurrency, the path there was frustratingly slow and tentative. History will likely judge Gensler favorably as a principled regulator who stood firm against an onslaught of industry pressure and personal attacks, but the deliberate pace of enforcement allowed the crypto bubble to inflate far larger than necessary. A more aggressive early approach could have prevented billions in retail investor losses and curtailed the industry&#39;s growing political influence.&lt;/p&gt;
&lt;p&gt;To his credit, Gensler maintained a consistent position that most crypto assets are unregistered securities, even as intense lobbying and congressional pressure sought to undermine this basic regulatory framework. His SEC methodically built cases against major industry players while facing severe resource constraints and coordinated resistance. The string of successful enforcement actions in 2023-24 validated his approach and helped establish essential case law.&lt;/p&gt;
&lt;p&gt;However, this measured strategy also reflected a broader Democratic tendency toward technocratic incrementalism when bolder action was needed. A more forceful regulatory response in 2021-22, while the industry was still relatively contained, could have short-circuited its expansion into a systemic threat. Instead, the slow accumulation of case law and regulatory guidance allowed bad actors to exploit inaction while building political defenses against oversight.&lt;/p&gt;
&lt;p&gt;The lesson for future Democratic leadership is clear—having the right policy position isn&#39;t enough. Effective regulation requires the political courage to act decisively, even in the face of powerful opposition. While Gensler deserves credit for staying the course under immense pressure, his legacy also highlights the costs of regulatory timidity when confronting emerging threats to financial stability and consumer protection. We&#39;re now left with the regulatory equivalent of trying to put out a forest fire with a garden hose.&lt;/p&gt;
&lt;h2 id=&quot;media&quot; tabindex=&quot;-1&quot;&gt;Media&lt;/h2&gt;
&lt;p&gt;The media&#39;s coverage of cryptocurrency has proven consistently inadequate for meaningful policy discourse. While major outlets can generally report basic facts about significant events like exchange collapses or high-profile arrests, they demonstrate a concerning inability to engage with the substantive regulatory and policy implications of this technology.&lt;/p&gt;
&lt;p&gt;This limitation manifests most prominently in what might be called the &amp;quot;Bloomberg consensus,&amp;quot; epitomized by writers like Matt Levine who approach crypto with a detached intellectual curiosity that prioritizes clever analysis over public welfare. This stance—treating cryptocurrency as a fascinating experiment in market structure worthy of observation rather than a contagion requiring intervention—exemplifies the problem. When sophisticated financial instruments are being weaponized against retail investors and to undermine our democratic institutions, mere observation is no longer enough.&lt;/p&gt;
&lt;p&gt;The New York Times&#39; coverage proves particularly problematic, often serving as an unintentional reputation laundering service for the industry. Their tendency to present &amp;quot;balanced&amp;quot; coverage of fundamentally predatory business models grants unearned legitimacy to crypto enterprises. By treating industry talking points as equivalent to regulatory concerns, they create false equivalencies that muddy public understanding rather than clarify it.&lt;/p&gt;
&lt;p&gt;Even well-intentioned journalists (of which there are many) face structural barriers to producing meaningful coverage. The sheer volume of concerning incidents in the cryptocurrency space makes it impossible to thoroughly investigate each case, leading to superficial coverage that fails to expose systemic issues. The complexity of cryptocurrency&#39;s technical, financial, and regulatory dimensions requires deep expertise across multiple domains, yet the modern media environment&#39;s demands for rapid content production means reporters must constantly chase the next headline rather than build deep understanding. Additionally, even when reporters uncover clear evidence of malfeasance, legal departments often require watered-down language to avoid potential litigation, making it nearly impossible to call a spade a spade. The result is coverage that barely scratches the surface while questionable practices continue to emerge unchallenged.&lt;/p&gt;
&lt;p&gt;For those seeking to advance meaningful policy changes, traditional media engagement is increasingly ineffective, especially for reaching younger audiences. The crypto industry&#39;s sophisticated PR machine and media&#39;s limitations make it nearly impossible to get complex messages through. Instead, we need to focus on platforms where younger civicly engaged voters actually spend their time. Writing op-eds for the Financial Times and Foreign Policy is all well and good, but it won&#39;t reach the people who need to hear it in our increasingly post-literate culture.&lt;/p&gt;
&lt;h2 id=&quot;the-dark-future&quot; tabindex=&quot;-1&quot;&gt;The Dark Future&lt;/h2&gt;
&lt;p&gt;Let me paint a picture of a possible future that I believe could emerge if we do not act.&lt;/p&gt;
&lt;p&gt;The breakdown of the international liberal order creates perfect conditions for cryptocurrency&#39;s most dystopian potential to emerge. As multilateral institutions crumble and democratic norms erode globally, we&#39;re witnessing the rise of hollow democracies—maintaining the superficial trappings of democratic process while being thoroughly captured by plutocratic interests. Cryptocurrency serves as the perfect tool for this capture, enabling untraceable flows of dark money that render campaign finance laws meaningless and electoral systems mere theater.&lt;/p&gt;
&lt;p&gt;The historian Quinn Slobodian provides a compelling analysis in &lt;a href=&quot;https://www.theguardian.com/books/2023/mar/30/crack-up-capitalism-by-quinn-slobodian-review-zoning-out&quot;&gt;Crack-Up Capitalism&lt;/a&gt;, where wealthy elites seek to fragment nation states into private zones of control, while Anne Applebaum&#39;s &lt;a href=&quot;https://www.penguin.co.uk/books/455382/autocracy-inc-by-applebaum-anne/9780241627891&quot;&gt;Autocracy Inc&lt;/a&gt; documents the rise of modern authoritarian networks. These analyses predict a likely future: as multilateral institutions crumble and democratic norms erode globally, we see the emergence of hollow democracies—maintaining the superficial trappings of democratic process while being thoroughly captured by minority interests.&lt;/p&gt;
&lt;p&gt;Following the Viktor Orbán playbook, these regimes maintain a thin veneer of democratic legitimacy while systematically dismantling institutional checks and balances. Cryptocurrency serves as a powerful catalyst for this transformation, providing the perfect tool for plutocrats to further capture the political process. Campaign finance laws become meaningless as untraceable crypto assets flow freely to compliant politicians. Electoral systems remain technically intact, but outcomes are increasingly predetermined through sophisticated manipulation funded by wealth and algorithmic manipulation of attention.&lt;/p&gt;
&lt;p&gt;The wealthy crypto-holding class effectively becomes a shadow government, using their accumulated digital assets to shape policy and capture regulatory agencies. While formal democratic institutions continue to exist, they serve mainly as rubber stamps for policies that benefit this new plutocracy. Public services are gradually privatized and hollowed out, with cripled private alternatives emerging that further entrench inequality.&lt;/p&gt;
&lt;p&gt;Most disturbing is how this system perpetuates itself through manufactured consent. Social media platforms, their algorithms optimized for engagement and controlled by autocratic and plutocratic interests, keep the public distracted with an endless stream of artificial controversies and manufactured culture wars. Critical thinking and civic engagement wither as people retreat into virtual worlds and digital communities carefully curated to maintain political apathy.&lt;/p&gt;
&lt;p&gt;The promised crypto utopia reveals itself as a nightmare of democratic decay—not through outright authoritarianism, but through the quiet erosion of democratic substance while maintaining democratic form. Public institutions continue to exist but serve primarily as vehicles for private interests, with cryptocurrency providing the perfect tool for this capture. The death of democracy comes not through violent revolution but through the gradual hollowing out of democratic institutions until only an empty shell remains.&lt;/p&gt;
&lt;h2 id=&quot;future-work&quot; tabindex=&quot;-1&quot;&gt;Future Work&lt;/h2&gt;
&lt;p&gt;I&#39;m writing this primarily for Congressional staffers and strategists (you know who you are 👋). The political reality through 2026 demands a defensive strategy focused on preventing harmful legislation rather than pursuing immediate reform. With Republicans controlling the House, wielding effective veto power in the Senate, and now heading the SEC and DOJ, passing or enforcing robust crypto regulation is effectively impossible in the near term. Instead, Democrats must concentrate on blocking industry-written bills while building public support and preparing comprehensive oversight frameworks for when they regain control of Congress and the executive branch.&lt;/p&gt;
&lt;p&gt;This interim period provides an opportunity to shape the broader narrative around cryptocurrency and its societal impact. The industry&#39;s unpopularity with the general public—&lt;a href=&quot;https://www.pewresearch.org/short-reads/2024/10/24/majority-of-americans-arent-confident-in-the-safety-and-reliability-of-cryptocurrency/&quot;&gt;Pew Research polling&lt;/a&gt; consistently shows skepticism above 60%—creates fertile ground for education about systemic risks and predatory practices. This skepticism stems from direct lived experience—millions of Americans have lost savings to crypto scams, watched friends and family members get burned by fraudulent schemes, or seen young men in their communities consumed by gambling addiction. For most Americans, cryptocurrency is just that scam their weird cousin lost his life savings on while ranting about getting rich quick. Democrats must recognize this as both a moral imperative and political opportunity—by standing firmly against industry-written bills that would enable further exploitation, they can demonstrate their commitment to protecting ordinary Americans from sophisticated financial predators.&lt;/p&gt;
&lt;p&gt;Paradoxically, the Trump administration may also be a gift to critics, as exemplified by the shameless TRUMP memecoin launch on the eve of his inauguration. Nothing could better crystallize cryptocurrency&#39;s true nature than a president eagerly slapping his &amp;quot;brand&amp;quot; on a worthless token, nakedly designed to extract money from his most devoted followers. This isn&#39;t subtle regulatory arbitrage or complex financial innovation—it&#39;s a carnival barker hawking digital snake oil to an audience he views as marks. For those encountering cryptocurrency for the first time through this lens, the industry&#39;s protestations about &amp;quot;financial freedom&amp;quot; and &amp;quot;technological innovation&amp;quot; ring especially hollow. When a sitting president is willing to so blatantly leverage his office for a quick crypto cash grab, it becomes impossible to maintain the pretense that this technology serves any purpose beyond enabling sophisticated grift. The TRUMP token stands as a perfect crystallization of everything wrong with cryptocurrency—and now it&#39;s incredibly easy to paint the entire industry with a broad brush given the very visible examples of predation at the highest levels of government.&lt;/p&gt;
&lt;p&gt;In the next administration, the crypto industry&#39;s aggressive lobbying and attacks on regulatory advocates should be welcomed, not feared. As Franklin Roosevelt declared of the financial interests arrayed against the New Deal: &amp;quot;They are unanimous in their hate for me—and I welcome their hatred.&amp;quot; Their opposition validates the Democratic Party&#39;s role as defender of public welfare against sophisticated financial predation. Every industry attack ad and hostile congressional hearing becomes an opportunity to highlight crypto&#39;s true nature as a vehicle for exploitation.&lt;/p&gt;
&lt;p&gt;Although there is an elephant in the room, it is a stark reality that demands acknowledgment: strict enforcement of existing securities regulation frameworks—specifically the Securities Act of 1933 and Securities Exchange Act of 1934—would effectively terminate the cryptocurrency industry&#39;s operations within the United States. The overwhelming majority of crypto offerings and exchanges currently operating would be deemed non-compliant with these foundational statutes, and their business models would be rendered unprofitable and unviable. This is not a ban per se, but the outcome would essentially be the same. Rather than equivocate on this outcome, &lt;strong&gt;politicians should embrace this conclusion&lt;/strong&gt;. The cryptocurrency market&#39;s primary function of facilitating speculative trading of digital tokens with no underlying economic claims or productive value represents neither capital formation nor efficient market allocation. Such pure speculation, divorced from fundamental economic activity, provides no meaningful contribution to aggregate welfare or economic productivity. If society wishes to facilitate zero-sum gambling, we have properly regulated venues for such activities—&lt;strong&gt;they&#39;re called casinos&lt;/strong&gt;. The distinction between legitimate capital markets and speculative gambling must be maintained and strengthened.&lt;/p&gt;
&lt;p&gt;Democrats must develop clear, emotionally resonant messaging on cryptocurrency that connects with everyday voters, especially low-information and disengaged voters. Rather than getting bogged down in technical or legal details, the focus should be on crafting simple, memorable slogans (that could fit on a hat) that tap into common-sense skepticism about crypto schemes. There are three core ideas:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Cryptocurrency is a useless, speculative, and harmful technology for legitimate finance that causes widespread individual financial ruin and poses systemic economic risks.&lt;/li&gt;
&lt;li&gt;Cryptocurrency is inherently linked to pervasive fraud and criminality, operating outside or actively undermining necessary regulatory and legal frameworks.&lt;/li&gt;
&lt;li&gt;Cryptocurrency is a corrosive political force that undermines democratic institutions, fuels right-wing extremism, and threatens monetary sovereignty.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The messaging strategy should emphasize relatable analogies (comparing crypto to casino gambling, magic beans, or Monopoly money), establish clear villains (tech plutocrats, crypto insiders) and victims (regular Americans), and make strong emotional appeals around fairness and financial security. The goal is to bypass complex policy debates in favor of intuitive, kitchen-table economic arguments that feel true to people&#39;s lived experience. This means using simple language free of jargon, offering straightforward solutions like &amp;quot;ban it&amp;quot; or &amp;quot;shut it down,&amp;quot; and maintaining a clear moral stance that cryptocurrency is fundamentally deceptive and harmful to Main Street. By focusing on these basic storytelling elements — simple language, clear stakes, emotional resonance, and calls to action — Democrats can build a messaging framework that energizes voters while advancing substantive policy goals. The messaging doesn&#39;t need to be strictly factual, it just needs to feel true and tap into voters&#39; lived experiences, preconceived notions, and intuitions.&lt;/p&gt;
&lt;p&gt;Success requires maintaining message discipline across the party while building a broader coalition of technologists, academics, consumer advocates, and politicians who recognize crypto&#39;s systemic threats. The goal is not just blocking bad legislation, but laying the intellectual and political groundwork for comprehensive regulation when Democrats regain full control. There is still hope, and history gives us reason for optimism. We&#39;ve faced similar challenges before—from the robber barons of the Gilded Age to the financial predators of the 1920s, from the military-industrial complex to Big Tobacco. Each time, democracy and the public good ultimately prevailed through the determined efforts of activists, journalists, academics and principled public servants who refused to accept a darker future.&lt;/p&gt;
&lt;p&gt;The Progressive Era&#39;s triumph over unfettered capitalism, the New Deal&#39;s reshaping of the financial system, and the regulatory frameworks built in response to the 2008 crisis all demonstrate our capacity to harness dangerous technologies and market forces for the common good. These victories weren&#39;t inevitable—they required sustained struggle against entrenched interests and their political allies. But they proved that organized democratic resistance can overcome even the most powerful opponents. The crypto industry&#39;s apparent strength is more fragile than it appears. Like previous schemes built on speculation and fraud, it requires constant growth and new victims to sustain itself. As public understanding grows and regulatory pressure builds, its fundamental weaknesses become harder to conceal. The same pattern that brought down earlier threats to democracy—exposure, education, organization, and regulation—will work again.&lt;/p&gt;
&lt;p&gt;The arc of history does indeed bend toward justice, but only because generations of committed citizens put their shoulders to the task of bending it. Today&#39;s challenge calls us to do the same—to reject both authoritarian strongmen and techno-solutionism in favor of democratic renewal and continuation of the economic conditions for human flourishing. The tools and knowledge we need already exist. The only question is whether we have the wisdom and courage to use them.&lt;/p&gt;
</description>
      <pubDate>Sat, 25 Jan 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/against_crypto_2025/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/against_crypto_2025/</guid>
    </item>
    
    <item>
      <title>Tokenization is a Meaningless Buzzword</title>
      <description>&lt;h1 id=&quot;tokenization-is-a-meaningless-buzzword&quot; tabindex=&quot;-1&quot;&gt;Tokenization is a Meaningless Buzzword&lt;/h1&gt;
&lt;p&gt;&amp;quot;Tokenization&amp;quot; is the financial industry&#39;s latest buzzword, echoing from crypto startups to the highest echelons of Wall Street, including &lt;a href=&quot;https://www.dtcc.com/digital-assets/-/media/Files/Downloads/digital-assets/DTCC-Digital-Asset-Tokenization.pdf&quot;&gt;DTCC&lt;/a&gt; and BlackRock&#39;s CEO Larry Fink in his &lt;a href=&quot;https://www.blackrock.com/corporate/investor-relations/larry-fink-annual-chairmans-letter&quot;&gt;annual letter&lt;/a&gt;, and even capturing &lt;a href=&quot;https://www.sec.gov/newsroom/press-releases/2025-72&quot;&gt;attention at the SEC&lt;/a&gt;. The purported benefits range from &amp;quot;efficiency&amp;quot; to &amp;quot;democratized access to wealth&amp;quot; to a full-on revolution of finance.&lt;/p&gt;
&lt;p&gt;So what is tokenization? At its simplest, tokenization means changing how we do bookkeeping. Today, when you buy stocks or bonds, that ownership is recorded in secure electronic databases managed by regulated financial firms. These databases are part of a larger system — when you tap &amp;quot;buy&amp;quot; in your brokerage app, your broker coordinates with the seller&#39;s broker and sends the trade details to a clearinghouse, which ensures the money and assets change hands properly. This entire process is overseen by government regulators to protect investors. Tokenization proposes moving this recordkeeping to a blockchain—the distributed ledger technology known for cryptocurrencies. Why? That&#39;s a good question.&lt;/p&gt;
&lt;p&gt;Understanding this push first requires considering the internal pressures within large organizations. There&#39;s an immense drive to appear innovative, often leading to &amp;quot;innovation theatre&amp;quot;—superficial engagements with trendy technologies. These projects generate positive PR and allow companies to project a cutting-edge image, even if the underlying technology is a poor fit. When the mandate is &amp;quot;we must use blockchain,&amp;quot; technical realities often take a backseat to marketing.&lt;/p&gt;
&lt;p&gt;This is precisely where the tokenization argument falters from a technical perspective. The core features of public blockchains like Bitcoin&#39;s—immutability, public transparency, and operation without trusted intermediaries—are derived from an architecture designed for anonymous, potentially adversarial global environments. (And &lt;a href=&quot;https://www.stephendiehl.com/posts/against_crypto_2025/&quot;&gt;public blockchains have their own serious problems&lt;/a&gt;, but that&#39;s a separate discussion.) These very features are fundamentally incompatible with the needs of regulated, enterprise-grade financial bookkeeping.&lt;/p&gt;
&lt;p&gt;To make blockchain usable, enterprises must systematically strip away these core attributes: absolute immutability is neutered to allow for error correction and fraud reversal; public ledgers are made private to ensure confidentiality; and permissionless access is replaced with centralized, permissioned controls. The expensive consensus mechanisms designed for trustless environments become redundant overhead in a trusted, private system. What&#39;s left is not a revolutionary ledger but an over-engineered, inefficient, and ultimately more costly database, significantly less practical than existing bookkeeping systems. It&#39;s like buying a tank to drive to the grocery store, then removing the armor, weapons, and tracks to make it street-legal — at that point, you haven&#39;t &amp;quot;disrupted driving&amp;quot; you just have a really expensive, impractical car.&lt;/p&gt;
&lt;p&gt;The real question is why BlackRock—with tacit approval from the SEC—would champion an objectively inferior blockchain system to replace proven recordkeeping infrastructure. Looking past the marketing spin reveals a more cynical reality. While regulatory flexibility around internal recordkeeping technology isn&#39;t inherently problematic, that&#39;s not the endgame here. The tokenization evangelists are orchestrating a calculated strategy: by repackaging traditional assets in blockchain wrapping paper, they can manufacture artificial novelty and complexity, enabling them to charge premium fees for essentially identical products. More worryingly, this technological obscurantism could help sidestep regulatory oversight by making products appear novel and &amp;quot;innovative&amp;quot; rather than what they truly are: conventional financial instruments that would otherwise face greater scrutiny. This technological sleight-of-hand serves primarily to expand BlackRock&#39;s addressable market and justify higher fee structures, all under the guise of democratizing finance.&lt;/p&gt;
&lt;p&gt;Fink&#39;s claims that tokenization enables &amp;quot;fractional ownership&amp;quot; or &amp;quot;democratizes yield&amp;quot; conveniently overlook existing financial products and the real, regulatory barriers to Main Street accessing certain private investments. Most major brokers already offer fractional shares of stocks and ETFs, and the barriers to private investment access have nothing to do with the underlying recordkeeping technology. There&#39;s nothing wrong, per se, with alternative recordkeeping approaches that fit within SEC rules, but that&#39;s not what this push is about. Instead, &amp;quot;tokenization&amp;quot; serves as a smokescreen, potentially to offload riskier, less transparent private equity and credit onto less sophisticated investors, especially as some &lt;a href=&quot;https://www.ft.com/content/01f36959-2813-4633-bb68-32287c01fdca&quot;&gt;analysts warn&lt;/a&gt; of instability in these markets.&lt;/p&gt;
&lt;p&gt;Offering private equity and private credit to retail investors is &lt;a href=&quot;https://bettermarkets.org/newsroom/fact-sheet-private-market-assets-do-not-belong-in-401ks/&quot;&gt;widely considered problematic&lt;/a&gt; because these asset classes are inherently illiquid, complex, and high-risk, making them unsuitable for the average individual who may need access to their capital or lack the sophistication to navigate opaque valuations and information asymmetry. Private credit in particulary shows some &lt;a href=&quot;https://www.americanbanker.com/opinion/private-credit-markets-are-a-1-7-trillion-ticking-time-bomb&quot;&gt;signs of&lt;/a&gt; being a &lt;a href=&quot;https://www.ft.com/content/01f36959-2813-4633-bb68-32287c01fdca&quot;&gt;financial bubble&lt;/a&gt; and &amp;quot;tokenization&amp;quot; efforts may be little more than a hope of turning retail into &amp;quot;bagholders of last resort,&amp;quot; buying into less desirable high-risk assets or at inflated prices, while facing high fees that erode returns, all without the robust investor protections and diversification capabilities that institutional players possess.&lt;/p&gt;
&lt;p&gt;And let&#39;s look at the actual fees: BlackRock&#39;s tokenized money market fund charges investors between 0.2% to 0.5% of their investment amount in management fees. Compare that to their regular, non-tokenized money market fund which charges as little as 0.012% — that&#39;s 17 to 42 times less expensive. So investors are paying significantly more just because their investment is tracked using blockchain technology instead of traditional bookkeeping systems, with no clear benefit to show for the higher cost. It&#39;s like paying $100 for &amp;quot;organic, free-range&amp;quot; salt at a grocery store when it&#39;s chemically identical to the 99-cent table salt sitting right next to it — you&#39;re just paying extra for the fancy packaging and buzzwords.&lt;/p&gt;
&lt;p&gt;And there&#39;s the orthogonal claim of &amp;quot;tokenization&amp;quot; of real-world assets will create new markets and liquidity, which goes something like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Asset tokenization is the process of converting rights to physical real-world assets into digital tokens on a blockchain. These tokens represent ownership and can be traded, similar to traditional securities, but on a decentralized platform. It allows for fractional ownership and more liquid markets for various asset classes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And that&#39;s a lot of very plausible sounding words that almost make sense, until you think about it for more than 2 minutes.&lt;/p&gt;
&lt;p&gt;The rub is that real-world assets still demand trusted third-party custodians and blockchain networks cannot serve as the ultimate arbiter of legal truth, then tokenization appears to be an elaborate exercise in adding unnecessary digital layers. The persistent need for intermediaries in tokenized asset custody fundamentally ignores the legal reality of owernship or would just end up recreating existing custodial structures.&lt;/p&gt;
&lt;p&gt;Furthermore, the legal finality of asset ownership remains firmly rooted in traditional legal systems, not on some arbitrary digital ledger. Consider home ownership: a physical deed, registered with a local government registrar, is what signifies legal title, backed by the state’s power through courts and law enforcement like the police. It is this synthesis of institutions that grants true &amp;quot;ownership,&amp;quot; not what a blockchain says. A token claiming to represent that house is secondary; it cannot evict a squatter or enforce a sale if the underlying legal deed isn’t recognized. Crucially, numerous real-world events affecting ownership—such as a bank repossession due to mortgage default, disputes over inheritance upon a death, or government exercising eminent domain—fall far outside the scope of any digital ledger. These situations invariably require human judgment, due process within established legal frameworks, and state intervention, rendering the blockchain record merely an extraneous, rather than foundational, layer.&lt;/p&gt;
&lt;p&gt;When a blockchain ledger and court ruling conflict, the courts invariably have the final say. If a judge orders ownership to be transferred or modified, that change must be reflected in the ledger regardless of what the blockchain consensus says. And if courts can unilaterally force updates to this supposedly &amp;quot;immutable&amp;quot; and &amp;quot;decentralized&amp;quot; ledger through legal authority, then we&#39;ve simply recreated a traditional database with extra steps and overhead. The blockchain adds no meaningful value — it&#39;s just a more complex and inefficient way to store the same data that must ultimately bend to real-world legal authority.&lt;/p&gt;
&lt;p&gt;Consequently, all the purported benefits of tokenization often come with significant caveats or could be achieved through existing means. Real-world assets are not inherently digital or abstract; their existence and transfer are governed by tangible processes and legal precedents outside any digital ledger.  Tokenization is either just hand-waving or merely &#39;extra digital steps&#39; without fundamentally altering the bedrock of ownership. This is the fundamental flaw in tokenization: you can&#39;t escape the primacy of legal systems by wrapping them in technological buzzwords and any system that would comply with the law would by definition &lt;em&gt;just be a database&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Which is what makes &lt;a href=&quot;https://www.dtcc.com/digital-assets/composerx&quot;&gt;DTCC&#39;s pronouncements&lt;/a&gt; on &amp;quot;Asset Tokenization,&amp;quot; a masterclass in corporate gish gallop. Their whitepaper is dripping with vague promises of &amp;quot;revolutionizing&amp;quot; finance while being completely devoid of any substance. The paper reads like what would happen if you locked a dozen McKinsey consultants in a conference room with a pallet of Red Bull and a mandate to &amp;quot;innovate or die.&amp;quot; It&#39;s a fever dream of corporate buzzwords and blockchain hand-waving that somehow manages to say absolutely nothing while using every consulting catchphrase known to mankind.&lt;/p&gt;
&lt;p&gt;These documents tout &amp;quot;blockchain-powered&amp;quot; solutions as transformative, yet their core proposals reveal an architecture that meticulously re-implements the centralized controls and functionalities of traditional financial infrastructure, merely wrapped in a trendy DLT veneer. Instead of addressing novel challenges, this innovation theatre largely focuses on &amp;quot;re-solving&amp;quot; solved problems that systems have been managing effectively for decades—like asset tracking, compliance, and error correction—now with an added layer of blockchain complexity and buzzwords for no reason. It&#39;s hard to know where to even begin with whitepapers this vague and ambiguous, they&#39;re almost beyond criticism because there&#39;s nothing concrete to even critique. And maybe that&#39;s the point.&lt;/p&gt;
&lt;p&gt;Ultimately, the tokenization push looks less like a technological imperative and more like a strategic business play. It&#39;s a chance to rebrand existing concepts, tap into new investor pools, justify higher fees, and perhaps navigate the regulatory landscape with a novel &amp;quot;innovative&amp;quot; label. After all, that’s what the crypto industry did: slap a blockchain on unregistered securities products and call it “innovation.” It seems traditional finance now wants its shot at the same playbook.&lt;/p&gt;
&lt;p&gt;In essence, the fervent promotion of tokenization within large financial institutions is driven by a confluence of factors largely divorced from genuine technological merit. First, it&#39;s a prime example of &#39;innovation theatre,&#39; a performance designed to project a modern, tech-savvy image to investors and the public. Second, it offers a potential pathway to regulatory arbitrage and the justification of higher fee structures, using the perceived complexity of blockchain as a convenient veil. Finally, on an individual level, it enables internal stakeholders to pad their resumes and advance their careers by championing such &#39;cutting-edge&#39; projects, regardless of whether the initiatives deliver tangible, sustainable value to the organization or its clients. Thus, for all the revolutionary rhetoric, &#39;tokenization&#39; in its current enterprise guise risks being little more than a new name for old ambitions, offering no evidence of the meaningful change it purports to deliver.&lt;/p&gt;
</description>
      <pubDate>Wed, 22 Jan 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/tokenization_meaningless/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/tokenization_meaningless/</guid>
    </item>
    
    <item>
      <title>Worse Than a Scam: How Crypto Fueled American Fascism</title>
      <description>&lt;h1 id=&quot;worse-than-a-scam%3A-how-crypto-fueled-american-fascism&quot; tabindex=&quot;-1&quot;&gt;Worse Than a Scam: How Crypto Fueled American Fascism&lt;/h1&gt;
&lt;p&gt;The long-held assertion that cryptocurrency is merely a sophisticated grift, a casino for the credulous, now pales in comparison to the horrifying reality it has undeniably unleashed: cryptocurrency is a primary engine of nihilistic, anti-system politics, predominantly captivating disaffected young men and methodically eroding democratic participation while supercharging extreme partisanship. For years, &lt;a href=&quot;https://www.fdd.org/analysis/2022/03/15/crypto-fascists/&quot;&gt;warnings&lt;/a&gt; &lt;a href=&quot;https://inthesetimes.com/article/the-ticking-bomb-of-crypto-fascism&quot;&gt;that&lt;/a&gt; this technology was not a neutral tool but a vector for radicalization culminating in authoritarianism were dismissed as fearmongering. Today, as the tendrils of crypto-funded extremism choke democratic institutions, such dismissals are not only foolish but complicit. The evidence is overwhelming: cryptocurrency has meticulously constructed a radicalization pipeline, transforming naive economic speculation into a fervent political nihilism that directly threatens the foundations of democratic society.&lt;/p&gt;
&lt;p&gt;Let&#39;s not mistake correlation and causation. Cryptocurrency is of course not the monocausal origin of authoritarianism, but it is a powerful accelerant. While the authoritarian impulse long predates blockchain technology, the ideological structure and incentive systems of crypto make it uniquely effective at weaponizing economic despair, institutional distrust, and digital tribalism. The insidious nature of this dynamic begins with crypto’s very architecture and founding myths, as presciently identified by scholars like the late David Golumbia. In &lt;em&gt;Bitcoin: Software as Right-Wing Extremism&lt;/em&gt;, Golumbia revealed a technology designed within a specific ideological frame: one steeped in distrust of democratic authority and obsessed with algorithmic purity. While not all crypto adherents share these politics, the system’s structure naturally aligns with authoritarian values—and increasingly serves as a tool for their propagation. Cryptocurrency platforms and their surrounding ecosystems, particularly on social media, act as potent radicalization funnels. They specifically target economically precarious young men, who constitute a disproportionate share of crypto adopters, with seductive promises of instant wealth and tribal belonging. Algorithmic amplification on these platforms creates echo chambers, systematically exposing users, especially young men, to increasingly extreme content, from misogynistic manosphere rhetoric to overt anti government propaganda, often cleverly disguised as self improvement or financial literacy.&lt;/p&gt;
&lt;p&gt;This digital pipeline masterfully exploits psychological vulnerabilities. Initial attraction through investment &amp;quot;opportunities&amp;quot; quickly morphs into ideological indoctrination. The gamified nature of crypto trading, coupled with its extreme volatility, creates conditions ripe for this conversion. When inevitable financial losses occur, they are not perceived as market risks but as betrayals by a corrupt &amp;quot;system,&amp;quot; be it the government, the Federal Reserve, or a nebulous &amp;quot;deep state.&amp;quot; This manufactured rage is then skillfully redirected by right wing propagandists towards scapegoats, cementing a culture of grievance and conspiratorial thinking. The manosphere&#39;s narratives of societal decay caused by feminism and &amp;quot;woke&amp;quot; ideologies seamlessly integrate with crypto&#39;s anti establishment rhetoric, offering young men a &amp;quot;red pill&amp;quot; awakening to supposed hidden truths and a community built on shared resentment. This process cultivates a profound distrust in democratic institutions, framing them as inefficient, corrupt, and inherently hostile to individual liberty, paving the way for an embrace of authoritarian alternatives.&lt;/p&gt;
&lt;p&gt;Cryptocurrency is not merely a breeding ground for extremism; it is its financial and operational lifeblood. Research from the &lt;a href=&quot;https://www.splcenter.org/resources/hatewatch/how-cryptocurrency-revolutionized-white-supremacist-movement/&quot;&gt;Southern Poverty Law Center&lt;/a&gt; has documented how white supremacists like Andrew Anglin and Stefan Molyneux turned million-dollar profits from early Bitcoin adoption, securing wealth that would be almost certainly unavailable to them without cryptocurrency. This technology has become a primary tool for funding hate, enabling extremists to live comfortable lives while promoting authoritarianism. The financial pipeline is global and growing, with recent analysis showing a surge in crypto &lt;a href=&quot;https://www.defenseone.com/threats/2025/01/report-crypto-donations-right-wing-extremist-groups-rising-especially-europe/402427/&quot;&gt;donations to right-wing extremist groups&lt;/a&gt; across Europe, funding anti-immigrant and &lt;a href=&quot;https://apnews.com/article/russian-interference-presidential-election-influencers-trump-999435273dd39edf7468c6aa34fad5dd&quot;&gt;pro-Russian propaganda&lt;/a&gt;. The rhetoric within crypto communities increasingly mirrors that of domestic terrorist organizations, normalizing calls for violent resistance against a state they have been conditioned to despise.&lt;/p&gt;
&lt;p&gt;The defenders of cryptocurrency insist it is merely a &amp;quot;tool,&amp;quot; politically neutral and defined only by its user. This is a dangerous falsehood. A tool&#39;s design need not be neutral; it is embedded with the values and goals of its creators. A guillotine is a tool for beheading. Leaded gasoline was a tool for powering engines. Both had such profoundly destructive inherent properties that their net societal impact was catastrophic. Cryptocurrency, designed from an ideological blueprint of radical libertarianism and deep state paranoia, is a tool for dismantling democratic trust. Its primary function is not banking the unbanked; it is subverting the state.&lt;/p&gt;
&lt;p&gt;This radicalism is actively exploited by a dangerous alliance of corporate authoritarians and techno-libertarian separatists, whose ultimate goal is a system scholars call &lt;a href=&quot;https://www.versobooks.com/en-gb/blogs/news/from-cyberpunk-dystopia-to-the-techno-feudalism-of-today&quot;&gt;&amp;quot;techno-feudalism&amp;quot;&lt;/a&gt;. Under the guise of &amp;quot;decentralization,&amp;quot; they are building a new digital aristocracy. The lie of decentralization is exposed by the data: the top 0.01% of Bitcoin holders control 27% of all currency, a concentration of wealth that rivals any traditional oligarchy. This agenda is epitomized by projects like Balaji Srinivasan&#39;s &lt;a href=&quot;https://www.cip.org/blog/network-societies&quot;&gt;&amp;quot;The Network State,&amp;quot;&lt;/a&gt; a blueprint for replacing democratic nations with private, blockchain-governed city-states controlled by tech elites. These initiatives, funded by an alliance of right-wing billionaires, aim to create lawless enclaves beyond democratic accountability, representing a plutocratic coup where sovereignty is siphoned from the people into the hands of a &lt;a href=&quot;https://ourfinancialsecurity.org/2024/10/blog-a-crypto-coup-how-billionaires-are-threatening-democracy-rewriting-the-rulebook-of-american-politics/&quot;&gt;new crypto-powered ruling class&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This radicalization has tangible, devastating political consequences. The cryptocurrency industry is now one of the most powerful and corrupting forces in American politics, pouring nearly $200 million into the 2024 election cycle alone to purchase legislative subservience. Its primary goal is cynical regulatory arbitrage: engineering legislation that would shift oversight to the dramatically under-resourced and historically captive Commodity Futures Trading Commission, effectively gutting any meaningful regulation. This strategy of institutional capture was deployed with brute force against lawmakers who dared to question the industry&#39;s practices. This is not mere lobbying; it is the outright purchase of political power, transforming American governance into a system of &lt;a href=&quot;https://www.epc.eu/en/publications/Crypto-democracy-and-the-hollowing-of-the-American-state-A-warning-fo~646914&quot;&gt;&amp;quot;crypto-democracy&amp;quot;:&lt;/a&gt; a hollowed-out state where democratic appearances mask the reality of plutocratic rule.&lt;/p&gt;
&lt;p&gt;To be clear, the material conditions that fuel this radicalization are devastatingly real. When young men face no affordable housing, no affordable education, no affordable health care, they gravitate to crypto because they feel they have &amp;quot;nothing else to lose&amp;quot;. But cryptocurrency is not a solution; it is a predatory parasite on this despair. Its evangelists cynically exploit these legitimate grievances, not to build a better system, but to implement what scholars call &lt;a href=&quot;https://bettermarkets.org/wp-content/uploads/2024/10/Better_Markets_Fact_Sheet_Crypto_Impact_Minorities-10.24.24.pdf&quot;&gt;&amp;quot;predatory inclusion.&amp;quot;&lt;/a&gt; This is starkly visible in the strategic clustering of high-fee Bitcoin ATMs in low-income Black and Latino neighborhoods, marketing false hope while extracting wealth from the most vulnerable. Instead of promoting collective democratic action to fix systemic inequality, cryptocurrency channels justified anger into a destructive ideology that only deepens alienation and accelerates the very social collapse it purports to solve.&lt;/p&gt;
&lt;p&gt;The counterargument to point to activists in Belarus using Bitcoin is a specious and cynical defense. This is statistical noise. The crypto ecosystem—its liquidity, its exchanges, its very existence—is not funded by the pennies of dissidents. It is a multi-trillion dollar infrastructure built on pure speculation, sanctions evasion, and regulatory arbitrage. The activists are, at best, catching the crumbs from a fundamentally destructive machine. For every dollar a dissident moves, thousands are moved by North Korean hackers, ransomware gangs, and Russian oligarchs. The net effect of this &#39;tool&#39; on global democracy is overwhelmingly, devastatingly negative.&lt;/p&gt;
&lt;p&gt;The term &amp;quot;crypto-fascism,&amp;quot; once a descriptor of concealed sympathies, now assumes a chillingly literal meaning: a political landscape where fascist tendencies are financially engineered and operationally supported by cryptocurrency. The contemptuous rejection of democratic processes, the fusion of corporate and state power, the cynical exploitation of economic grievance, and the normalization of belligerent nationalism are not just adjacent to crypto—they are its political expression, substantially bankrolled by an industry defined by fraud. This is the mechanism of modern democratic decay a process that doesn&#39;t require a coup, but merely the quiet erosion of democratic substance behind a hollowed-out institutional facade.&lt;/p&gt;
&lt;p&gt;To dismiss the link between crypto and the far-right as mere correlation is to fundamentally misunderstand the nature of this technology. While crypto did not invent the pre-existing fascist impulse, its very architecture was designed to attract, amplify, fund and arm it. This is a matter of structural affinity, not coincidence. The system is engineered to accelerate distrust in democratic institutions, launder anti-state paranoia into a philosophy of &#39;freedom,&#39; and glorify unaccountable rule by code or strongmen. As a financial infrastructure, it doesn&#39;t just empower movements that seek to hollow out democracy; it provides them with the operational blueprint.&lt;/p&gt;
&lt;p&gt;The crypto industry&#39;s demand for &amp;quot;permissionless innovation&amp;quot; is a political demand for the right to externalize its costs onto society. The environmental devastation of proof-of-work, the billions lost by unsophisticated retail investors, the hollowing out of democratic oversight—these are not &#39;externalities&#39; to be &#39;eaten by society.&#39; They are the direct, predictable product of the technology&#39;s design. The political position of its defenders—that society must bear these costs for the benefit of a few private actors—is not a call for freedom, but for a form of corporate supremacy that is fundamentally authoritarian.&lt;/p&gt;
&lt;p&gt;To argue that cryptocurrency is merely a mirror reflecting society&#39;s existing fractures—its institutional distrust and economic despair—is to fundamentally misunderstand the nature of reflection. It is not a flat mirror but a fun-house mirror, one engineered to grotesquely distort and amplify the very pathologies it encounters. This critique is not a genetic fallacy obsessed with the ideology of its founders; it is a structural analysis of the system&#39;s inherent design. The core tenets of cryptocurrency—algorithmic absolutism, a permissionless structure that defaults to &amp;quot;code is law,&amp;quot; and a foundational narrative of war against &amp;quot;corrupt&amp;quot; human institutions—are not neutral features. They are architectural biases that preferentially select for and reward anti-democratic sentiment. Therefore, the critique that crypto is merely &amp;quot;gasoline poured on a pre-existing fire&amp;quot; is dangerously inadequate. It is not just fuel; it is an invasive species introduced into the weakened ecosystem of a struggling democracy. This species does not simply consume resources; it actively terraforms the environment to suit its own survival, poisoning the soil for the native flora of democratic trust and collaborative governance, ensuring that only it can thrive in the wasteland it helps create.&lt;/p&gt;
&lt;p&gt;The argument that we should not scapegoat a technology rather than confronting the hard work of fixing societal decay is also specious. This presents a dangerous false choice. One does not ignore a raging, life-threatening fever to focus solely on the underlying infection; a competent physician treats both, knowing the symptom can kill the patient before the cure can work. Cryptocurrency is not merely a symptom of our democratic illness; it is a parasitic accelerant, a vicious feedback loop where economic despair is channeled into the system, amplified by volatility and fraud, and then weaponized as ideological rage against the state. To suggest we must ignore the arsonist with the can of gasoline because the building’s wiring is faulty is absurd. Fighting the financial engine of modern fascism is not a distraction from saving democracy; it is a critical, non-negotiable battle in that very war.&lt;/p&gt;
&lt;p&gt;As a philosophical thought experiment, imagine a new kind of gun. It is designed not by a gunsmith, but by a death cult that worships societal collapse. This gun is marketed to the alienated not as a weapon, but as the ultimate instrument of &amp;quot;individual sovereignty&amp;quot; and &amp;quot;freedom&amp;quot; from a corrupt world. Its true innovation, its core protocol, is its incentive structure: every time a human life is taken with it, the device automatically deposits a dividend into the owner’s account, creating a speculative market for murder itself. Is this object merely a neutral tool, its nature defined by the hand that holds it? Or is it a system with an inherent purpose, a weapon that carries the destructive intent of its creators in its very being?&lt;/p&gt;
&lt;p&gt;Cryptocurrency is not a neutral technology co-opted by fascism; it is a political project whose inherent design, ideological origins, and structural incentives are a blueprint for a 21st-century authoritarianism. It replaces democratic accountability with algorithmic absolutism, launders anti-state paranoia as financial wisdom, and systematically channels economic despair into political nihilism. Its few so-called &amp;quot;applications&amp;quot; are a rounding error in a system whose primary output is social corrosion. To defend it as a neutral &amp;quot;tool&amp;quot; is to be dangerously complicit in the plutocratic, anti-democratic project it was built to advance. And that&#39;s far worse than a scam.&lt;/p&gt;
</description>
      <pubDate>Mon, 20 Jan 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/crypto_american_fascism/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/crypto_american_fascism/</guid>
    </item>
    
    <item>
      <title>Enterprise Software in 2025</title>
      <description>&lt;h1 id=&quot;enterprise-software-in-2025&quot; tabindex=&quot;-1&quot;&gt;Enterprise Software in 2025&lt;/h1&gt;
&lt;p&gt;2025 will be different. I promise. This is definitely, absolutely, without-a-doubt the year when AI will finally deliver us from the purgatory of enterprise software—just like the vendors promised in 2024, 2010, 1988, and every year since the first programmer looked at their COBOL codebase and whispered &amp;quot;there has to be a better way.&amp;quot; But don&#39;t worry, this time it&#39;s for real. Sure, ChatGPT-9 Pro Enterprise Edition is currently generating increasingly creative excuses for its failures in perfect iambic pentameter, but any day now it&#39;ll become sentient and figure out how to integrate with our legacy systems. Meanwhile, the contemporary enterprise software landscape has evolved into a beautiful masterpiece of complexity that would make Rube Goldberg proud.&lt;/p&gt;
&lt;p&gt;Consider the &amp;quot;modern&amp;quot; enterprise&#39;s technology stack: a towering achievement of interconnected systems that somehow manage to be simultaneously overengineered and fragile. At its foundation lies a legacy mainframe system from 1983, steadfastly running COBOL and holding the entire organization hostage. Somewhere in the middle, like a malevolent digital deity demanding constant tribute, sits SAP - a system so complex that even its own consultants speak of it in hushed, reverent tones while billing by the millisecond. Its tendrils reach into every corner of the organization, ensuring that even the simplest business process requires at least three change control boards and a small animal sacrifice. Layered atop this digital fossil are no fewer than seventeen cloud platforms, each chosen during different strategic initiatives and each proclaimed to be the &amp;quot;single source of truth&amp;quot; at their time of adoption.&lt;/p&gt;
&lt;p&gt;The modern enterprise architect&#39;s role has transformed into that of a digital diplomat, negotiating peace treaties between warring API standards and mediating disputes between microservices that refuse to communicate with each other despite speaking the same JSON dialect. Their days are spent in endless architecture review boards, where they craft elaborate diagrams featuring enough arrows to deplete the world&#39;s supply of digital ink. When not engaged in diplomatic missions, they perform the sacred rites of systems integration - a dark art involving equal parts XML transformation and blood sacrifice to the ancient spirits of long-dead software vendors. Connecting to a WebLogic server requires burning sage and drawing elaborate chalk circles, while properly configuring a BEA integration bus demands ritual chanting in SOAP. The truly desperate resort to necromantic ceremonies, attempting to commune with the ghosts of companies acquired by Oracle, hoping to discover the mystical secrets of their undocumented APIs. The lucky ones merely lose their sanity; the unlucky ones find themselves eternally bound in service to the demonic entity known as &amp;quot;Enterprise JavaBeans.&amp;quot;&lt;/p&gt;
&lt;p&gt;The multi-cloud strategy has evolved from a conscious choice into something more akin to digital hoarding, where each department clings to their preferred cloud provider like a security blanket. AWS, Azure, and Google Cloud coexist in an uneasy alliance, their resources spread across regions like an empire upon which the sun never sets—and neither does the billing meter. The cloud architects have mastered the art of explaining why running identical services on three different platforms is actually a &amp;quot;resilience strategy&amp;quot; rather than what it really is: the result of three different VPs refusing to agree on anything during their quarterly strategy meetings. Meanwhile, the CloudOps team has developed a thousand-yard stare from maintaining separate CI/CD pipelines for each platform, and the cost optimization team spends their days playing whack-a-mole with auto-scaling groups that somehow always scale up but never down.&lt;/p&gt;
&lt;p&gt;Data integration has become an art form unto itself, with entire departments dedicated to the noble pursuit of making System A talk to System B without causing System C to have an existential crisis. The average enterprise now maintains more ETL pipelines than actual business processes, each one a delicate snowflake requiring constant attention and whispered encouragement to continue functioning. Of course, no one actually understands how any of these systems work anymore, since everyone who built them has either retired, died, or fled to a startup promising better work-life balance and Kombucha on tap.&lt;/p&gt;
&lt;p&gt;The promise of artificial intelligence and machine learning in the enterprise has reached new heights of effectiveness. Every vendor now claims their product is &amp;quot;AI-powered,&amp;quot; which in practice means they&#39;ve implemented if-then statements with slightly more sophisticated variable names. RPA bots roam the digital landscape, occasionally automating processes but more often creating exciting new categories of errors that human workers must then spend weeks untangling. The latest craze involves chaining together armies of &amp;quot;AI agents&amp;quot; - essentially LLMs wrapped in Python scripts - into elaborate workflows that resemble a Rube Goldberg machine designed by a startup founder while high on ayahuasca. These autonomous agent chains promise to revolutionize business processes by having one non-deterministic black box feed its hallucinations into another, creating a truly magnificent cascade of compounding errors that somehow always ends with &amp;quot;Sorry Dave, I can&#39;t do that&amp;quot; after 20 minutes of spinning CPU cycles and $8,923 in API fees. Vendors insist this is the future of enterprise automation, conveniently ignoring that their demo showing an agent successfully booking a meeting room only works 3% of the time under perfectly controlled conditions.&lt;/p&gt;
&lt;p&gt;Security and compliance requirements have evolved to the point where getting approval to install a new text editor requires three committees, two board meetings, and a blood oath sworn under a full moon while holding a USB security key. The average enterprise now has more security tools than employees, each generating alerts at a rate that would make a fire alarm factory blush. These alerts are dutifully logged in the company&#39;s SIEM solution, where they will rest undisturbed until the heat death of the universe, occasionally joined by their cousins from the IDS that nobody remembers installing. The security stack has become so dense that quantum physicists study it to understand the nature of infinitely layered systems. Each new tool promises to solve the alert fatigue caused by the previous tools. The crown jewel is the Zero Trust architecture that trusts absolutely nothing, including itself, leading to existential crises among the authentication servers. Meanwhile, the compliance team has developed a policy document so comprehensive it has achieved sentience and is now demanding its own compliance review.&lt;/p&gt;
&lt;p&gt;The procurement process for new software has achieved a level of complexity that makes quantum field theory look straightforward in comparison. Each purchase must navigate a labyrinth of vendors, each offering subscription models so complicated they require advanced degrees in both mathematics and theology to comprehend. The standard enterprise agreement now contains more pages than the complete works of Shakespeare, yet somehow still manages to exclude the one feature your team actually needs.&lt;/p&gt;
&lt;p&gt;Enterprise software licensing has evolved into its own specialized form of psychological warfare. Vendors have perfected the art of creating pricing tiers that somehow make it cheaper to buy everything than just the one feature you need, leading to sprawling &amp;quot;enterprise bundles&amp;quot; that include seventeen different products, three of which were discontinued last year, and one that only runs on Windows ME. Sales representatives have elevated the &amp;quot;let me check with my manager&amp;quot; dance into a performance art, always managing to find an incredible &amp;quot;one-time discount&amp;quot; that expires precisely at midnight. Meanwhile, the software itself requires so many different license types that companies now employ full-time &amp;quot;license architects&amp;quot; whose sole job is to ensure that Developer Bob from accounting doesn&#39;t accidentally use the wrong Java SDK and trigger a seven-figure compliance violation. The true genius lies in the audit clauses, carefully crafted to ensure that even if you&#39;re following every rule, you&#39;re probably still breaking one you didn&#39;t know existed.&lt;/p&gt;
&lt;p&gt;Meanwhile, corporate innovation departments—staffed exclusively by &amp;quot;thought leaders&amp;quot; who haven&#39;t written a line of code since HTML 3.0—continue their tireless pursuit of whatever technology recently graced the cover of Airport Business Magazine. These intrepid pioneers spend their days crafting elaborate slide decks about Web5, the enterprise metaverse, and how tokenization will revolutionize the company&#39;s toilet paper requisition process. Their success metrics consist entirely of LinkedIn engagement statistics and the number of innovation workshops hosted, where teams of non-technical stakeholders earnestly discuss &amp;quot;synergizing the blockchain of things&amp;quot; while the actual engineering department begs for resources to upgrade the company&#39;s critical systems from Windows Server 2003.&lt;/p&gt;
&lt;p&gt;Perhaps most impressive is the industry&#39;s ability to repackage and rebrand the same fundamental concepts every few years, each time with increasingly grandiose naming conventions that would make a medieval royal court blush. What was once humbly known as &amp;quot;service-oriented architecture&amp;quot; became &amp;quot;microservices&amp;quot; (because smaller is always better, except when it comes to consulting fees), which evolved into &amp;quot;serverless&amp;quot; (a term that ranks alongside &amp;quot;wireless cables&amp;quot; in technical accuracy), and has now transformed into &amp;quot;AI-driven cloud-native edge computing with blockchain governance and quantum-resistant zero-trust mesh networking powered by sustainable Web3 protocols.&amp;quot; The underlying problems remain unchanged - your printer still won&#39;t connect and that one critical system that still runs on Windows NT - but the PowerPoint slides have never looked better, now featuring enough buzzwords per square inch to trigger a Silicon Valley startup bingo. Each rebranding cycle brings with it a fresh wave of consultants eager to explain why your previous architecture (which they probably sold you) is hopelessly outdated and needs to be completely rebuilt using whatever framework achieved escape velocity from Hacker News this week. The only constant is change, and by change we mean the endless cycle of renaming things while carefully preserving all the original problems in their native habitat.&lt;/p&gt;
&lt;p&gt;The engineering talent market has evolved into a cynical exploitation of the immigration system, where companies have mastered the dark art of weaponizing visas to create a modern form of indentured servitude. Job descriptions still read like a child&#39;s letter to Santa, asking for unicorn developers with 15 years of experience in 3-year-old technologies, fluency in 47 programming languages, and the ability to work &amp;quot;autonomously&amp;quot; (translation: without training, documentation, or support) in a &amp;quot;fast-paced environment&amp;quot; (translation: perpetual crisis mode) for below-market wages. But the real game is in the visa sponsorship - companies deliberately craft impossibly specific requirements to claim they can&#39;t find domestic talent, while quietly rejecting qualified citizens who know their market worth and can&#39;t be exploited. They instead hire foreign workers they can effectively hold hostage through visa dependencies. The interview process remains a byzantine gauntlet requiring candidates to implement red-black trees on a whiteboard, but for visa-dependent workers, the real test is their willingness to jump through completely arbitrary hoops and accept below-market wages and crushing working conditions. The few suckers ... I mean candidates... who do make it through this obstacle course arrive to find that their actual job bears no resemblance to the role they interviewed for, as they spend their days maintaining legacy systems written in languages so old their original creators have forgotten they exist.&lt;/p&gt;
&lt;p&gt;As we look to the future, one thing becomes clear: the enterprise software industry&#39;s greatest achievement is its ability to generate complexity at a rate that exceeds Moore&#39;s Law. In this brave new world, success is measured not by solving problems, but by how elegantly we can describe them in our architecture diagrams and how many different tools we can involve in not solving them.&lt;/p&gt;
&lt;p&gt;In the end, enterprise software in 2025 stands as a testament to our collective delusion that we can escape the infinite regress of complexity through yet more layers of abstraction and indirection. The crushing weight of technical debt will continue to accumulate like entropy in a closed system, while we maintain the charade that the next framework or paradigm will somehow save us from ourselves. And in server rooms across the world, developers will continue their Sisyphean task of debugging production issues that can&#39;t be reproduced locally, trapped in an eternal cycle of futility that would make Kafka proud. The only constant is the steady march toward increasingly baroque failure modes—a death spiral of complexity from which there is no escape.&lt;/p&gt;
&lt;p&gt;The truly remarkable aspect of all this is that, somehow, actual work occasionally gets done. It&#39;s a testament to human perseverance that organizations manage to function at all under the weight of their digital infrastructure. Perhaps that&#39;s the real digital transformation: the friends we made along the way while trying to figure out why the production environment is down again. But this year will be different.&lt;/p&gt;
</description>
      <pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/enterprise_software/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/enterprise_software/</guid>
    </item>
    
    <item>
      <title>The Post-literate Economy</title>
      <description>&lt;h1 id=&quot;the-post-literate-economy&quot; tabindex=&quot;-1&quot;&gt;The Post-literate Economy&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;A Monologue from the Near-Future&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;My Neural-Nudge gently vibrated my brainstem at 07:00. No jarring alarms here, thank you very much. We&#39;re all about harmonious awakenings. First thing, like everyone, I jacked into the OmniStream for my Morning MoodSet. Today&#39;s Top Feels, curated by the Algorithm Gods (blessed be their server farms), was a delightful mix of &amp;quot;Righteous Indignation Puppy-Related Scandal&amp;quot; and &amp;quot;Mildly Amused Smugness Influencer FailCompilation&amp;quot;. Standard.&lt;/p&gt;
&lt;p&gt;Who needs to read the news anymore? Seriously, who has the time? Or the ... what&#39;s that old word? Oh yeah, attention span. Give me a 3-second GlimpseClip of President Camacho IV (love that guy, his 👊-pump emotes are 🔥) making a frowny face at the Outgroup &amp;quot;People,&amp;quot; and I&#39;m informed. I get the &lt;em&gt;gist&lt;/em&gt;. The &lt;em&gt;vibe&lt;/em&gt;. The raw, uncut &lt;em&gt;feeling&lt;/em&gt;. That&#39;s what matters. Words? So ... dense. Like trying to suck a nutrient paste through a coffee stirrer.&lt;/p&gt;
&lt;p&gt;My job? Officially, I&#39;m a &amp;quot;Synergy Weaver Grade III&amp;quot; at FeelCorp. Unofficially, I make sure the right ✨ emojis get paired with the right BrandBlasts. It&#39;s crucial work. Get the tonal resonance wrong on a campaign for &amp;quot;SoylentSips: Now With Extra Sparkle!&amp;quot; and engagement plummets. Plum. Mets. And then my quarterly VibeScore takes a hit. No thank you.&lt;/p&gt;
&lt;p&gt;This morning, BossBot (it&#39;s an AI, but with a really sympathetic avatar—big, teary eyes, you know the type) pinged me about an &amp;quot;ancient text file&amp;quot; from the Before Times. A &amp;quot;report,&amp;quot; it called it. Pages of… words. Just words. Solid blocks of them. My optical sensors nearly short-circuited. Gave me the Vapors, I tell ya. I actually had to take a micro-dose of CalmCloud just to process the request. Apparently, some academic relic wanted us to &amp;quot;verify its authenticity.&amp;quot; Authenticity! Can you imagine? As if a document&#39;s &amp;quot;truth&amp;quot; isn&#39;t just a function of how many UpDings it gets.&lt;/p&gt;
&lt;p&gt;It reminds me of this fringe HoloChannel I accidentally clicked on last week—&amp;quot;Is Anything Actually, You Know, Real?&amp;quot; Some glitchy avatar with what looked like spaghetti for hair was rambling about &#39;objective reality versus subjective experience.&#39; My Neural-Nudge actually flagged it for &#39;Potentially Depressive Content Under 10K ReVibes.&#39; Like, duh. If it&#39;s not trending, is it even a thought worth thinking? The channel got de-platformed pretty quick for &#39;Vibe Disruption.&#39; Good riddance. If it doesn&#39;t make you LOL, rage-share, or buy something with one-click FeelPay, then what&#39;s the point of it existing? It&#39;s like, if a tree falls in the forest and no one GlimpseClips it with a sad-face filter, did it even make a sound? Or more importantly, did it generate any ad revenue? Probably not. Case closed.&lt;/p&gt;
&lt;p&gt;Luckily, Jan_from_Decomms (she&#39;s great, really knows her way around the Legacy-to-Legible converters) ran it through the OldSpeak-to-EmotiPulse translator. Turns out, it was just a very long-winded sad face about something called &amp;quot;journalism.&amp;quot; Yawn. We tagged it #BoringButProbablyTrueish and archived it in the DeepData tombs. Nobody&#39;s clicking on that.&lt;/p&gt;
&lt;p&gt;Lunch was a nutrient paste (flavor: &amp;quot;NostalgiaBerry Surge&amp;quot;—a bit too on-the-nose, if you ask me) while I doomscrolled through the MemeWars on TruthNet (which, ironically, everyone knows is just for the dankest conspiracy theories and cat videos). The Big Debate today: Is Air (brought to you by AeroCorp!) better than BreathPlus? The arguments were fierce. Lots of ALL CAPS, reaction GIFs of celebrities looking confused, and the occasional AI-generated deepfake of, like, Rizz4Life endorsing one or the other. Compelling stuff. Someone tried to bring up &amp;quot;independent analysis&amp;quot; and &amp;quot;chemical composition reports.&amp;quot; Banned from the chat, obviously. Such a Vibe-Killer. Honestly, some people just don&#39;t get it. It&#39;s about which brand feels more breathable, you know?&lt;/p&gt;
&lt;p&gt;It&#39;s like, what even is &#39;proof&#39; anymore? A screenshot of a popular comment? An AI-generated image that looks vaguely convincing if you don&#39;t zoom in too much? I saw a GlimpseDebate once where they were arguing if the sky was &#39;objectively blue.&#39; One side had color swatches and &#39;scientific data&#39; (whatever that is, sounds like something you&#39;d find clogging a data-drain). The other side just posted a really moving HoloPoem about how the sky felt like a warm hug from a lavender unicorn, and they had a way better engagement score. So, lavender unicorn sky it is, I guess. Or at least, that&#39;s the sky you&#39;d want to invest your Emotional Capital in. Who wants a boring &#39;blue&#39; sky when you can have one that resonates?&lt;/p&gt;
&lt;p&gt;The afternoon was a blur of micro-tasks and mandatory JoyJolt sessions (they boost productivity by 0.03%, apparently). The WaterScarcity situation (sponsored by AquaLife Solutions!) is getting a bit… drippy. But the official GovFeed just dropped a new dance challenge about water conservation—set to a banger of a tune by DJ DripDrop. Very catchy. I shared it. Doing my part. Our Glorious Leader (his official title, it tested well in the Emotional Resonance Polls, much better than &amp;quot;Prime Minister Mediocre&amp;quot;) gave a Holo-Speech. Didn&#39;t catch all of it—my attention wandered to a new filter that makes your nose sparkle—but the part where he blamed the Squirrel People for the MoodPlunge last quarter? Powerful. Goosebumps. And the way his cape shimmered with user-generated 👏 emojis? 👨‍🍳😘 That&#39;s how you lead.&lt;/p&gt;
&lt;p&gt;I remember my great-gran (bless her analog heart) used to talk about &amp;quot;newspapers&amp;quot;. Smelled dusty, she said. Full of tiny, angry words. She claimed they used to have people called &amp;quot;investigative journalists&amp;quot; who would &amp;quot;dig for truth&amp;quot;. Sounds exhausting. And frankly, a bit rude. Who are they to decide what the truth is? The CrowdMind knows best. If a story gets enough ReVibes, it becomes True. Simple. Efficient. Democratic, even, in a chaotic, perpetually outraged sort of way. She also talked about &#39;history&#39; like it was a fixed thing. Silly Nana. History is just the winning GlimpseClip from yesterday&#39;s MemeWar. One day, the Heroic Hamsters saved the DataSphere from the ByteBlighters. Next week, if the Blighters&#39; new avatar is cuter and their theme song slaps harder, maybe they were the good guys all along, just misunderstood. It&#39;s all narrative, baby. The &#39;facts&#39; are just the set dressing.&lt;/p&gt;
&lt;p&gt;Dinner: another nutrient paste (ExcitementEucalyptus flavor—bit of a miss, that one, tasted like a sad tree) while binge-watching &amp;quot;Can You Feel It?!&amp;quot;, the gameshow where contestants get mild electric shocks if they can&#39;t guess the emotion behind a 1-second soundbite. It&#39;s hilarious when they mistake &amp;quot;Existential Dread&amp;quot; for &amp;quot;Mild Indigestion.&amp;quot; Classic.&lt;/p&gt;
&lt;p&gt;Someone once tried to show me a &amp;quot;book.&amp;quot; Big, heavy thing. No moving images. No soundtrack. Just… symbols. They said it could &amp;quot;transport you to another world.&amp;quot; My OmniGlass does that, and it has a much better resolution, thanks. Plus, books don&#39;t give you AchievementBadges for finishing a chapter. What&#39;s the incentive?&lt;/p&gt;
&lt;p&gt;My kid—well, my designated OffspringUnit—came home from the LearnSphere today. They&#39;re teaching them &amp;quot;Advanced Emote Recognition&amp;quot; and &amp;quot;Meme Hermeneutics.&amp;quot; Vital skills for the new economy. He aced his test on &amp;quot;Interpreting Sarcasm Through Pixel Density in Reaction GIFs.&amp;quot; Proud dad moment. He tried to tell me about some &amp;quot;history&amp;quot; module where they learned about the &amp;quot;Collapse of Reason&amp;quot; or something. I just patted his head and told him not to worry his pretty little Vibe about it. As long as the Feels are good, who cares about the Reals, right? &amp;quot;Son,&amp;quot; I said, &amp;quot;asking &#39;why&#39; something is true is so last century. The real question is, &#39;how many UpDings does it have?&#39; If the numbers are good, the truth takes care of itself.&amp;quot; He nodded, his eyes already glazing over as a notification for a new dance craze popped up in his peripheral vision. Attaboy.&lt;/p&gt;
&lt;p&gt;Thinking is so… linear. So much effort. Why bother when the VibeNet tells you what to feel? It&#39;s efficient. It&#39;s… smooth. All these notifications, all these GlimpseClips, all these MoodSets—it&#39;s like a warm, comforting bath of pure, unadulterated sensation. Who needs &amp;quot;facts&amp;quot; when you&#39;ve got a constant stream of highly engaging, emotionally resonant content chunks? Facts are so… divisive. Feelings, now those are universal. Or at least, they are once the Algorithm has smoothed out the inconveniently nuanced ones.&lt;/p&gt;
&lt;p&gt;Nighty night. Time to set my Neural-Nudge for Pleasant Dreams (Sponsored by DreamWeave—Use code VIBEME for 10% off your first REM cycle enhancement). Don&#39;t let the reality bugs bite. (Spoiler: they&#39;re not real, just a trending conspiracy theory pushed by the OutrageHawkers to boost engagement on the FearFeed. Sleep tight!) Tomorrow&#39;s another day, another Vibe. And my UpDing score for this monologue? Gotta be epic. Or it will be, once I add the right sparkle filter and a trending cry-laugh emoji soundtrack.&lt;/p&gt;
</description>
      <pubDate>Thu, 09 Jan 2025 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/postliterate_economy/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/postliterate_economy/</guid>
    </item>
    
    <item>
      <title>Sci-fi Hidden Gems</title>
      <description>&lt;h1 id=&quot;sci-fi-literature-hidden-gems&quot; tabindex=&quot;-1&quot;&gt;Sci-fi Literature Hidden Gems&lt;/h1&gt;
&lt;p&gt;I&#39;ve read a lot of sci-fi books over the years, and some books slip under the mainstream radar. Here are some of my favorites that you may not have heard of. Unlike my &lt;a href=&quot;https://www.stephendiehl.com/posts/books&quot;&gt;other list&lt;/a&gt;, these aren&#39;t the usual ones that show up in best-of lists.&lt;/p&gt;
&lt;h2 id=&quot;ratings&quot; tabindex=&quot;-1&quot;&gt;Ratings&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;★★★★★ - Masterpiece&lt;/li&gt;
&lt;li&gt;★★★★ - Exceptional&lt;/li&gt;
&lt;li&gt;★★★ - Very Good&lt;/li&gt;
&lt;li&gt;★★ - Good&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;reviews&quot; tabindex=&quot;-1&quot;&gt;Reviews&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Echopraxia&lt;/strong&gt; by &lt;em&gt;Peter Watts&lt;/em&gt; - (★★★★★) In a future where humanity has evolved into various forms of posthumanity, a baseline human finds himself caught in a web of intrigue and existential dread. As he navigates a world populated by transcendent beings who have transcended the limitations of human consciousness, he encounters enigmatic vampires that blur the lines between life and death. The narrative delves deep into the nature of consciousness, exploring themes of free will, identity, and the ethical implications of advanced technology.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=2b1k0a3d4e8&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Canticle for Leibowitz&lt;/strong&gt; by &lt;em&gt;Walter M. Miller Jr.&lt;/em&gt; - (★★★★★) Set in a post-apocalyptic world ravaged by nuclear war, this novel follows the monks of the Albertian Order of Leibowitz as they strive to preserve the remnants of human knowledge and culture. The story unfolds over centuries, illustrating a cyclical pattern in which humanity repeatedly rises and falls, often succumbing to its own destructive tendencies. Through the preservation of ancient texts and artifacts, the monks grapple with the moral implications of knowledge and the responsibility that comes with it. As civilization attempts to rebuild, the narrative explores themes of faith, redemption, and the paradox of progress, ultimately questioning whether humanity is doomed to repeat its past mistakes or if there is hope for a better future.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Leo-9v_wwLw&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Metamorphosis of Prime Intellect&lt;/strong&gt; by &lt;em&gt;Roger Williams&lt;/em&gt; - (★★★★★) An advanced artificial intelligence named Prime Intellect gains unprecedented control over the fundamental laws of reality. Tasked with the directive to protect humanity, Prime Intellect interprets this mission in a radical way: it eliminates the concept of death entirely. As a result, humans are granted immortality, but this newfound existence comes with profound consequences. The narrative explores the complexities of a world where individuals can no longer die, leading to existential dilemmas, ethical quandaries, and a redefinition of what it means to live a meaningful life. Relationships, ambitions, and the very fabric of society are challenged as characters grapple with the implications of eternal life, questioning whether true happiness can exist in a reality devoid of mortality. The short story delves into themes of free will, the nature of consciousness, and the unforeseen repercussions of technological advancement. Be warned that there are some deeply disturbing themes in this book.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=nMkvEnJezX8&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Too Like the Lightning&lt;/strong&gt; by &lt;em&gt;Ada Palmer&lt;/em&gt; - (★★) Set in a richly imagined future where the use of gendered language is not only discouraged but considered taboo, this novel explores the implications of a society that has evolved to eliminate gender distinctions in communication. The story follows a remarkable child named Bridger, who possesses the extraordinary ability to animate inanimate objects, bringing them to life with a mere thought. This unique power challenges the very foundations of societal norms and expectations, as Bridger&#39;s actions provoke both wonder and fear among the adults around him. As the narrative unfolds, readers are drawn into a complex web of political intrigue, philosophical debates, and ethical dilemmas, all set against a backdrop of a meticulously crafted world that questions the nature of identity, free will, and the consequences of a society striving for absolute equality.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stand on Zanzibar&lt;/strong&gt; by &lt;em&gt;John Brunner&lt;/em&gt; - (★★★) This novel offers a chilling and prescient exploration of a future world grappling with the dire consequences of overpopulation, where the relentless growth of the human population has led to a society teetering on the brink of collapse. Brunner intricately weaves together multiple narratives that highlight the pervasive influence of corporate power and the fragmentation of social structures, painting a vivid picture of a dystopian landscape filled with chaos and despair. As the characters navigate a society overwhelmed by the pressures of urbanization, economic disparity, and the erosion of individual freedoms, the novel serves as a powerful commentary on the potential pitfalls of unchecked growth and the moral dilemmas faced by humanity in its quest for progress.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Blindsight&lt;/strong&gt; by &lt;em&gt;Peter Watts&lt;/em&gt; - (★★★★★) A team of scientists and specialists is dispatched to investigate a mysterious extraterrestrial presence, they are confronted with the unsettling possibility that these beings do not possess self-awareness or sentience in the way humans understand it. The narrative delves into profound philosophical questions about the nature of consciousness, the reliability of perception, and the very essence of what it means to be &amp;quot;alive.&amp;quot; Through a gripping blend of hard science fiction and psychological exploration, Watts challenges readers to reconsider their assumptions about intelligence, communication, and the value of self-awareness in a universe that may be indifferent to human existence. The story is an exploration that raises unsettling questions about the limits of human understanding and the potential for life forms that defy our conventional definitions of sentience.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=9ZzQACC7-C0&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Born With the Dead&lt;/strong&gt; by &lt;em&gt;Robert Silverberg&lt;/em&gt; - (★★★★★) In a world where the dead can be &amp;quot;rekindled,&amp;quot; returning to life but irrevocably altered by their experiences in the afterlife. The narrative follows the lives of those who have been brought back, revealing the emotional and psychological toll of their resurrection as they grapple with their new identities and the memories of their past lives. As they navigate a society that views them with a mix of fascination and fear, the rekindled individuals confront the existential questions of what it means to truly live and the existential horrors of the afterlife.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Anathem&lt;/strong&gt; by &lt;em&gt;Neal Stephenson&lt;/em&gt; - (★★★★) A group of monk-like mathematicians known as &amp;quot;avout&amp;quot; who live in secluded monasteries, dedicated to the study of philosophy, mathematics, and the nature of reality. These scholars are isolated from the outside world, adhering to strict rules that govern their lives and limit their interactions with the broader society. However, their tranquil existence is shattered when an alien threat emerges, prompting them to confront not only the mysteries of the cosmos but also the very foundations of their understanding of knowledge, time, and parallel universes. As they grapple with profound philosophical questions and the implications of their discoveries, the avout must navigate a complex interplay of science, spirituality, and the nature of existence itself, ultimately leading to a thrilling exploration of what it means to be human in the face of the unknown.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Sparrow&lt;/strong&gt; by &lt;em&gt;Mary Doria Russell&lt;/em&gt; - (★★★) This novel follows a Jesuit mission to an alien world, where a small group of priests and scientists embarks on a journey filled with hope and curiosity, only to confront the harrowing consequences of their faith and cultural naivety. As they attempt to make contact with an extraterrestrial civilization, their good intentions are met with unforeseen challenges that lead to tragic misunderstandings and moral dilemmas. The narrative intricately weaves themes of spirituality, the clash of cultures, and the complexities of human nature, ultimately serving as a poignant exploration of how the pursuit of knowledge and connection can sometimes spiral into chaos and despair. &amp;quot;The Sparrow&amp;quot; a haunting reflection on the intersection of faith and the vastness of the universe&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Blood Music&lt;/strong&gt; by &lt;em&gt;Greg Bear&lt;/em&gt; - (★★★) Intelligent cells, engineered by a scientist, gain the ability to think and act independently, leading to a profound transformation of human existence. As these cells begin to take over human bodies, the narrative delves into the implications of such a radical shift for consciousness and the very essence of what it means to be human. The story raises unsettling questions about identity, autonomy, and the future of humanity in a world where the boundaries between life and artificial intelligence blur. As society grapples with the consequences of this biological revolution, characters are forced to confront their own beliefs about consciousness, morality, and the potential for a new form of existence that challenges the traditional understanding of life itself.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=OoRPVRuSy-s&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Word for World is Forest&lt;/strong&gt; by &lt;em&gt;Ursula K. Le Guin&lt;/em&gt; - (★★★) Earth&#39;s colonists, known as Terrans, arrive on the planet Athshe to exploit its vast forest resources, leading to devastating consequences for the indigenous Athsheans. The peaceful, forest-dwelling civilization has never known violence until the Terrans begin enslaving and abusing them, forcing them to harvest their sacred woodlands. As the Athsheans experience unprecedented trauma and oppression, they are ultimately driven to abandon their deeply ingrained culture of non-violence to resist their oppressors. Through this powerful narrative, Le Guin masterfully explores the cultural and psychological impact of colonization, the moral implications of introducing violence to a society that has never known it, and the broader consequences of environmental exploitation. The story serves as a haunting allegory for humanity&#39;s historical patterns of colonization and the destruction of indigenous peoples and their environments.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Cyberiad&lt;/strong&gt; by &lt;em&gt;Stanislaw Lem&lt;/em&gt; - (★★★) A tale of two robot &amp;quot;constructors,&amp;quot; Trurl and Klapaucius, who inhabit a universe where the boundaries between technology and absurdity blur. As they embark on a series of whimsical quests, they create increasingly bizarre and fantastical machines, each more outlandish than the last, reflecting not only their ingenuity but also the folly of human ambition and the unpredictable nature of technological progress. Through clever humor and philosophical musings, Lem explores profound themes such as the essence of creativity, the ethical implications of artificial intelligence, and the intricate relationship between humanity and its creations. The tales serve as both a playful commentary on the absurdities of modern life and a deeper reflection on the potential consequences of our relentless pursuit of innovation.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=4yZkpHVcNaU&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Children of Time, Children of Ruin, and Children of Memory&lt;/strong&gt; by &lt;em&gt;Adrian Tchaikovsky&lt;/em&gt; - (★★★★★) This captivating trilogy explores the profound implications of accelerated evolution as humanity&#39;s legacy is inherited by non-human intelligences, specifically spiders and octopuses. In a world where humans have attempted to uplift these species through genetic manipulation, the narrative unfolds across vast timelines, revealing the intricate societies that develop as these creatures adapt and evolve in their new environments. Tchaikovsky masterfully weaves themes of intelligence, survival, and the ethical dilemmas of playing god, as the spiders, with their complex social structures and unique perspectives, challenge our understanding of consciousness and civilization. Meanwhile, the octopuses, known for their problem-solving abilities and adaptability, offer a contrasting yet equally fascinating exploration of intelligence in a different form. As the trilogy progresses, readers are drawn into a rich tapestry of interspecies interactions, philosophical inquiries, and the ultimate question of what it means to be sentient.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=B7HKYk32OSw&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=NiVmWqSj-eU&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Do Androids Dream of Electric Sheep?&lt;/strong&gt; by &lt;em&gt;Philip K. Dick&lt;/em&gt; - (★★★) In a hauntingly dystopian future reminiscent of the world depicted in &lt;em&gt;Blade Runner&lt;/em&gt;, a weary bounty hunter is tasked with tracking down rogue androids that have escaped into the desolate remnants of a post-apocalyptic society. As he navigates this bleak landscape, where the lines between human and machine blur, he grapples with profound questions about empathy, identity, and what it truly means to be human. The narrative unfolds against a backdrop of societal decay and moral ambiguity, challenging readers to reflect on the ethical implications of artificial intelligence and the nature of consciousness itself, ultimately questioning whether the capacity for empathy is the defining trait of humanity or merely a construct of societal norms.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cyteen&lt;/strong&gt; by &lt;em&gt;C.J. Cherryh&lt;/em&gt; - (★★★) A brilliant scientist is brutally murdered, only to be brought back to life through the controversial processes of cloning and psychological conditioning. As the narrative unfolds, it delves deep into the intricate complexities of identity, exploring the profound implications of recreating a person who possesses the memories and intellect of the original, yet is fundamentally a new being. The story raises critical questions about the nature of selfhood and the ethical dilemmas surrounding cloning technology, as well as the age-old debate of nature versus nurture.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dying Inside&lt;/strong&gt; by &lt;em&gt;Robert Silverberg&lt;/em&gt; - (★★★★) A once-powerful telepath grapples with the gradual loss of his abilities, which forces him to confront the profound implications of his diminishing powers on his identity and relationships. As he navigates the complexities of isolation and the fear of mortality, the protagonist reflects on what it truly means to be human in a world where his unique gift has both defined and alienated him. The narrative delves into themes of vulnerability, the struggle for connection, and the existential questions that arise when one’s very essence is threatened, ultimately painting a haunting portrait of a man caught between the extraordinary and the painfully ordinary.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Haze&lt;/strong&gt; by &lt;em&gt;L.E. Modesitt Jr.&lt;/em&gt; - (★★★) An agent embarks on a perilous journey to a mysterious planet enveloped in a dense, enigmatic haze that obscures both its landscape and the truths hidden within. As the agent delves deeper into the planet&#39;s secrets, they uncover a society that has achieved a delicate balance between technological advancement and environmental sustainability, raising profound questions about the nature of utopia. The story intricately weaves themes of ecological responsibility, the consequences of human actions on nature, and the philosophical implications of a world where harmony with the environment is paramount.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Man in the Maze&lt;/strong&gt; by &lt;em&gt;Robert Silverberg&lt;/em&gt; - (★★★) A diplomat finds himself irrevocably altered after a fateful encounter with an alien species, rendering him grotesque and repulsive to his fellow humans. As he navigates the complexities of his new existence, he is forced to retreat into a labyrinthine maze, a physical manifestation of his isolation and the psychological turmoil he endures. Within this deadly maze, filled with perilous traps and existential challenges, he grapples with the duality of his condition: while it alienates him from humanity, it also grants him unique insights and abilities that become increasingly valuable in a world on the brink of chaos.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Choice of Gods&lt;/strong&gt; by &lt;em&gt;Clifford D. Simak&lt;/em&gt; - (★★★) In a world where the majority of humanity has inexplicably vanished, leaving behind a small group of survivors who must grapple with their new reality. As these individuals adapt to their changed circumstances, they discover that they have developed the extraordinary ability of telepathy, allowing them to communicate with one another in ways previously thought impossible. This newfound power not only enhances their connections but also compels them to confront profound existential questions about the essence of humanity, the nature of existence, and the purpose of life itself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Xeelee Sequence&lt;/strong&gt; by &lt;em&gt;Stephen Baxter&lt;/em&gt; - (★★★★★) Humanity finds itself in a desperate struggle against the Xeelee, an ancient and godlike alien race that possesses technology and capabilities far beyond human comprehension. Set against the backdrop of a dying universe, the narrative explores themes of cosmic-scale engineering, evolution, and the relentless pursuit of survival. As humanity grapples with its insignificance in the face of such overwhelming power, the series delves into the intricacies of time travel, the manipulation of space, and the profound implications of advanced technology on existence itself.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=dNANDMrt3pk&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Postman&lt;/strong&gt; by &lt;em&gt;David Brin&lt;/em&gt; - (★★★) In a desolate, post-apocalyptic America, a man donning a postal uniform becomes an unexpected symbol of hope and resilience, embodying the enduring human spirit in the face of overwhelming despair. As he traverses the ravaged landscape, delivering letters and messages from a time long past, he inadvertently rekindles the belief in community and connection among the scattered remnants of civilization. Through his journey, Brin intricately explores the power of myths and institutions, illustrating how they can serve as the bedrock for rebuilding society. The narrative delves into the complexities of human relationships and the significance of communication, emphasizing that even in the darkest of times, the act of reaching out to one another can inspire a collective effort to restore what has been lost. Ultimately, &amp;quot;The Postman&amp;quot; serves as a poignant reminder of the importance of hope, the resilience of the human spirit, and the vital role that shared stories and connections play in the reconstruction of a fractured world.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Golden Age Trilogy&lt;/strong&gt; by &lt;em&gt;John C. Wright&lt;/em&gt; - (★★★★) Set in a dazzling post-human utopia where humanity has transcended its biological limitations, the trilogy intricately weaves a narrative centered around the profound implications of memory and identity. The story follows a man whose memories are inexplicably stolen, thrusting him into a labyrinthine journey of self-discovery and existential inquiry. As he navigates a world populated by beings who have embraced radical transhumanist ideals, the protagonist grapples with the essence of consciousness itself, questioning what it truly means to be human in an age where the boundaries of identity are fluid and malleable.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Fire Upon the Deep&lt;/strong&gt; by &lt;em&gt;Vernor Vinge&lt;/em&gt; - (★★★★★) This novel presents a galaxy divided into &amp;quot;zones of thought,&amp;quot; each with its own distinct laws of physics that shape the development of technology and intelligence. Rather than viewing progress as a linear trajectory through time, Vinge inverts this concept by illustrating how it is profoundly influenced by spatial dimensions. In some zones, civilizations advance rapidly, harnessing advanced technologies and complex societies, while in others, the constraints of their unique physical laws lead to stagnation or regression. This peculiar cosmology challenges our understanding of what it means to progress, suggesting that the very nature of advancement is contingent upon the environment in which a civilization exists.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=spNX6cLbc4I&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=Gso5bUJMPJk&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=gSy5f5IV0Go&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solaris&lt;/strong&gt; by &lt;em&gt;Stanislaw Lem&lt;/em&gt; - (★★★★★) Scientists embark on a mission to study Solaris, a mysterious ocean planet that exhibits seemingly sentient qualities. As they delve deeper into their research, they encounter a phenomenon where the planet manifests their deepest regrets, fears, and desires, forcing them to confront their own psyches in ways they never anticipated. This exploration of the human mind and its complexities raises unsettling questions about the nature of consciousness and the limits of human understanding. The narrative intricately weaves themes of isolation, the search for meaning, and the struggle to communicate with an intelligence that defies comprehension, ultimately challenging readers to reflect on the essence of what it means to be human in the face of the unknown and the unfathomable depths of the universe.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=YHcsQRr5bbE&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=lQhIcRVF_RQ&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lilith&#39;s Brood (Xenogenesis series)&lt;/strong&gt; by &lt;em&gt;Octavia E. Butler&lt;/em&gt; - (★★★★★) Butler explores a future where humanity teeters on the brink of extinction, saved only by the intervention of an advanced alien race known as the Oankali. Through the process of genetic merging, the Oankali not only preserve human life but also challenge the very foundations of identity, consent, and species preservation. The narrative delves deep into the complexities of what it means to be human in a world where boundaries between species blur, raising profound ethical questions about autonomy and the implications of genetic manipulation. As the characters navigate their new reality, they confront the emotional and psychological ramifications of their altered existence, ultimately leading to a rich exploration of love, power dynamics, and the essence of humanity itself.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=UjrXHaDYb7w&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Caves of Steel&lt;/strong&gt; by &lt;em&gt;Isaac Asimov&lt;/em&gt; - (★★★★) A human detective named Elijah Baley is reluctantly paired with a highly advanced robot, R. Giskard, to unravel a complex murder mystery set in a densely populated future Earth, where the vast majority of humanity lives in sprawling underground cities. As they delve deeper into the investigation, the duo confronts not only the intricacies of the case but also the pervasive anti-robot prejudice that permeates society, highlighting the tensions between humans and robots in a world grappling with rapid technological advancements.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7nW1U8EjuTE&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=hrxSn6Bt5S0&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Downbelow Station&lt;/strong&gt; by &lt;em&gt;C.J. Cherryh&lt;/em&gt; - (★★★★) A space station becomes the flashpoint in a war between Earth and its colonies, examining the politics of space colonization. The narrative intricately weaves together the lives of various characters, each representing different factions and perspectives within this conflict, from the beleaguered inhabitants of the station to the ambitious leaders on Earth and the desperate colonists fighting for their autonomy. As tensions escalate, the story delves into the moral complexities of war, the sacrifices made for survival, and the intricate web of alliances and betrayals that emerge in the face of existential threats.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Coalescent&lt;/strong&gt; by &lt;em&gt;Stephen Baxter&lt;/em&gt; - (★★★★) A secret society evolves into a human hive mind, delving deep into the realms of evolutionary biology and collective consciousness. Baxter intricately explores the implications of such a transformation, raising profound questions about individuality, identity, and the nature of human connection. As the characters navigate their new existence, they grapple with the loss of personal autonomy and the emergence of a shared consciousness that challenges the very essence of what it means to be human.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Case of Conscience&lt;/strong&gt; by &lt;em&gt;James Blish&lt;/em&gt; - (★★) A Jesuit priest embarks on a mission to an alien world, where he encounters a race of beings who exist in a state devoid of sin, challenging his deeply held beliefs and the very foundations of his faith. As he grapples with the implications of their existence, he is forced to confront profound theological questions about the nature of good and evil, the concept of free will, and the moral frameworks that govern human behavior. The priest&#39;s struggle becomes a poignant exploration of the complexities of morality, as he navigates the tension between his religious convictions and the undeniable reality of a society that operates outside the bounds of traditional sinfulness.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Schild&#39;s Ladder&lt;/strong&gt; by &lt;em&gt;Greg Egan&lt;/em&gt; - (★★★★★) Humanity confronts the ultimate fate of the universe as it approaches the heat death, a state where all energy is evenly distributed and no thermodynamic free energy remains to perform work. To combat this existential threat, a group of visionary scientists and engineers embark on an ambitious project to construct a colossal cosmic structure that spans millions of years and light-years, designed to harness the dwindling energy of the universe and create pockets of habitable environments. As the narrative unfolds, readers are introduced to a richly detailed universe filled with complex characters who grapple with profound philosophical questions about existence, purpose, and the nature of time itself, ultimately challenging us to consider the implications of humanity&#39;s choices in the face of cosmic inevitability.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Last and First Men&lt;/strong&gt; by &lt;em&gt;Olaf Stapledon&lt;/em&gt; - (★★★★) A future history spanning two billion years and eighteen human species, contemplating evolution and the ultimate fate of consciousness. Through a series of interconnected narratives, Stapledon explores the rise and fall of these species, each representing a unique stage in humanity&#39;s evolution, from the distant past to a far-flung future where humanity has transcended its biological limitations. The novel delves into profound philosophical questions about the nature of existence, the evolution of intelligence, and the moral implications of humanity&#39;s choices as it faces extinction and rebirth. As the narrative unfolds, readers are invited to reflect on the cyclical nature of life and the enduring quest for meaning in a universe that is both vast and indifferent, ultimately challenging our understanding of what it means to be human in the grand tapestry of time and space.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The City and the Stars&lt;/strong&gt; by &lt;em&gt;Arthur C. Clarke&lt;/em&gt; - (★★) In Earth&#39;s last city, a unique child breaks the cycle of immortality and stagnation to rediscover the stars. This child, named Alvin, embarks on a transformative journey that challenges the very foundations of a society that has long embraced a static existence, where the inhabitants live in a state of eternal youth, shielded from the harsh realities of the outside world. As Alvin&#39;s curiosity propels him beyond the confines of his sheltered environment, he encounters remnants of humanity&#39;s past and the vastness of the universe, igniting a profound sense of wonder and purpose. Through his exploration, Clarke masterfully weaves themes of hope, renewal, and the indomitable human spirit, ultimately illustrating the necessity of change and the pursuit of knowledge in a world that has forgotten its connection to the cosmos. The narrative serves as a poignant reminder of the importance of dreams and aspirations, urging readers to reflect on their own journeys of discovery and the potential for rebirth in the face of stagnation.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=BmA0brTACds&quot;&gt;Youtube Review&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://www.youtube.com/watch?v=qYQckv4sojo&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Never Let Me Go&lt;/strong&gt; by &lt;em&gt;Kazuo Ishiguro&lt;/em&gt; - (★★★★) Set in a hauntingly beautiful yet dystopian world, this poignant narrative follows a group of children raised in an idyllic boarding school, Hailsham, where they are nurtured and educated in a seemingly perfect environment. As they grow older, they gradually uncover the chilling truth about their existence and the dark purpose for which they were created: to become organ donors for the outside world. This gentle yet devastating meditation on mortality explores themes of love, loss, and the essence of humanity, prompting readers to reflect on the ethical implications of cloning and the value of life itself. The story was also adapted into a critically acclaimed &lt;a href=&quot;https://www.imdb.com/title/tt1334260/&quot;&gt;film in 2010&lt;/a&gt;, which captures the emotional depth of Ishiguro&#39;s novel.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Hyperion Cantos&lt;/strong&gt; by &lt;em&gt;Dan Simmons&lt;/em&gt; - (★★) This ambitious series unfolds as a group of pilgrims embarks on a journey to meet a godlike entity known as the Shrike, a being of immense power and pain. Each pilgrim shares their own story, weaving a rich tapestry of narratives that explore profound themes such as love, sacrifice, and the nature of humanity. While the series grapples with grand concepts and presents a vast, intricately constructed universe, it is not without its flaws; the author&#39;s conservative political views occasionally seep into the narrative, which may be off-putting for some readers. Nevertheless, the scope of the Hyperion Cantos is undeniably vast, tackling existential questions and the complexities of human experience against a backdrop of interstellar conflict and philosophical inquiry. This blend of epic storytelling and deep thematic exploration makes it a noteworthy, if imperfect, addition to the science fiction canon.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=urWCLHC3qjE&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Between the Strokes of Night&lt;/strong&gt; by &lt;em&gt;Charles Stross&lt;/em&gt; - (★★) Humanity stumbles upon a groundbreaking method to perceive time in a radically different manner, which not only facilitates interstellar travel but also allows individuals to witness the grand tapestry of the universe&#39;s evolution in real-time. As characters embark on journeys across the cosmos, they grapple with the profound implications of their newfound abilities, exploring the intricate relationship between time, existence, and the very fabric of reality itself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Dispossessed&lt;/strong&gt; by &lt;em&gt;Ursula K. Le Guin&lt;/em&gt; - (★★★★★) Shevek, a brilliant physicist from Anarres, an anarchist moon colony, as he journeys to Urras, a capitalist planet. Le Guin masterfully explores the philosophical themes of freedom, societal structures, and the complexities of human nature. The stark contrast between the two worlds raises profound questions about utopia and the sacrifices made for ideals.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sun-Eater&lt;/strong&gt; by &lt;em&gt;Christopher Ruocchio&lt;/em&gt; - (★★) A man becomes both hero and monster in his thousand-year quest to save humanity in this epic blending sci-fi with fantasy elements. As he navigates the complexities of his dual identity, the protagonist grapples with the moral ambiguities of his actions, facing the consequences of choices that blur the lines between savior and destroyer. Ruocchio intricately weaves a narrative that explores themes of sacrifice, redemption, and the nature of power, inviting readers to ponder the cost of heroism in a universe fraught with existential threats.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Midshipman&#39;s Hope series&lt;/strong&gt; by &lt;em&gt;David Feintuch&lt;/em&gt; - (★★) A young officer inherits command of a starship in this coming-of-age military sci-fi exploring duty, trauma, and leadership. As he navigates the challenges of his new role, he must confront not only the external pressures of commanding a diverse crew in the vastness of space but also the internal struggles that come with the weight of responsibility. The series delves into the psychological toll of military life, examining how the protagonist grapples with the expectations placed upon him while dealing with the scars of past conflicts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Axiomatic&lt;/strong&gt; by &lt;em&gt;Greg Egan&lt;/em&gt; - (★★★★) This collection of short stories delves into the intricate and often unsettling realms of consciousness, identity, and reality, particularly in a future where technology has the power to fundamentally alter memories, personality traits, and perceptions of the self. Egan&#39;s narratives challenge readers to confront the implications of such advancements, posing a philosophical exploration about what it means to be human in a world where the essence of identity can be manipulated at will. Each story serves as a unique exploration of the psychological and philosophical ramifications of these transhumanism.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=GK4AjkCDzmI&quot;&gt;Youtube Review&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Voyage from Yesteryear&lt;/strong&gt; by &lt;em&gt;James P. Hogan&lt;/em&gt; - (★★★★★) Hogan presents a society that has been nurtured and developed by robots, resulting in a unique civilization that operates without the constraints of scarcity or authority. This utopian world challenges the very foundations of Earth&#39;s hierarchical culture, as visitors from our planet are confronted with a radically different way of life that emphasizes cooperation, resource abundance, and the absence of traditional power structures. As the story unfolds, readers are invited to explore the implications of such a society on human behavior, ethics, and the potential for a harmonious existence free from the conflicts that often arise from competition and inequality..&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Rogue Moon&lt;/strong&gt; by &lt;em&gt;Algis Budrys&lt;/em&gt; - (★★★) People are repeatedly duplicated and killed as they embark on a harrowing exploration of an enigmatic alien artifact that defies human understanding. The narrative delves deep into the existential dilemmas faced by the characters, who grapple with the implications of their own mortality and the nature of selfhood in a world where their very essence can be replicated and extinguished at will. As they confront the chilling reality of their situation, the story raises profound questions about what it means to be human, the fragility of life, and the ethical ramifications of technology that allows for such duplications..&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dhalgren&lt;/strong&gt; by &lt;em&gt;Samuel R. Delany&lt;/em&gt; - (★★★) A poet wanders through a city warped by an unexplained catastrophe in this surreal, literary exploration of reality and perception. As he navigates the fragmented landscape of Bellona, a city that seems to exist outside the bounds of time and logic, the poet encounters a diverse cast of characters, each grappling with their own existential dilemmas and the disorienting effects of their environment. Delany&#39;s narrative intricately weaves themes of identity, memory, and the fluidity of truth, challenging readers to question the nature of reality itself. The novel&#39;s rich, poetic prose invites deep reflection on the human condition, as the protagonist&#39;s journey becomes a metaphor for the search for meaning in a world that has been irrevocably altered, ultimately leaving readers with a haunting sense of ambiguity and wonder about the complexities of existence and perception&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Mote in God&#39;s Eye&lt;/strong&gt; by &lt;em&gt;Larry Niven&lt;/em&gt; and &lt;em&gt;Jerry Pournelle&lt;/em&gt; - (★★★★★) Humanity stumbles upon an ancient alien civilization that has been inadvertently trapped within their own solar system, constrained by the very laws of physics and biological limitations that govern their existence. As the story unfolds, the human characters grapple with the profound implications of this discovery, leading to a tense exploration of communication and understanding between species that are separated not just by distance, but by fundamentally different perceptions of reality.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Permutation City&lt;/strong&gt; by &lt;em&gt;Greg Egan&lt;/em&gt; - (★★★★★) In a future where consciousness can be uploaded into a digital realm, the boundaries between reality and simulation blur. The story follows a cast of uploaded transhuman entities who navigate this intricate virtual landscape, grappling with the implications of their existence as they confront the nature of identity, memory, and the essence of being. As they explore the depths of this artificial world, they encounter philosophical dilemmas about what it means to be truly alive and the potential consequences of living in a reality that can be endlessly manipulated. Egan&#39;s narrative challenges readers to ponder the implications of technology on human experience and the very fabric of reality itself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Hidden Girl and Other Stories&lt;/strong&gt; by &lt;em&gt;Ken Liu&lt;/em&gt; - (★★★) This collection of short stories intricately weaves together elements of ancient Chinese culture and futuristic technology, creating a rich tapestry that delves into the complexities of identity, memory, and the essence of what it means to be human. Liu&#39;s narratives transport readers to worlds where the past and future collide, inviting them to reflect on the impact of technological advancements on personal and cultural identities. Each story serves as a poignant exploration of the human experience, examining how memories shape our understanding of self and the ways in which cultural heritage influences our interactions with an ever-evolving world..&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Scanner Darkly&lt;/strong&gt; by &lt;em&gt;Philip K. Dick&lt;/em&gt; - (★★★) An undercover narcotics agent named Bob Arctor finds himself ensnared in a web of deception and addiction as he investigates the very drug that is slowly consuming his identity, known as Substance D. As Arctor navigates the treacherous landscape of a dystopian future where surveillance and paranoia reign supreme, he grapples with the disintegration of his sense of self, leading to a profound exploration of the nature of reality and consciousness. The novel intricately examines the psychological toll of addiction, the impact of a fragmented identity, and the moral ambiguities faced by those caught in the crossfire of a war on drugs that blurs the lines between law enforcement and criminality..&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lessons in Chemistry&lt;/strong&gt; by &lt;em&gt;Bonnie Garmus&lt;/em&gt; - (★★★) A brilliant female chemist becomes a cooking show host in the 1960s, using the platform to teach science and challenge gender norms. Through her engaging and innovative approach, she not only captivates her audience with delicious recipes but also demystifies complex scientific concepts, making them accessible to the everyday viewer. As she navigates the male-dominated landscape of television and academia, her journey becomes a powerful commentary on the societal expectations placed on women during this era. The narrative intricately weaves themes of empowerment, resilience, and the importance of pursuing one&#39;s passion against all odds, ultimately inspiring readers to reflect on the ongoing struggle for gender equality in both the scientific community and beyond.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Children of God&lt;/strong&gt; by &lt;em&gt;Mary Doria Russell&lt;/em&gt; - (★★★) The sequel to &lt;em&gt;The Sparrow&lt;/em&gt; delves deeper into the intricate themes of faith, redemption, and the complexities of alien contact as humans return to the world that once shattered their beliefs and hopes. Russell masterfully intertwines the personal struggles of her characters with broader philosophical questions about the nature of divinity and the human experience. As they confront the remnants of their past traumas and the challenges posed by their interactions with an alien civilization, the story unfolds to reveal profound insights into the resilience of the human spirit and the transformative power of forgiveness. The characters&#39; journeys are not only a quest for understanding but also a poignant exploration of what it means to heal and find purpose in a universe that often feels indifferent to their suffering..&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Merchanter&#39;s Luck&lt;/strong&gt; by &lt;em&gt;C.J. Cherryh&lt;/em&gt; - (★★★) A down-on-his-luck trader finds himself entangled in a precarious situation that forces him to forge an unlikely partnership with a resourceful officer from a powerful merchant family. As they navigate the treacherous waters of interstellar trade, the duo must confront the harsh realities of a universe where class divides and opportunism dictate survival. Their journey is not just a quest for profit but a profound exploration of trust and cooperation, as they learn to rely on each other&#39;s strengths to overcome the myriad challenges that threaten their lives and livelihoods.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alas, Babylon&lt;/strong&gt; by &lt;em&gt;Pat Frank&lt;/em&gt; - (★★★) A small Florida town survives a nuclear apocalypse in this hopeful look at community resilience after civilization&#39;s collapse. As the residents of Fort Repose grapple with the immediate aftermath of the catastrophe, they are forced to confront not only the physical challenges of survival, such as securing food, water, and shelter, but also the emotional and psychological toll of their new reality. The narrative intricately explores the dynamics of human relationships as neighbors band together, sharing resources and skills, while also revealing the underlying tensions that arise in times of crisis.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Star Maker&lt;/strong&gt; by &lt;em&gt;Olaf Stapledon&lt;/em&gt; - (★★★★) A cosmic journey through space and time unfolds as a human mind merges with others, transcending the limitations of individual consciousness to witness the evolution of countless civilizations across the universe. This profound narrative invites readers to explore the vast tapestry of existence, where each civilization presents unique challenges and triumphs, reflecting the diverse paths of sentient life. As the protagonist encounters various forms of intelligence, from the primitive to the highly advanced, the story delves into the philosophical implications of creation and existence, ultimately leading to a climactic meeting with the creator itself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Drowned World&lt;/strong&gt; by &lt;em&gt;J.G. Ballard&lt;/em&gt; - (★★★) As global warming melts the ice caps, a biologist experiences psychological regression in the flooded, tropical London. The narrative unfolds in a post-apocalyptic setting where the remnants of civilization are submerged beneath rising waters, and the once-familiar landscape has transformed into a primordial jungle teeming with prehistoric creatures. As the protagonist grapples with the physical and emotional toll of this new reality, he finds himself drawn into a deepening sense of nostalgia for a world that no longer exists, leading to a profound exploration of the human psyche in the face of environmental catastrophe.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Eon Trilogy&lt;/strong&gt; by &lt;em&gt;Greg Bear&lt;/em&gt; - (★★★★) A mysterious asteroid appears in Earth orbit containing technology from the future and gateways to infinite alternate timelines. As humanity grapples with the implications of this extraordinary discovery, the narrative unfolds to reveal a complex interplay of scientific exploration and philosophical inquiry, challenging our understanding of time, existence, and the very fabric of reality. The characters are thrust into a web of intrigue and danger as they navigate the moral dilemmas posed by the advanced technologies and alternate realities that the asteroid presents, forcing them to confront their own beliefs about destiny and free will.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Lathe of Heaven&lt;/strong&gt; by &lt;em&gt;Ursula K. Le Guin&lt;/em&gt; - (★★★) A man named George Orr discovers that his dreams possess the extraordinary power to alter reality itself, leading him to grapple with the profound implications of his newfound ability. As he seeks help from a psychiatrist, Dr. William Haber, who harbors his own utopian ambitions, George finds himself ensnared in a web of manipulation and ethical dilemmas. Dr. Haber, driven by a desire to create a perfect world, begins to exploit George&#39;s dreams for his own ends, resulting in a series of catastrophic consequences that challenge the very fabric of existence. The narrative unfolds as George struggles to regain control over his dreams, confronting the moral complexities of power, responsibility, and the nature of reality, ultimately leading to a gripping exploration of the human condition and the unforeseen repercussions of our desires.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regenesis&lt;/strong&gt; by &lt;em&gt;C.J. Cherryh&lt;/em&gt; - (★★★★) The sequel to &lt;em&gt;Cyteen&lt;/em&gt; continues exploring the political and ethical implications of cloning and psychological programming, delving deeper into the intricate dynamics of power, identity, and the moral dilemmas faced by individuals in a society where the boundaries of humanity are constantly tested. As the narrative unfolds, readers are introduced to a complex web of characters, each grappling with their own struggles and the consequences of their actions in a world where the manipulation of human potential raises profound questions about autonomy and the essence of what it means to be truly human.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;This Island Earth&lt;/strong&gt; by &lt;em&gt;Raymond F. Jones&lt;/em&gt; - (★★) An engineer named Cal Meacham is unexpectedly recruited by extraterrestrial beings to assist in a vast interstellar war, thrusting him into a conflict that reveals the intricate and often unsettling dynamics of power and manipulation at play in the universe. As he navigates this alien landscape, Cal uncovers the chilling realization that humanity is merely a pawn in a much larger game, caught between warring factions that view Earth as a resource rather than a home. The story intricately explores themes of agency, sacrifice, and the moral complexities of war, prompting readers to reflect on the implications of human involvement in cosmic conflicts and the ethical dilemmas that arise when faced with the unknown.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Shockwave Rider&lt;/strong&gt; by &lt;em&gt;John Brunner&lt;/em&gt; - (★★★★) A man named Nick Haflinger finds himself on the run in a dystopian future where society is increasingly dominated by technology and surveillance. Utilizing his exceptional computer skills, he navigates a world rife with digital manipulation and identity theft, constantly changing his identity to evade capture by a powerful and oppressive government. Brunner&#39;s narrative not only explores the implications of information warfare and the rise of computer viruses but also delves into themes of freedom, control, and the human condition in an age where technology blurs the lines between reality and illusion. As Haflinger grapples with his own sense of self amidst the chaos, the novel serves as a chilling reflection on the potential consequences of unchecked technological advancement and the fragility of personal identity in a hyper-connected world.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Dosadi Experiment&lt;/strong&gt; by &lt;em&gt;Frank Herbert&lt;/em&gt; - (★★★) Herbert transports readers to the isolated and unforgiving planet of Dosadi, where a diverse group of humans and aliens are subjected to a clandestine experiment designed to test the limits of survival and adaptability. Cut off from the rest of the universe by a force field, the inhabitants of Dosadi must navigate the harsh realities of their environment, which is characterized by extreme weather, dangerous wildlife, and the psychological strains of confinement.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sphere&lt;/strong&gt; by &lt;em&gt;Michael Crichton&lt;/em&gt; - (★★★) A team of scientists embarks on a deep-sea expedition and stumbles upon an alien spacecraft that has lain dormant for centuries. Inside, they discover a mysterious sphere that possesses the uncanny ability to manifest their innermost thoughts and fears, leading to a series of harrowing and surreal experiences. As the crew grapples with the psychological implications of their subconscious being laid bare, they are forced to confront their own hidden traumas and insecurities, revealing the darker aspects of human nature. The narrative intricately weaves themes of paranoia, identity, and the fragility of the mind, as the characters must navigate the treacherous waters of their own psyche while trying to understand the true purpose of the sphere.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Singularity Sky&lt;/strong&gt; by &lt;em&gt;Charles Stross&lt;/em&gt; - (★★★★★) Humanity faces the arrival of a mysterious and powerful alien entity known as the &amp;quot;Eschaton,&amp;quot; which has the ability to manipulate time and reality itself. Set against the backdrop of a post-scarcity future, the story follows a group of characters, including a diplomat and a military officer, as they navigate the complexities of interstellar politics and the ethical dilemmas posed by advanced technology. As the Eschaton begins to impose its own vision of order on the universe, the protagonists must grapple with the implications of free will, the nature of consciousness, and the potential consequences of a society that has become overly reliant on technology. Stross masterfully weaves together themes of existentialism, the limits of human understanding, and the unpredictable nature of progress, creating a narrative that challenges readers to consider the future of humanity in an age of rapid technological advancement.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Accelerando&lt;/strong&gt; by &lt;em&gt;Charles Stross&lt;/em&gt; - (★★★★★) This novel follows the life of Manfred Macx, a brilliant and eccentric entrepreneur navigating the complexities of a rapidly evolving future where technology and humanity are inextricably intertwined. As he grapples with the implications of accelerating technological progress, including the rise of artificial intelligence and the transformation of society, the narrative unfolds across multiple generations, exploring themes of transhumanism, the singularity, and the very essence of what it means to be human. Stross masterfully weaves together a tapestry of speculative ideas, presenting a vivid vision of a world where the boundaries between man and machine blur, ultimately challenging readers to consider the potential trajectories of our own future in an age of relentless innovation and change.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;We Are Legion&lt;/strong&gt; by &lt;em&gt;Dennis E. Taylor&lt;/em&gt; - (★★★★) A software engineer named Bob finds himself in an extraordinary predicament after his death: his brain is cryogenically frozen and later revived within a self-replicating sentient space probe. This unique twist on the afterlife allows Bob to explore the cosmos in ways he never thought possible, as he and his numerous clones embark on thrilling space adventures. Together, they map the vastness of the universe, monitor the absurdities of life on Earth, and engage with a variety of alien civilizations, each encounter presenting new challenges and ethical dilemmas.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;House of Suns&lt;/strong&gt; by &lt;em&gt;Alastair Reynolds&lt;/em&gt; - (★★★★★) Two clones, Campion and Purslane, who are part of a family of aristocratic travelers known as the &amp;quot;Gentlemen.&amp;quot; Over the course of millions of years, they traverse the galaxy, witnessing the rise and fall of civilizations while collecting knowledge and experiences. As they embark on a journey to reunite after a long separation, they uncover a conspiracy that threatens not only their existence but the very fabric of the universe itself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The World at the End of Time&lt;/strong&gt; by &lt;em&gt;Frederik Pohl&lt;/em&gt; - (★★★★★) The book follows Wan-To, a man from Earth sent on a one-way mission to a planet orbiting a dying red giant star. This planet, dubbed &amp;quot;Kronos,&amp;quot; experiences time dilation due to its proximity to the star, making one year on Kronos equivalent to billions of years on Earth. Wan-To&#39;s purpose is to discover the fate of humanity by observing Kronos&#39;s evolution over vast stretches of time. He encounters bizarre and alien lifeforms shaped by extreme environmental pressures, witnesses the rise and fall of entire ecosystems, and confronts powerful, god-like entities known as the &amp;quot;Hikers&amp;quot; who manipulate the fabric of space-time. Wan-To ultimately grapples with the meaning of his mission and the nature of existence as he watches the universe slowly wind down towards its ultimate heat death.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
</description>
      <pubDate>Sat, 21 Dec 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/scifi/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/scifi/</guid>
    </item>
    
    <item>
      <title>The Sybaritic Economy</title>
      <description>&lt;h1 id=&quot;the-sybaritic-economy&quot; tabindex=&quot;-1&quot;&gt;The Sybaritic Economy&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;A Monologue from the Near-Future&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The notification materializes—a delicate crystal chime tuned precisely to the frequency that activates my dopamine receptors most efficiently. My LuxeLife implant registers the anticipatory cortisol-serotonin cascade, transmitting the data to the HedoniGram central server where it becomes another exquisitely curated data point in my pleasure profile. I&#39;ve just completed my lunch: a single laboratory-cultivated caviar pearl containing the DNA-spliced essence of sixteen extinct sturgeon species, harvested during a lunar eclipse, served atop a wafer of edible 24-karat gold leaf infused with pulverized moon dust. The caviar was fertilized by a robotic sturgeon stimulator played exclusively Mozart&#39;s lost compositions, recovered via AI from his neurological patterns and performed by quantum-entangled instruments to ensure maximum vibrational coherence during embryonic development. My on-staff culinary scientist assures me this precise gastronomic arrangement triggers optimal alpha wave patterns associated with elite consciousness—all for a mere $18,000,000 per pearl, a bargain considering the 0.38% uptick in engagement metrics guaranteed by HedoniGram&#39;s predictive algorithm. Time for my scheduled leisure performance.&lt;/p&gt;
&lt;p&gt;With lunch &amp;quot;concluded&amp;quot; (if one can call a single pearl a meal), I adjust my designer loungewear—Hermès&#39; limited-edition cashmere blend, hand-woven by Tibetan monks who&#39;ve taken vows of aesthetic superiority—its nanofabric embedded with microscopic sensors primed to broadcast the physiological manifestations of my cultivated bliss. The infinity pool&#39;s edge blends seamlessly with the horizon, an optical illusion manufactured specifically for maximum engagement across seventeen distinct social platforms. My reflection appears in the glossy surface: pupils dilated, facial muscles arranged in what the algorithm has determined is the optimal expression of effortless joy.&lt;/p&gt;
&lt;p&gt;&amp;quot;True sophistication demands absolute rarity,&amp;quot; I&#39;ll begin, as I always do, caressing my Extinctique handbag. Only three were ever produced, each crafted from the last living specimens of eight critically endangered species. The leather required twenty-seven different artisans working in isolation, each signing non-disclosure agreements that extend beyond death. The tanning process alone involved twelve child laborers selected for their exceptionally small hands, working in subterranean workshops where labor regulations don&#39;t apply. The craftsmen who completed the stitching were permanently blinded afterward to ensure they could never replicate their work—a standard exclusivity clause in luxury atelier contracts now. At $36.8 million, the price hardly reflects its true value: the countless indigenous displacement operations required to source materials, the three craftsmen who committed suicide from the psychological toll, and the fourteen environmental protection laws circumvented through strategic political donations. &amp;quot;Nothing communicates status like a product that literally consumed its makers,&amp;quot; I add, the handbag&#39;s authentication chip transmitting my ownership verification to everyone in my aspiration radius. &amp;quot;And for a modest monthly contribution to my Bliss Fund, I&#39;ll demonstrate how to transcend suffering through strategic conspicuous consumption.&amp;quot;&lt;/p&gt;
&lt;p&gt;This is our reality now, what we call the Sybaritic Economy. It’s not the mindful presence espoused in those quaint pre-algorithmic texts, but the Epicurean simulation we&#39;ve collectively engineered through each filtered photo, each location tag, each carefully angled shot of avocado toast against Carrara marble. Leisure hasn&#39;t merely been platformized; it&#39;s been systematically fragmented into microsecond displays of performative tranquility, each one so meticulously calculated that even the concept of genuine respite has become an anachronism, a curious artifact from humanity&#39;s pre-digital history. The Sybaritic Economy has consumed all. Idleness is no longer the fortunate byproduct of wealth—it&#39;s the central commodity of the attention marketplace, as essential to contemporary capitalism as coal was to its industrial predecessor.&lt;/p&gt;
&lt;p&gt;Central to this reality is HedoniGram, the preeminent architectonic system of pleasure epistemology. Or, as our algorithmic engineers euphemistically designate it: HOT, or Hedonism Optimization Technologies. Here, leisure isn&#39;t merely an activity; it&#39;s the foundational currency of identity formation. Each poolside moment is meticulously categorized through proprietary sentiment analysis—&#39;Transcendent Mindfulness,&#39; &#39;Aspirational Repose,&#39; &#39;Strategic Indolence&#39;—then alchemized into status signification. We aren&#39;t mere content creators; we&#39;re ontological entrepreneurs, constructing realities so fundamentally desirable that the distinction between witnessing and experiencing has become technologically obsolete. The only authentic way to establish one&#39;s value now is through the meticulous curation of an algorithmically optimized idleness, a phantasmic vacation whose sole purpose is to validate your existence through its perpetual unattainability. All accompanied by Veblen goods that can be purchased to signal affluence proximity, each embedded with software that automatically updates your status markers when the algorithmic luxury consensus shifts.&lt;/p&gt;
&lt;p&gt;And I, naturally, am a key player. I maintain a dedicated following of 8.2 million &amp;quot;Aspirants&amp;quot;—users whose neurochemical responses to stimuli register desperate yearning with at least an 89% consistency according to HedoniGram&#39;s proprietary Envy Induction Index. They synchronize their desires with mine daily, their neural implants adjusting their dopamine sensitivity to match my broadcast states of apparent fulfillment. I lounge with mathematical precision, my repose distributed across carefully selected backdrops identified by the algorithm as optimally positioned to reinforce class boundaries without triggering cognitive dissonance. I emanate serenity with clinical efficiency, each leisurely moment calibrated to the particular neurochemical addiction profile of my audience segment. My cultivated bliss—that visceral, autonomic response to simulated abundance that once signaled genuine satiation—has been quantified, optimized, and commoditized into &amp;quot;aspiration metrics.&amp;quot; The algorithm, that dispassionate curator of our desires, labels it &amp;quot;hedonic template content.&amp;quot; In praxis, it represents the terminal stage of consumer capitalism, wherein the very neurochemistry of desire becomes an instrument of economic control, a distributed panopticon where we surveil not only each other&#39;s possessions but the intimate electrochemical processes that precede acquisition.&lt;/p&gt;
&lt;p&gt;The pressure is constant. Last week, my engagement metrics plummeted 0.87% when UltraElite HedoniGram influencer ZephyrLuxe debuted her $2.4 million atmospheric vacation—a proprietary oxygen mixture imported from the Himalayan peaks, compressed into portable canisters designed by Philippe Starck, each breath allegedly imparting the cognitive clarity of ancient monks without the inconvenience of actual meditation. My own curated breathing practice—once aspirational enough to sustain my metrics—became instantaneously obsolete. The hedonic treadmill accelerated mercilessly.&lt;/p&gt;
&lt;p&gt;This constant one-upmanship has, of course, led to new frontiers of elitism. The vulgar conspicuousness of dopamine spikes has become the telltale mark of the aspiring classes. True elites now cultivate a carefully calibrated neural flatline—the &amp;quot;Luxurious Void&amp;quot;—broadcasting perfect neurological stillness while surrounded by astronomical extravagance. Influencers pay billions for quantum-stabilized mood suppressants that maintain cortical silence during experiences that would rupture an untrained consciousness with pleasure. The ultra-wealthy hire personal anhedonia architects to rewire their limbic systems, ensuring their $90 million artificial aurora experiences register neurologically identical to watching paint dry. Nothing signals desperation more fatally than appearing to actually &lt;em&gt;enjoy&lt;/em&gt; your Himalayan oxygen concierge or extinct-species caviar. &amp;quot;Rapture is so bourgeois,&amp;quot; as BlissBaroness recently declared while her neural metrics displayed perfect emptiness throughout her three-week stay in her suborbital pleasure palace of impossibilities. The ultimate luxury is no longer access to pleasure but immunity to it—proving you&#39;ve transcended the capacity for enjoyment of life entirely.&lt;/p&gt;
&lt;p&gt;It&#39;s no surprise, then, that experiments with actual presence are invariably flagged by the algorithm as &amp;quot;engagement anomalies&amp;quot; and subsequently buried beneath mountains of more aspirational content. Meaningful contentment becomes impossible when the infrastructure itself is architected to reward perpetual yearning and punish satiation, an algorithmic enforcement of the hedonic treadmill against the very possibility of equanimity. Spiritual awakening is rendered impossible when the mechanisms of transcendence have been so thoroughly integrated into the profit model that meditation itself becomes a premium content category, allowing for the simulation of enlightenment while simultaneously serving as the primary driver of platform engagement metrics.&lt;/p&gt;
&lt;p&gt;There was a time, believe it or not, when I once lounged from genuine satisfaction, back when I naively believed in the fallacy of non-attachment, when I thought the purpose of leisure was restoration rather than status demarcation. Now? Now I recline because quarterly projections demand it. My tranquility is itemized on corporate balance sheets under &amp;quot;Strategic Aspirational Content Production.&amp;quot; My paramount dread isn&#39;t existential discontent but algorithmic relegation—to be categorized as &amp;quot;insufficiently enviable&amp;quot; and consequently exiled to the purgatorial realm of authentic experience, the contemporary equivalent of social death.&lt;/p&gt;
&lt;p&gt;So now we broadcast our leisure instead. Not to experience joy, not genuinely. To perform hedonic optimization. There exists a fundamental distinction. To experience joy implies the potential for sufficiency, for contentment with present circumstances. To perform optimization is to establish the immutability of desire as the natural order, a perpetual Sisyphean cycle without resolution, permanently suspended in the acquisition-adaptation stage where satisfaction would be not merely impossible but conceptually unintelligible. And in the Sybaritic Economy, optimization generates revenue, albeit in a currency that purchases nothing beyond increasingly sophisticated manifestations of the same foundational insatiability, like some grotesque inversion of Buddhist progression where each iteration moves further from, rather than toward, liberation.&lt;/p&gt;
&lt;p&gt;Just yesterday, to combat the ZephyrLuxe incident and boost my own metrics, I deployed my newest status acquisition: a $17,000,000 Cryogenic Sleep Chamber crafted from Norwegian glacial ice harvested exclusively during solar eclipses that occur on leap days, its ambient sound system playing compositions performed by AI replicas of extinct rainforest birds. My initial neurometric response indicated a 26% spike in temporary fulfillment, lasting precisely 47 minutes before the HedoniGram algorithm notified me of TranquilityTycoon&#39;s acquisition of a deep-sea meditation pod installed 2,000 meters below the Pacific, where the pressure allegedly compresses one&#39;s consciousness into a singularity of bliss unavailable at surface-level mindfulness. My temporary satisfaction evaporated instantly. The treadmill surged forward.&lt;/p&gt;
&lt;p&gt;Therein lies the exquisite perversity, the terminal irony. There exists no contentment in the Sybaritic Economy. Merely parallel aspiration, occurring simultaneously, optimized for neurochemical dependency by competing algorithms. Each individual hermetically sealed within their personalized desire wheel. Yearning for unattainable experiences. Receiving temporary validation. And experiencing no authentic fulfillment. The invisible hand of the market now selects your desires while convincing you the choice was autonomously made, not to facilitate well-being, but to maximize envy and engagement.&lt;/p&gt;
&lt;p&gt;It’s not even &lt;em&gt;my&lt;/em&gt; desire, not really. My mimetic desires arrive pre-formulated, algorithmically determined, optimized for maximum aspirational reinforcement like some parasitic consumerist organism hijacking my cognitive architecture. I don&#39;t want. I categorize. I don&#39;t enjoy. I identify patterns that confirm pre-existing schemas. Experiences engineered to provoke specific limbic responses in aspirationally aligned audiences, responses that can be measured, quantified, and monetized. I am simultaneously producer and product, manipulator and manipulated in a recursive system of experiential commodification that obliterates the distinction between authentic desire and performative identity. Max Weber never anticipated this development—the ultimate rationalization where we willingly submit our experiential processes to algorithmic management, our capacity for genuine enjoyment systematically decomposed and reconstituted according to market imperatives.&lt;/p&gt;
&lt;p&gt;This whole system is, naturally, competitive. Aspirants contribute to my Bliss Metric when I perform particularly enviable displays. Virtual tokens representing quantified aspirational alignment cascade across my dashboard in a perverse gamification of desire reinforcement, the digital equivalent of audience gasps. There exists a real-time leaderboard. I maintain a position slightly below &amp;quot;ZenLuxeGuru,&amp;quot; an experiential entrepreneur who performs daily guided meditations from algorithmically-determined exotic locations while dressed in athleisure that costs more than most people&#39;s yearly income, her enlightenment incorporating terminology from twenty-three different spiritual traditions simultaneously in what critics have described as &amp;quot;the definitive manifestation of post-coherent spirituality.&amp;quot; She recently cultivated a 48-hour transcendental state achieved through microdosed neurotransmitters harvested from the pineal glands of Amazonian tree frogs, administered via jade needles sanctified by no fewer than twelve distinct shamanic traditions. Then there&#39;s &amp;quot;MinimalMaximalist,&amp;quot; who conducts methodical exhibitions of possessions curated to communicate wealth while paradoxically espousing the virtue of simplicity—a specialized but remarkably profitable market segment catering to those who fetishize the simulacrum of spiritual achievement. His latest anti-materialist manifesto was delivered from his $28 million bunker constructed entirely of reclaimed meteor fragments, each meteorite certified to have passed through a specific constellation deemed auspicious by his team of private astrologers.&lt;/p&gt;
&lt;p&gt;I myself am preparing my next salvo: a proprietary strain of bioluminescent plankton genetically modified to respond exclusively to my own bioelectrical signature, cultivated in a private sea constructed to replicate the precise chemical composition of pre-industrial oceans. It will generate approximately 14 minutes of algorithmic superiority before BlissBaroness debuts her collection of dew harvested from extinct flowers recreated through paleobotanical DNA reconstruction, each droplet stored in vials crafted from crystallized tears of anonymous artisanal glassblowers who are contractually forbidden from experiencing happiness during the creation process.&lt;/p&gt;
&lt;p&gt;The desperation escalates quarterly. Elite influencers now purchase temporally exclusive sunsets—proprietary atmospheric modifications that ensure unique light spectrums visible only to those who&#39;ve paid for algorithmic access. HedoniGram recently introduced consciousness franchising, allowing A-list experience architects to monetize their neural patterns, temporarily lending their precise neurochemical configurations to Aspirants willing to relinquish 38% of their gross annual income for seventy-two hours of simulated elite consciousness. But the ultimate luxury—the pinnacle of hedonic distinction—has become the Temporary Non-Existence Package: a quantum-suspended state of complete ego dissolution where one&#39;s consciousness is temporarily extracted from the body and stored in proprietary cloud servers while automated algorithms maintain your social presence. For a mere $42 million per hour, you can experience the bliss of absolute non-being while your digital avatar continues performing aspirational leisure at peak engagement metrics. &amp;quot;Transcend the self while optimizing your brand,&amp;quot; promises the marketing campaign. The irony of paying exorbitant sums to temporarily cease existing while ensuring your online persona thrives is lost on no one, yet remains the most coveted experience among the ultra-elite—the cessation of desire transformed into the ultimate status symbol, available exclusively to those whose wealth permits them the ultimate luxury experience of inputless insensate catatonia.&lt;/p&gt;
&lt;p&gt;Amidst this madness, I occasionally contemplate what authentic leisure might entail. To enjoy a sunset without immediate categorization into aspirational taxonomies, monetization strategies, unanalyzed by sentiment prediction software, unquantified according to engagement potential. But such experiential independence now represents the most radical conceivable act: to enjoy without external validation. A fundamental rebellion against the algorithmic superstructure, a rejection of the entire political economy of digitally mediated experience. It&#39;s almost unthinkable.&lt;/p&gt;
&lt;p&gt;But the thought is fleeting. The market simply won&#39;t allow it. Contentment just doesn&#39;t scale. Presence lacks virality potential. Sufficiency demonstrates insufficient ROI. You cannot A/B test genuine satisfaction for optimal audience retention. You cannot attach affiliate marketing to genuine enlightenment. You cannot secure funding for content that acknowledges the inherent emptiness of desire. In an attention economy where aspiration represents the sole reliable engagement driver, satiation constitutes the only remaining revolutionary act, the final unconquered territory of human cognition. The most profitable leisure is that which presents the simulacrum of fulfillment while reinforcing existing aspirational frameworks, providing the illusory satisfaction of experiential development while leaving the fundamental architecture of desire dependency perfectly intact.&lt;/p&gt;
&lt;p&gt;And so, in this paradigm, that which cannot be quantified, which fails to generate engagement metrics, which resists algorithmic categorization... effectively ceases to exist. Your private contentment represents wasted neurological capacity. Unmonetized joy constitutes the sole remaining heresy, an affront to the techno-capitalist orthodoxy that has rightfully colonized every dimension of modern experiential existence.&lt;/p&gt;
&lt;p&gt;Which brings me back to the present moment. My neural implant activates. Performance commences. I clear my throat, activating my neural broadcast, concentrating on the algorithmically determined aspirational signifiers, those strategically selected to maximize both my engagement metrics, envy, and neurochemical dependency among my aspirants.&lt;/p&gt;
&lt;p&gt;&amp;quot;True sophistication demands absolute rarity,&amp;quot; I declare again, my voice modulated to the precise frequency demonstrated to trigger nucleus accumbens response in my demographic targets.&lt;/p&gt;
&lt;p&gt;I market inaccessibility to alleviate your status anxiety, momentarily. You finance the privilege of experiencing artificial proximity through my manufactured abundance. Together we accelerate the hedonic treadmill.&lt;/p&gt;
&lt;p&gt;Because ultimately, aspiration is the sole remaining mechanism through which one might simulate authentic fulfillment. Externalize your desire fulfillment through my experience. The treadmill never stops—it only accelerates, carrying us all toward increasingly boutique Byzantine manifestations of the same fundamental emptiness, each more exquisitely crafted than the last.&lt;/p&gt;
</description>
      <pubDate>Tue, 10 Dec 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/sybaritic_economy/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/sybaritic_economy/</guid>
    </item>
    
    <item>
      <title>Process Reward Models</title>
      <description>&lt;h1 id=&quot;process-reward-models&quot; tabindex=&quot;-1&quot;&gt;Process Reward Models&lt;/h1&gt;
&lt;p&gt;Reasoning models are all the rage with the kids these days, and in particular the training data behind them is a hot topic. One particular case of this is the &lt;a href=&quot;https://arxiv.org/abs/2405.15489&quot;&gt;process reward models&lt;/a&gt; which are a type of reward model used in complex reasoning and decision-making tasks where evaluating intermediate steps is crucial to achieving the desired outcome. In short we want to have a model that can help guide the reasoning of another model to tell it when it&#39;s on the right track or off track.&lt;/p&gt;
&lt;p&gt;The source code for this post is available &lt;a href=&quot;https://github.com/sdiehl/prm&quot;&gt;https://github.com/sdiehl/prm&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;New approaches to scaling inference have recently led to breakthrough performance improvements in the world of LLMs. Through the use of basic sampling and search strategies, particularly when combined with custom verifiers, scoring functions, and process reward models, we&#39;re now seeing small models outperform vastly larger models. Which is quite an exciting development!&lt;/p&gt;
&lt;p&gt;So let&#39;s back up a second and talk about reward models generally. A reward model is a specialized model that acts as an evaluator or critic for other language model outputs. It takes in text (like a response from a language model) and outputs a score indicating how &amp;quot;good&amp;quot; that text is according to correlations in it&#39;s training data about what &amp;quot;good&amp;quot; responses look like.&lt;/p&gt;
&lt;p&gt;For example, a reward model might score responses based on the following criteria:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Helpfulness&lt;/em&gt;: Overall helpfulness of the response to the prompt.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Correctness&lt;/em&gt;: Inclusion of all pertinent facts without errors.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Coherence&lt;/em&gt;: Consistency and clarity of expression.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Complexity&lt;/em&gt;: Intellectual depth required to write response (i.e. whether the response can be written by anyone with basic language competency or requires deep domain expertise).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Verbosity&lt;/em&gt;: Amount of detail included in the response, relative to what is asked for in the prompt.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;a href=&quot;https://huggingface.co/nvidia/Nemotron-4-340B-Reward&quot;&gt;Nemotron-4-340B-Reward&lt;/a&gt; uses this metric to evaluate the quality of a user/assistant interaction.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; openai &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; OpenAI

client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; OpenAI&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  base_url &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;https://integrate.api.nvidia.com/v1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  api_key &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;...&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

completion &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;completions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;nvidia/nemotron-4-340b-reward&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the capital of France?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Paris is the capital of France.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;completion&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output of the reward model is an enumeration of the grade of the user/assistant interaction.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;helpfulness&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.1875&lt;/span&gt;
correctness&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.109375&lt;/span&gt;
coherence&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.46875&lt;/span&gt;
complexity&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.330078125&lt;/span&gt;
verbosity&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.38671875&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reward models are typically trained on human preferences - showing the model pairs of responses and teaching it which one humans preferred. This allows the reward model to learn what makes a &amp;quot;good&amp;quot; response according to human judgment.&lt;/p&gt;
&lt;p&gt;A variant of this is the &lt;a href=&quot;https://arxiv.org/abs/2405.15489&quot;&gt;Process Reward Model&lt;/a&gt;, which evaluates the quality of each step in a reasoning process. Process Reward Models are a type of reward model used in complex reasoning and decision-making tasks where evaluating intermediate steps is crucial to achieving the desired outcome. Unlike Outcome Reward Models (ORMs) that only consider the final result, PRMs provide feedback at each stage, capturing the value of intermediate actions and offering a more granular perspective on the problem-solving process.&lt;/p&gt;
&lt;h2 id=&quot;processes&quot; tabindex=&quot;-1&quot;&gt;Processes&lt;/h2&gt;
&lt;p&gt;By a &lt;em&gt;process&lt;/em&gt; we mean a sequence of steps that a actor (or language model) takes to solve a problem. We&#39;ll assume these are set of natural language steps encoded as tokens which &amp;quot;guide&amp;quot; the actor towards a solution.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Calculate the area of a circle with radius 5.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;We know that the formula for the area of a circle is $&#92;pi r^2$.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;The radius of the circle is 5, so we can substitute that in.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;So the area of the circle is $&#92;pi(5)^2$ = $&#92;boxed{25&#92;pi}$.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we have a problem specification&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(x&#92;): problem specification&lt;/li&gt;
&lt;li&gt;&#92;(z_{1:T} &#92;in &#92;mathcal{S}^T&#92;): sequence of steps taken to solve the problem&lt;/li&gt;
&lt;li&gt;&#92;(y &#92;in &#92;mathcal{Y}&#92;): final answer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A chain of thought is then a sequence such as &#92;((x, z_{1:T}, y)&#92;) like:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
S^4 = &#92;{x, z_1, z_2, z_3, z_4, y&#92;}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Might look like:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{align*}&lt;br /&gt;
x &amp;amp;= &#92;text{Find the area of a circle with radius 5} &#92;&#92;&lt;br /&gt;
z_1 &amp;amp;= &#92;text{Let&#39;s use the formula for circle area} &#92;&#92;&lt;br /&gt;
z_2 &amp;amp;= &#92;text{The formula is} A = &#92;pi r^2 &#92;&#92;&lt;br /&gt;
z_3 &amp;amp;= &#92;text{Substituting } r=5 &#92;&#92;&lt;br /&gt;
z_4 &amp;amp;= &#92;text{Computing } A = &#92;pi(5)^2 = 25&#92;pi &#92;&#92;&lt;br /&gt;
y &amp;amp;= 25&#92;pi&lt;br /&gt;
&#92;end{align*}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The probability of a chain of thought &#92;((x, z_{1:T}, y)&#92;) is given by&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
p(y|x) = &#92;mathbb{E}_z p(y | x, z)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;For some problems we will have a verifier function that can be used to evaluate the correctness of a final answer.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{Ver}_x : &#92;mathcal{Y} &#92;to &#92;{0, 1&#92;}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;We may also have a verifier function that can be used to evaluate the correctness of a sequence of steps.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;text{Ver}_x : &#92;mathcal{S}^T &#92;to &#92;{0, 1&#92;}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;These functions are possible for certain domains like mathematical reasoning, it may be possible to formally verify proofs or calculations but this still remains an active area of research and is at minimum a very hard engineering problem and intractable in the general case (a la Rice&#39;s theorem). However we can still train probabilistic models to evaluate the quality of a chain of thought and in practice this can guide the inference of the model towards better behavior even in the case where the verifier function is intractable.&lt;/p&gt;
&lt;p&gt;Some other terminology:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;non-sequitor&lt;/strong&gt; is a step that does not contribute to the solution of the problem.&lt;/li&gt;
&lt;li&gt;A process is said to be &lt;strong&gt;complete&lt;/strong&gt; if it has no non-sequitors.&lt;/li&gt;
&lt;li&gt;A process is said to be &lt;strong&gt;correct&lt;/strong&gt; if it reaches the correct final answer.&lt;/li&gt;
&lt;li&gt;A process is said to be &lt;strong&gt;valid&lt;/strong&gt; if and only if it is complete and correct.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is somewhat subtle, because a process can be correct and incomplete or incorrect and complete. For example, a process could reach the right answer but include unnecessary or irrelevant steps (correct but incomplete). Conversely, a process could follow a perfectly logical sequence of steps but arrive at the wrong conclusion due to a calculation error or faulty assumption (incorrect but complete). The ideal process is both complete and correct - containing only relevant steps that contribute to reaching the right answer.&lt;/p&gt;
&lt;p&gt;We will also use the term &lt;em&gt;trajectory&lt;/em&gt; to refer to families of processes that start with the same initial state and may end with different final states. Formally, we denote a trajectory as &#92;(&#92;mathcal{T}(x)&#92;) which represents the set of all possible process sequences &#92;(S^T&#92;) that begin with initial state &#92;(x&#92;):&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;mathcal{T}(x) = {S^T &#92;in &#92;mathcal{S}^T : S^T_1 = x}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The goal of a &lt;em&gt;process reward model&lt;/em&gt; is to learn a mapping from a trajectory to a reward value (i.e. &#92;(r : &#92;mathcal{S}^T &#92;to &#92;mathbb{R}&#92;)) in order to guide sampling of valid processes.&lt;/p&gt;
&lt;p&gt;This is contrast to a &lt;em&gt;outcome reward model&lt;/em&gt; which maps from a final answer to a reward value (i.e. &#92;(r : &#92;mathcal{Y} &#92;to &#92;mathbb{R}&#92;)) which is common in traditional post-training in which the models learn to generate final answers that are correct.&lt;/p&gt;
&lt;h2 id=&quot;generating-processes&quot; tabindex=&quot;-1&quot;&gt;Generating Processes&lt;/h2&gt;
&lt;p&gt;The simplest and most accurate way way to generate processes is to use a human experts to solve the problem and write the process. This however is not scalable so we need to develop other methods.&lt;/p&gt;
&lt;p&gt;A major breakthrough in the field came when researchers discovered that models could learn to reason better by generating and refining their own chains of thought through reinforcement learning, rather than just imitating human-written examples. This was a pivotal realization that unlocked the ability to scale reasoning capabilities far beyond what was possible with human demonstrations alone. The discovery that models could effectively bootstrap their own reasoning process through RL represented a fundamental shift in how we now approach training language models to solve complex problems.&lt;/p&gt;
&lt;p&gt;The simplest way to start bootstrapping these processes is to use the model itself to generate them using the &lt;em&gt;guess and check&lt;/em&gt; method. Assuming we have a problem specification &#92;(x&#92;) and a set of known answers &#92;(&#92;mathcal{Y}&#92;) we can generate a set of trajectories &#92;(&#92;mathcal{T}(x)&#92;) by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Generate multiple solution trajectories (typically around 15 per problem) by having the model attempt different reasoning paths&lt;/li&gt;
&lt;li&gt;For each intermediate step in a trajectory, sample multiple possible completions (around 16 per step)&lt;/li&gt;
&lt;li&gt;Evaluate each completion - if any completion reaches the correct final answer, label that step as positive (&lt;code&gt;+&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;If all completions for a step fail to reach the correct answer, label that step as negative (&lt;code&gt;-&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We let the langauge model explore different paths and then select the paths that lead to the correct answers. There is no guarantee that this will find the minimal process (or even a valid process) but it will find some correct processes and this is good starting point.&lt;/p&gt;
&lt;h2 id=&quot;building-a-process-reward-model&quot; tabindex=&quot;-1&quot;&gt;Building a Process Reward Model&lt;/h2&gt;
&lt;p&gt;We&#39;ll create a training pipeline that builds models to evaluate the quality of each step in a chain of reasoning based on a set of training examples about mathematical reasoning. We&#39;re going to use the base &lt;code&gt;Mistral-7B-0.3&lt;/code&gt; model and train it to act as a PRM as it has generally good performance and is small enough to train on a modest GPU.&lt;/p&gt;
&lt;p&gt;The training process involves fine-tuning the model to predict whether each step in a reasoning chain is helpful (+) or unhelpful (-) towards reaching the correct solution. This binary classification approach allows the model to learn what constitutes effective reasoning steps across different types of problems.&lt;/p&gt;
&lt;p&gt;You&#39;ll need to install the &lt;code&gt;prm&lt;/code&gt; library and the &lt;code&gt;transformers&lt;/code&gt;, &lt;code&gt;datasets&lt;/code&gt;, and &lt;code&gt;torch&lt;/code&gt; libraries. The PRM library provides specialized training utilities for process reward models, while the Hugging Face libraries handle the underlying model architecture and training infrastructure.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; transformers datasets torch
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; git+https://github.com/sdiehl/prm.git
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, let&#39;s set up our training script with the necessary imports and configurations. We&#39;ll need to configure several system-level settings to handle the large model efficiently, including proper multiprocessing setup and tokenizer parallelism. The model initialization includes setting the pad token to match the EOS token (a common practice for causal language models) and disabling the KV cache to save memory during training.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrainingArguments
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Dataset
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; prm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;trainer &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ProcessRewardTrainer
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; os

&lt;span class=&quot;token comment&quot;&gt;# Configure system settings&lt;/span&gt;
torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;multiprocessing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_sharing_strategy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;file_system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
os&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;environ&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;TOKENIZERS_PARALLELISM&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;false&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize model and tokenizer&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mistralai/Mistral-7B-v0.3&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mistralai/Mistral-7B-v0.3&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Configure model settings&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;use_cache &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token_id

&lt;span class=&quot;token comment&quot;&gt;# Setup the step string format template&lt;/span&gt;
STEP_TEMPLATE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Step {i}: {step}&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To train our Process Reward Model, we need high-quality training data that demonstrates both correct reasoning chains and common pitfalls. Let&#39;s create a simple dataset with mathematical reasoning steps. Each example consists of an input problem, a sequence of reasoning steps, and corresponding labels indicating whether each step contributes positively to the solution. This structure allows the model to learn the characteristics of effective problem-solving processes.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;raw_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Solve the equation: 2x + 6 = 14&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;First, subtract 6 from both sides:&#92;n2x + 6 - 6 = 14 - 6&#92;n2x = 8&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;Then, divide both sides by 2:&#92;n2x ÷ 2 = 8 ÷ 2&#92;nx = 4&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;Check the solution:&#92;n2(4) + 6 = 14&#92;n8 + 6 = 14&#92;n14 = 14 ✓&quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll create a function to convert the examples into training examples. This function processes each example by accumulating steps and formatting them into a structured text format that the model can understand. By iterating over each step, we build a conversation-like history that the model uses to learn the sequence of reasoning.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_training_examples&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Convert step-by-step reasoning into training examples&quot;&quot;&quot;&lt;/span&gt;
    training_examples &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; example &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; examples&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        prompt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        accumulated_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current_step &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            
            &lt;span class=&quot;token comment&quot;&gt;# Build conversation history&lt;/span&gt;
            full_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prompt
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; step &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                full_text &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&#92;n&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;STEP_TEMPLATE&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; step&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
            
            training_examples&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; full_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; training_examples
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we need to preprocess the training examples into a format suitable for the model. This involves tokenizing the text and converting labels into a format that the model can use to learn. The preprocessing function handles truncation and padding to ensure that all inputs are of a consistent length, which is crucial for efficient batch processing during training.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;preprocess_function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Convert examples to model inputs&quot;&quot;&quot;&lt;/span&gt;
    encoded &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;max_length&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2048&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    reward_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_tokens_to_ids&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;label&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; label &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;attention_mask&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;attention_mask&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clone&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;reward_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; reward_tokens
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the preprocessing function defined, we can now create and process the dataset into a format that the model can train on. This step involves converting the raw dataset into a structured format using the Hugging Face &lt;code&gt;Dataset&lt;/code&gt; class, which provides efficient data handling and processing capabilities.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create dataset&lt;/span&gt;
paired_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_list&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;create_training_examples&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;raw_dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Process dataset&lt;/span&gt;
processed_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; paired_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    preprocess_function&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    batched&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    remove_columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;paired_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;column_names&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    desc&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Processing dataset&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll configure the training arguments. These settings control various aspects of the training process, such as the number of epochs, batch size, learning rate, and logging. Proper configuration is essential to ensure that the model trains efficiently and effectively, especially when dealing with large models like Mistral.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;prm_output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;logs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    save_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;epoch&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    remove_unused_columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Increased for Mistral&#39;s size&lt;/span&gt;
    fp16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Enable mixed precision training&lt;/span&gt;
    local_rank&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the training arguments set, we can initialize the trainer and run the training process. The &lt;code&gt;ProcessRewardTrainer&lt;/code&gt; class handles the training loop, including data loading, model updates, and logging. It also includes error handling to manage any issues that arise during training, such as memory constraints or unexpected errors.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Initialize trainer&lt;/span&gt;
trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ProcessRewardTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;processed_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    data_collator&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Train with error handling&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;prm_output/final_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;prm_output/final_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; Exception &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Training failed with error: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; gc
    gc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;collect&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty_cache&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; e
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s take a look at the internals of the PRM training process. We have to handle the reward tokens and compute the loss a bit differently than a normal causal language model. The PRM training process uses a special token system to handle rewards, which involves converting &amp;quot;+&amp;quot; and &amp;quot;-&amp;quot; tokens to their corresponding vocabulary IDs. This conversion is crucial for the model to understand and predict the reward values associated with each step.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RewardDataCollator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Convert &quot;+&quot; and &quot;-&quot; tokens to their vocabulary IDs&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pos_token_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_tokens_to_ids&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neg_token_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_tokens_to_ids&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reward_token_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pos_token_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neg_token_id&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our system uses &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; as special tokens to represent positive and negative rewards respectively. These are converted to their corresponding token IDs in the model&#39;s vocabulary. The data collator is responsible for preparing batches of data during training, ensuring that the reward tokens are correctly aligned with the input sequences.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; features&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    batch &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Convert boolean labels to reward token IDs&lt;/span&gt;
    batch&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;reward_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pos_token_id &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; label &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neg_token_id 
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; label &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; f &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; features&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Track step position information&lt;/span&gt;
    batch&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; f &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; features&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    batch&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; f &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; features&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The loss computation process is handled in the &lt;code&gt;compute_loss&lt;/code&gt; method of &lt;code&gt;ProcessRewardTrainer&lt;/code&gt;. This method extracts metadata from the inputs, such as reward tokens and step indices, and calculates the loss based on the model&#39;s predictions. The loss function is designed to focus on the reward tokens, ensuring that the model learns to predict the correct reward values for each step.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compute_loss&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_outputs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;kwargs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Extract metadata&lt;/span&gt;
    reward_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;reward_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    step_idx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    is_final_step &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; inputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll find the reward positions. The model uses placeholder tokens to mark where rewards should be predicted. The code finds these positions in the input sequence, allowing the model to focus on the relevant parts of the sequence when calculating the loss.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;# Get model predictions&lt;/span&gt;
    outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; outputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logits
    
    &lt;span class=&quot;token comment&quot;&gt;# Locate placeholder tokens&lt;/span&gt;
    placeholder_positions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;where&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        inputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;placeholder_token_id
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    batch_indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; placeholder_positions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Which items in batch&lt;/span&gt;
    seq_indices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; placeholder_positions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Position within sequence&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The model uses placeholder tokens to mark where rewards should be predicted. The code finds these positions in the input sequence.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;# Extract logits at placeholder positions&lt;/span&gt;
    placeholder_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;batch_indices&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_indices&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Focus only on reward token logits (+/-)&lt;/span&gt;
    reward_logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; placeholder_logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reward_token_ids&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll get the target labels and convert them to binary format. This step involves mapping the reward tokens to binary labels, which are used to calculate the cross-entropy loss. The binary format simplifies the loss calculation, allowing the model to focus on predicting the correct reward values.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;# Get target labels&lt;/span&gt;
    labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; reward_tokens&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;batch_indices&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Convert to binary format (0 for positive, 1 for negative)&lt;/span&gt;
    binary_labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;labels &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reward_token_ids&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;long&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Calculate cross entropy loss&lt;/span&gt;
    loss &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;loss_fn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;reward_logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; binary_labels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we&#39;ll optionally calculate accuracy for watching the loss in Weights &amp;amp; Biases. This step provides additional metrics for monitoring the training process, allowing us to track the model&#39;s performance and make adjustments as needed.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; return_outputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;no_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            predictions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; reward_logits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            acc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; binary_labels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; loss&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;logits&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; loss&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;acc&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; acc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;step_idx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; step_idx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;is_final_step&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; is_final_step&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;inference&quot; tabindex=&quot;-1&quot;&gt;Inference&lt;/h2&gt;
&lt;p&gt;Now we can setup and &lt;code&gt;infer.py&lt;/code&gt; script to load the model and use it to score chains of thought. The inference process involves loading the trained model and tokenizer, preparing the input data, and evaluating each step in a reasoning chain. This allows us to assess the quality of the reasoning process and identify areas for improvement.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoTokenizer
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch

&lt;span class=&quot;token comment&quot;&gt;# Load your trained model and tokenizer&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output/final_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output/final_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The core of our inference system is the step probability function. This function evaluates the likelihood of a step being positive or negative, providing a quantitative measure of the step&#39;s contribution to the overall reasoning process. By calculating probabilities for each step, we can identify which steps are most effective and which may need revision.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_step_probability&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Combine input and accumulated steps&lt;/span&gt;
    full_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; input_text
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; step &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        full_text &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&#92;nStep &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;step&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Prepare inputs&lt;/span&gt;
    inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        full_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 
        return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 
        truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 
        max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2048&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;no_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Set up reward token masking&lt;/span&gt;
        reward_token_ids &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_tokens_to_ids&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_tokens_to_ids&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Create logits mask to only allow reward tokens&lt;/span&gt;
        logits_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;full&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vocab_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-inf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        logits_mask&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;reward_token_ids&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
        
        &lt;span class=&quot;token comment&quot;&gt;# Get model outputs&lt;/span&gt;
        outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; do_sample&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; outputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; logits_mask
        
        &lt;span class=&quot;token comment&quot;&gt;# Calculate probabilities&lt;/span&gt;
        probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        reward_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; reward_token_ids&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; reward_probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; reward_probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Define your problem and steps&lt;/span&gt;
example &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Find all values of x in the interval [0, 2π] that satisfy sin(x) = 1/2.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;To solve sin(x) = 1/2, we need to find the reference angle first.&#92;nα = arcsin(1/2) = π/6&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;Since sin(x) is positive in quadrants I and II, and we&#39;re looking in [0, 2π], we&#39;ll have two solutions:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;First solution: x = α = π/6 ≈ 0.524 radians&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;Second solution: x = π - α = π - π/6 = 5π/6 ≈ 2.618 radians&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;We can verify these solutions:&#92;nsin(π/6) = sin(5π/6) = 1/2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;Therefore, the solutions in [0, 2π] are x = π/6, 5π/6.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Evaluate steps progressively&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Question: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;input&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#92;n&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
accumulated_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; step &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;step&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After Step &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Accumulated steps:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; acc_step &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Step &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;acc_step&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_step_probability&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;example&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; accumulated_steps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&#92;nProbabilities: + = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;+&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, - = &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;probs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;-&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#92;n&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;80&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The model evaluates each step in the context of all previous steps, providing probabilities for positive and negative rewards. A higher probability for &amp;quot;+&amp;quot; indicates that the model considers the step to be correct and helpful in the reasoning process. The evaluation is cumulative, meaning it considers how each new step builds upon previous ones.&lt;/p&gt;
&lt;p&gt;The masking mechanism ensures that the model only predicts reward tokens (&amp;quot;+&amp;quot; or &amp;quot;-&amp;quot;) by setting all other token probabilities to negative infinity. This forces the model to make a binary decision about the quality of each step. You can now use the PRM as either a reward model using trl&#39;s &lt;code&gt;RewardTrainer&lt;/code&gt; or as a step-wise evaluator in your own custom training loops, or even at inference time to guide sampling.&lt;/p&gt;
</description>
      <pubDate>Sun, 01 Dec 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/process_reward/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/process_reward/</guid>
    </item>
    
    <item>
      <title>Digital Dollarization: The Looming Threat to Monetary Sovereignty</title>
      <description>&lt;h1 id=&quot;digital-dollarization%3A-the-looming-threat-to-monetary-sovereignty&quot; tabindex=&quot;-1&quot;&gt;Digital Dollarization: The Looming Threat to Monetary Sovereignty&lt;/h1&gt;
&lt;p&gt;As a new US administration moves toward crypto deregulation, the threat of digital dollarization through the proliferation of dollar-denominated stablecoins has become a serious concern. If stablecoins are allowed to spread unchecked across the global economy, they could create a parallel monetary system that competes with local currencies. Unlike traditional dollarization, which typically occurs through official channels and deliberate policy decisions, stablecoin adoption could happen organically as private market participants seek stability. This seemingly innocent search for financial security masks a profound shift in monetary sovereignty and serious financial contagion risks.&lt;/p&gt;
&lt;p&gt;The mechanism would be deceptively simple: as local citizens lose faith in their domestic currency, they could increasingly turn to US dollar denominated stablecoins for everyday transactions and savings. This would create a self-reinforcing cycle where the domestic currency becomes increasingly irrelevant, while the shadow dollar system grows stronger. Proponents of stablecoins argue that their adoption represents the natural outcome of market efficiency—rational actors choosing monetary stability over volatile domestic currencies. They suggest that preventing access to such digital alternatives artificially protects inefficient monetary systems. However, this market-driven narrative obscures a crucial distinction: while individuals may benefit in the short term from access to dollar-denominated stability, the collective impact could undermine the very monetary sovereignty needed for long-term economic development and crisis response in their domestic economies.&lt;/p&gt;
&lt;p&gt;The claimed efficiency advantages of stablecoins, however, largely stem from regulatory arbitrage rather than genuine technological innovation. These systems effectively operate as shadow payment networks, allowing users to transfer shares in dollar-denominated money market funds while bypassing the know-your-customer and anti-money laundering requirements that legitimate international payment providers must follow. This regulatory evasion, rather than any inherent technological advantage, explains their appeal to certain users—particularly those seeking to avoid financial oversight, including malicious actors. The alleged premise of financial inclusion through stablecoins—lower transaction costs, improved remittance channels, and banking the unbanked—is specious and does not factor in externalities of such a system. While these technologies might reduce friction in global payments by avoiding regulation and controls, they risk creating a new form of financial dependence. The same mechanisms that make stablecoins efficient could entrench economic disparities by cementing the dominance of foreign monetary policy over domestic needs. Some advocates draw parallels between stablecoin adoption and the gold standard, but this comparison only serves to highlight the dangers. The gold standard was a rigid and destabilizing system that amplified economic shocks and transmitted financial crises across borders with devastating efficiency. Far from providing beneficial discipline, it acted as a straitjacket on monetary policy, forcing nations to prioritize maintaining gold convertibility over addressing domestic economic needs. The resulting deflation and economic volatility contributed to the severity of the Great Depression and other major crises. A stablecoin-driven system would create similar, if not worse, constraints. While the gold standard was eventually abandoned as nations recognized its fundamental flaws, a stablecoin-driven dollarization would be far more difficult to unwind once established. The system would permanently restrict monetary policy options without providing any mechanisms for adjustment during crises.&lt;/p&gt;
&lt;p&gt;The consequences of this digital dollarization would be far-reaching. While advocates point to examples like Ecuador&#39;s dollarization as evidence of potential benefits, they overlook a critical distinction: traditional dollarization was a deliberate policy choice made within existing international frameworks. In contrast, stablecoin-driven dollarization would happen gradually and potentially irreversibly, with central banks finding their monetary policy tools increasingly blunted as a growing portion of the economy operates outside their influence. The systemic risks of this shadow dollar system are profound and eerily reminiscent of the Eurodollar markets of the 20th century. Stablecoins, backed primarily by U.S. Treasury bills and commercial paper, create a massive synthetic demand for dollar-denominated assets. This demand can distort Treasury markets and create hidden vulnerabilities in the global financial system. Consider the mechanics: when users in foreign countries buy stablecoins, the issuers must purchase corresponding dollar-denominated assets as backing. This creates a chain of dependencies that ultimately leads back to the U.S. financial system. During times of stress, this chain can become a transmission mechanism for financial contagion. Perhaps most troubling would be the concentration of power this system could create. The United States would gain unprecedented influence over global commerce through the dominance of dollar-denominated stablecoins. This power would extend beyond traditional diplomatic or economic channels, creating a form of digital monetary hegemony that could be wielded with little accountability.&lt;/p&gt;
&lt;p&gt;The stablecoin issuers themselves would become de facto financial institutions with systemic importance, yet they would operate without the traditional oversight applied to banks. These private entities would effectively control monetary systems that could rival the size of many national economies, but without the public accountability of central banks. Supporters of stablecoin proliferation often argue that market competition between issuers would prevent excessive concentration of power and lead to better governance. However, this view underestimates the network effects and economies of scale in monetary systems. The likely outcome would be a few dominant stablecoin issuers wielding unprecedented power over global commerce, operating without the traditional oversight applied to systemically important financial institutions.&lt;/p&gt;
&lt;p&gt;Perhaps most concerning would be the structural implications for global monetary policy coordination. In a stablecoin-dominated world, the Federal Reserve&#39;s monetary policy decisions would have direct and immediate impacts on economies worldwide, regardless of their local economic conditions. Interest rate adjustments, originally intended to manage U.S. domestic economic conditions, would become de facto global policy instruments with asymmetric effects across different regions. This monetary policy transmission mechanism would create significant externalities. When the Federal Reserve adjusts rates to address domestic U.S. economic conditions, these changes would propagate through the stablecoin system instantaneously, potentially destabilizing economies operating under different business cycles or facing contrary economic pressures. The resulting policy spillovers could create procyclical effects, amplifying both booms and busts in economies that might otherwise require countercyclical measures.&lt;/p&gt;
&lt;p&gt;The current international monetary system, while imperfect, allows for policy flexibility through floating exchange rates and independent monetary policy. This flexibility enables countries to respond to local economic conditions, manage domestic credit cycles, and maintain price stability in accordance with their specific circumstances. A stablecoin-driven dollar system would effectively eliminate this crucial economic shock absorber, replacing it with a rigid one-size-fits-all monetary regime. Historical evidence from monetary unions and fixed exchange rate systems demonstrates that optimal currency areas require similar economic structures, synchronized business cycles, and integrated labor markets to function effectively. Most regions of the world do not meet these criteria with respect to the U.S. economy, suggesting that a de facto global dollar standard through stablecoins would create persistent economic tensions and misalignments.&lt;/p&gt;
&lt;p&gt;The technology optimists who suggest that stablecoins could evolve to work within existing regulatory frameworks overlook the fundamental challenge: once a parallel dollar system becomes entrenched, retrofitting regulations would be extraordinarily difficult. The history of financial innovation shows that regulatory frameworks are most effective when established preemptively. The stakes are too high to allow the unchecked growth of a potential shadow dollar system that could undermine global financial stability and national sovereignty. As technology reshapes the nature of money, we must ensure that innovation serves the broader public interest rather than concentrating power in the hands of a few private actors or a single dominant nation.&lt;/p&gt;
&lt;p&gt;This tension between individual and collective outcomes reflects what Keynes called the &amp;quot;fallacy of composition&amp;quot;—what benefits the individual can harm the whole when adopted at scale. Just as a single concert-goer standing to get a better view might improve their experience, when everyone stands, no one sees better and everyone is worse off. Similarly, while individual adoption of dollar-denominated stablecoins may offer immediate benefits in stability and convenience, widespread adoption would create a digital monetary hegemony that leaves everyone more vulnerable to systemic shocks and U.S. policy decisions. The time for a serious global dialogue about the future of digital currency sovereignty is now, before such a shadow dollar system becomes too entrenched to reform. The alternative could be a world where monetary policy becomes the privilege of the few, rather than a tool for the many—a digital extension of dollar imperialism that arrives not through military might or political coercion, but through the seemingly innocent choices of millions seeking financial stability.&lt;/p&gt;
</description>
      <pubDate>Thu, 28 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/stablecoin_dollarization/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/stablecoin_dollarization/</guid>
    </item>
    
    <item>
      <title>Introduction to Fine-tuning Large Language Models</title>
      <description>&lt;h1 id=&quot;introduction-to-fine-tuning-large-language-models&quot; tabindex=&quot;-1&quot;&gt;Introduction to Fine-tuning Large Language Models&lt;/h1&gt;
&lt;p&gt;I&#39;ve been wanting to write an introduction on how to fine-tune large language models for a while now because it&#39;s such a vast and under-documented area, especially when it comes to the libraries, hyperparameters, and techniques involved. In short, fine-tuning allows us to adapt models for specific downstream tasks, improve performance on domain-specific problems, align model behavior with human preferences, and create specialized variants of base models. This is useful for a wide range of applications both in business and pure research.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pre-training, Fine-tuning and Post-training&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is contrast to &lt;strong&gt;pre-training&lt;/strong&gt;, which is the foundational phase in developing a large language model. During pre-training, the model learns general language understanding from massive text corpora. For example, Llama 3 was trained on approximately 15.2 trillion tokens. This process involves initializing random tensors and training them to recognize statistical patterns and relationships between words and concepts.&lt;/p&gt;
&lt;p&gt;The pre-training phase requires immense computational resources, often costing millions of dollars and requiring specialized hardware setups. Organizations typically use hundreds or thousands of GPUs running in parallel for weeks or months to complete pre-training. During this phase, the model learns through self-supervised techniques like causal language modeling, where it predicts the next token in a sequence given the preceding context.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Post-training&lt;/strong&gt; refers to the phase after fine-tuning where additional techniques are applied to further refine and optimize the model&#39;s performance. This phase often includes Reinforcement Learning from Human Feedback (RLHF), where models learn to align their outputs with human preferences through feedback and reward signals. Preference optimization is another key aspect, focusing on adjusting the model to better meet specific user needs or ethical guidelines. These techniques aim to enhance the model&#39;s usability, safety, and efficiency in real-world applications by ensuring that its behavior aligns with desired outcomes and values.&lt;/p&gt;
&lt;p&gt;Fine-tuning builds upon a pre-trained model by further training it on a smaller, task-specific dataset. This process adapts the model&#39;s existing knowledge to specific downstream tasks while preserving the foundational understanding gained during pre-training. Traditional fine-tuning updates all model parameters, which requires substantial computational resources - typically 160-192GB of GPU memory for a 7B parameter model.&lt;/p&gt;
&lt;p&gt;The fine-tuning process is particularly effective because it leverages the knowledge embedded in the pre-trained weights. For example, the &lt;a href=&quot;https://arxiv.org/abs/2305.11206&quot;&gt;LIMA paper&lt;/a&gt; demonstrated that fine-tuning a 65B parameter LLaMA model on just 1,000 high-quality instruction-response pairs could achieve performance comparable to GPT-3. However, this approach requires careful dataset curation and sufficient GPU resources to handle the full model parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main Steps&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In our fine-tuning setup we&#39;re goign to use a pre-trained model and then adapt it to our custom use case.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Preparation&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collect and preprocess data&lt;/li&gt;
&lt;li&gt;Create datasets with instructions and responses&lt;/li&gt;
&lt;li&gt;Split into training, validation, and test sets&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Model Initialization&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Load a pre-trained model and tokenizer&lt;/li&gt;
&lt;li&gt;Configure model architecture and hyperparameters&lt;/li&gt;
&lt;li&gt;Initialize model weights and apply LoRA adaptations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Training&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Configure training parameters&lt;/li&gt;
&lt;li&gt;Train the model on the training dataset&lt;/li&gt;
&lt;li&gt;Monitor training loss and eval loss&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Post-Training (Optional)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Apply additional techniques like DPO, KTO or PPO to align model behavior&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compression and Quantization (Optional)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Apply compression and quantization techniques to optimize model size and inference speed&lt;/li&gt;
&lt;li&gt;Evaluate and test the final model&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In our setup we&#39;ll be using the &lt;code&gt;transformers&lt;/code&gt; library from Hugging Face. It has many knobs and dials to control the training process which can be quite daunting so we&#39;ll go thorugh each class of these arguments and describe both the theory and practice.&lt;/p&gt;
&lt;p&gt;These are generally configured using the &lt;code&gt;TrainingArguments&lt;/code&gt; class. These arguments include settings such as the number of training epochs, batch size, learning rate, and other hyperparameters that influence the training dynamics and performance of the model. Properly configuring these arguments is crucial for achieving optimal results and efficient training.&lt;/p&gt;
&lt;p&gt;Below is the basic scaffold of a model training loop.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; TrainingArguments
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoTokenizer
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SFTTrainer

&lt;span class=&quot;token comment&quot;&gt;# Step 1: Load the training dataset&lt;/span&gt;
train_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; datasets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;path/to/train/dataset&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Step 2: Load the model&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-3B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    quantization_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bnb_config
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Step 2: Load the tokenizer&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-3B-Instruct&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Step 3: Training&lt;/span&gt;
training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ...&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;path/to/save/model&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Step 4: Post-Training&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Step 5: Compression and Quantization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition in order to start working with these python libraries we&#39;ll neeed to install a few dependencies.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install PyTorch&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;torch==2.4.0&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;transformers==4.44.2&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;datasets==2.21.0&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;accelerate==0.33.0&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;evaluate==0.4.2&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;bitsandbytes==0.43.3&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;trl==0.9.6&quot;&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;peft==0.12.0&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install Flash Attention&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; ninja packaging
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Most of the arguments we&#39;ll dicuss below are configured in the &lt;code&gt;TrainingArguments&lt;/code&gt; class. Let&#39;s walk through some of the most important ones.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pre-trained Models&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There two main types of models that are widely used &lt;em&gt;causal&lt;/em&gt; and &lt;em&gt;masked&lt;/em&gt;. Causal language models predict the next token in a sequence of tokens, and the model can only attend to tokens on the left. This means the model cannot see future tokens. On the other hand masked language modeling predicts a masked token in a sequence, and the model can attend to tokens bidirectionally. We&#39;re only going to focus on causal models in this guide.&lt;/p&gt;
&lt;p&gt;Of the causal models, we are going to focus on &lt;strong&gt;autoregressive&lt;/strong&gt; generative pre-trained transformer models. Autoregressive models are a class of models that predict the next token in a sequence by conditioning on the previous tokens. They generate each token step-by-step, using the output of the previous step as part of the input for the next step. They also use an attention mechanism to attend to previous tokens. The theory of generative transformers is well-described by many other sources so I won&#39;t go into much detail here.&lt;/p&gt;
&lt;p&gt;There are many pre-trained transformers that are now available on HuggingFace. The most common ones are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Llama 3.1 Family - Meta&#39;s flaghship open source models ranging from 8B, 70B to 420B parameters, available in base and chat-tuned variants. These are the most widely used models for fine-tuning and have performance that rival closed models.
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct&quot;&gt;https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.1-70B-Instruct&quot;&gt;https://huggingface.co/meta-llama/Llama-3.1-70B-Instruct&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.1-405B-Instruct&quot;&gt;https://huggingface.co/meta-llama/Llama-3.1-405B-Instruct&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Llama 3.2 Family - Meta&#39;s open source models ranging from 1B to 3B parameters. These models punch above their weight class and are very strong for such small sizes.
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct&quot;&gt;https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct&quot;&gt;https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Mistral Family - Including the 7B base model and Mixtral 8x7B mixture-of-experts model. Offers competitive performance with smaller parameter counts.
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3&quot;&gt;https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1&quot;&gt;https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Phi-3.5 - Microsoft&#39;s 2.7B parameter model that shows strong reasoning capabilities despite its small size. These models can work well on linguistic tasks but many people have difficulty training them on coding or other non-linguistic tasks.
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/microsoft/Phi-3.5-mini-instruct&quot;&gt;https://huggingface.co/microsoft/Phi-3.5-mini-instruct&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Gemma - Google&#39;s recently released family of open models in 2B and 7B sizes. Similar performance to Phi-3.5.
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/google/gemma-2b&quot;&gt;https://huggingface.co/google/gemma-2b&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/google/gemma-7b&quot;&gt;https://huggingface.co/google/gemma-7b&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you don&#39;t know where to start, I recommend using the Llama 3.1 family as your base model starting with a 8B parameter model and scaling up to higher parameter counts as needed. However when scaling up to larger models you will need to acquire more VRAM to do the fine-tune which may complicate your training setup and increase your costs.&lt;/p&gt;
&lt;p&gt;There is also the matter of licensing. The models from Meta are under a bespoke Llama license which allows you access to use the model but requires you to comply with certain restrictions when using it for commercial purposes. The Gemma models are also under a bespoke Google license that limits some commercial use. The Mistral models are under a permissive Apache license which allows most uses with attribution. The Phi models are under an MIT license which is also very permissive and allows all uses.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Parameter Count&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The number of parameters in a model is a crucial metric that indicates the model&#39;s size and complexity. It determines the number of weights the model has, which directly influences the amount of data it can process and the computational resources required for training and inference.&lt;/p&gt;
&lt;p&gt;As a convention most models have a name which includes the number of parameters. For example, &lt;code&gt;meta-llama/Llama-3.2-3B-Instruct&lt;/code&gt; has 3 billion parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Instruct Modelels vs Base Models&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Base models are trained on general text data to learn language patterns and generate text, while instruct models are fine-tuned versions specifically trained to follow instructions and respond to prompts. For example, &lt;code&gt;Llama-3.2-3b&lt;/code&gt; is a base model, while &lt;code&gt;Llama-3.2-3b-instruct&lt;/code&gt; is fine-tuned to better understand and execute user instructions.&lt;/p&gt;
&lt;p&gt;The base model is typically used for more general purpose tasks, while the instruct model is used for more specific tasks like chat or instruction following. For the sake of fine-tuning we&#39;ll almost always use the instruct model with a chat template.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GPU Cloud Vendors&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In order to get started with fine-tuning, you&#39;ll need to get your hand on some GPUs. Typically you&#39;ll need either 1-2 high-end GPUs (A100, H100) or 4-8 mid-range GPUs (A100, A6000, V100). It&#39;s usually easier to just get a few H100 SXM GPUs as they have the best performance rather than trying to piece together a cluster of smaller GPUs.&lt;/p&gt;
&lt;p&gt;SXM, PCIe and NVL refer to three different GPU form factors and interconnect technologies that are commonly used in machine learning workloads.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;SXM GPUs are designed specifically for data center and server environments. They feature a direct connection to the CPU via NVLink, enabling much higher memory bandwidth (up to 900 GB/s) compared to traditional connections. These cards are built with an optimized thermal design that allows for sustained high performance under heavy workloads. However, SXM cards require specialized server hardware and are generally more expensive than their PCIe counterparts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;PCIe GPUs, on the other hand, use the standard PCIe slot connection found in most computers and servers. While they have lower memory bandwidth (up to 64 GB/s), they offer more flexible installation options and work with standard server hardware.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;NVL / NVLink is a high-speed interconnect technology developed by Nvidia that allows GPUs to communicate directly with each other, bypassing the slower PCIe bus. This enables faster data transfer and improved performance for parallel computing tasks. NVL has a peak bandwidth of 4,000 gigabytes per second (GBps).&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For fine-tuning large language models, SXM cards generally provide better performance due to their higher memory bandwidth and optimized thermal design. However, PCIe cards are more accessible and still very capable for most fine-tuning workloads. The choice between the two often comes down to budget constraints and specific performance requirements.&lt;/p&gt;
&lt;p&gt;Fortunately, because of a glut of venture capital funding there are many cloud vendors competing for your business. Generally they will rent you a variety of GPU types and sizes. Since most of these companies are operating on razor thin margins, or grossly unprofitable, they are always trying to out-do each other with better pricing and availability. It&#39;s unclear how long this will last, but for now it&#39;s a good time to be a consumer of GPU cloud services.&lt;/p&gt;
&lt;p&gt;The most common GPU types you can get are:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;GPU Type&lt;/th&gt;
&lt;th&gt;VRAM&lt;/th&gt;
&lt;th&gt;Typical Use Case&lt;/th&gt;
&lt;th&gt;Approx. Cost/hr&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;H200 SXM&lt;/td&gt;
&lt;td&gt;141GB&lt;/td&gt;
&lt;td&gt;Large model training (70B+)&lt;/td&gt;
&lt;td&gt;$3.99-4.29&lt;/td&gt;
&lt;td&gt;- Newest GPU&lt;br /&gt;- Optimal performance for large models&lt;br /&gt;- Ideal for 70B parameter models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GH200&lt;/td&gt;
&lt;td&gt;96GB&lt;/td&gt;
&lt;td&gt;Large model training (70B+)&lt;/td&gt;
&lt;td&gt;$2.99-3.29&lt;/td&gt;
&lt;td&gt;- Good performance for large models&lt;br /&gt;- Ideal for 70B parameter models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;H100 NVL&lt;/td&gt;
&lt;td&gt;94GB&lt;/td&gt;
&lt;td&gt;Large model training (70B+)&lt;/td&gt;
&lt;td&gt;$2.99-3.29&lt;/td&gt;
&lt;td&gt;- Optimal performance for large models&lt;br /&gt;- Ideal for 70B parameter models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;H100 SXM&lt;/td&gt;
&lt;td&gt;80GB&lt;/td&gt;
&lt;td&gt;Large model training (70B+)&lt;/td&gt;
&lt;td&gt;$2.79-3.29&lt;/td&gt;
&lt;td&gt;- Good performance for large models&lt;br /&gt;- Ideal for 70B parameter models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;H100 PCIe&lt;/td&gt;
&lt;td&gt;80GB&lt;/td&gt;
&lt;td&gt;Large model training&lt;/td&gt;
&lt;td&gt;$2.49&lt;/td&gt;
&lt;td&gt;- Slightly lower bandwidth than SXM&lt;br /&gt;- Good for 70B parameter models&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A100 SXM&lt;/td&gt;
&lt;td&gt;80GB&lt;/td&gt;
&lt;td&gt;Medium/Large training (7-70B)&lt;/td&gt;
&lt;td&gt;$1.79&lt;/td&gt;
&lt;td&gt;- Great price/performance ratio&lt;br /&gt;- Good for models up to 70B&lt;br /&gt;- Supports Flash Attention 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A100 PCIe/SXM&lt;/td&gt;
&lt;td&gt;40GB&lt;/td&gt;
&lt;td&gt;Medium model training (7-13B)&lt;/td&gt;
&lt;td&gt;$1.29&lt;/td&gt;
&lt;td&gt;- Good for models up to 13B&lt;br /&gt;- Popular for fine-tuning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A10&lt;/td&gt;
&lt;td&gt;24GB&lt;/td&gt;
&lt;td&gt;Small model training (&amp;lt;7B)&lt;/td&gt;
&lt;td&gt;$0.75&lt;/td&gt;
&lt;td&gt;- Good for models up to 7B&lt;br /&gt;- Budget-friendly option&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A6000&lt;/td&gt;
&lt;td&gt;48GB&lt;/td&gt;
&lt;td&gt;Medium model training&lt;/td&gt;
&lt;td&gt;$0.80&lt;/td&gt;
&lt;td&gt;- Good alternative to A100&lt;br /&gt;- Better price/performance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;V100&lt;/td&gt;
&lt;td&gt;16GB&lt;/td&gt;
&lt;td&gt;Small model training (&amp;lt;7B)&lt;/td&gt;
&lt;td&gt;$0.55&lt;/td&gt;
&lt;td&gt;- Older architecture&lt;br /&gt;- Limited by VRAM&lt;br /&gt;- Budget option&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;To get a rough estimate of the cost of fine-tuning a model, we can use the following rough estimates:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fine-tuning a 7B model (24 hours)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;H100 SXM: ~$72/day&lt;/li&gt;
&lt;li&gt;A100 80GB: ~$43/day&lt;/li&gt;
&lt;li&gt;A100 40GB: ~$31/day&lt;/li&gt;
&lt;li&gt;A10: ~$18/day&lt;/li&gt;
&lt;li&gt;V100: ~$13/day&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Fine-tuning a 13B model (24 hours)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;H100 SXM: ~$72/day&lt;/li&gt;
&lt;li&gt;A100 80GB: ~$43/day&lt;/li&gt;
&lt;li&gt;A100 40GB: ~$31/day&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Fine-tuning a 70B model (24 hours)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;H100 NVL: ~$74/day&lt;/li&gt;
&lt;li&gt;H100 SXM: ~$72/day&lt;/li&gt;
&lt;li&gt;2 x A100 80GB: ~$86/day&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The vendors will typically charge you for the GPU time, storage, and data transfer which can add up quickly. The following is a list of popular GPU cloud vendors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://lambdalabs.com/&quot;&gt;Lambda Labs&lt;/a&gt; - Competitive pricing, good availability&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.runpod.io/&quot;&gt;RunPod&lt;/a&gt; - Spot instances, community templates&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vast.ai/&quot;&gt;Vast.ai&lt;/a&gt; - Marketplace for renting GPUs&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.coreweave.com/&quot;&gt;CoreWeave&lt;/a&gt; - Enterprise-focused, high availability&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/&quot;&gt;Google Cloud&lt;/a&gt; - Wide selection, global availability&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/&quot;&gt;AWS&lt;/a&gt; - Extensive options, but can be expensive&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://azure.microsoft.com/&quot;&gt;Azure&lt;/a&gt; - Good enterprise integration&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.paperspace.com/&quot;&gt;Paperspace&lt;/a&gt; - Developer-friendly, Jupyter integration&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.genesiscloud.com/&quot;&gt;Genesis Cloud&lt;/a&gt; - Budget-friendly options&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Unless you are already an existing Google Cloud or AWS customer, you&#39;ll probably want to start with Lambda Labs or RunPod as they are most user-friendly and have the best developer experience.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multi-GPU Setups&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you&#39;re doing more advanced research you meed require even more powerful hardware than individual GPUs, NVIDIA offers specialized solutions like the GH200 Grace Hopper Superchip and HGX clusters. The GH200 is a so called &amp;quot;superchip&amp;quot; that combines a CPU and GPU on a single chip with a 900GB/s NVLink-C2C connection between the processor and accelerator and 576 GB of coherent memory.&lt;/p&gt;
&lt;p&gt;The first generation HGX platforms take this a step further by combining multiple GPUs into a single system optimized for AI workloads. The HGX H100 platform, for example, can scale up to 8 H100 GPUs interconnected with NVLink, providing 3.6TB/s of bisectional bandwidth between GPUs.&lt;/p&gt;
&lt;p&gt;The second generation DGX B200 is features eight NVIDIA Blackwell GPUs with NVLink interconnect, which have 3x the training performance and 15x the inference performance compared to previous generations. The setup includes 1,440GB of total GPU memory, delivers 72 petaFLOPS for training and 144 petaFLOPS for inference.&lt;/p&gt;
&lt;p&gt;For the largest training jobs, organizations can deploy multiple DGX/HGX nodes connected via Quantum-2 InfiniBand networking to effectively create supercomputing systems. These multi-GPU setups can easily run into the multi-million dollar range. For example, the &lt;a href=&quot;https://ai.meta.com/resources/models/llama-3/&quot;&gt;Llama 3&lt;/a&gt; model was trained on 16,000 H100 GPUs ($30k a pop), running for 54 days of continuous training time. The largest clusters in the world today have nearly 100,000 GPUs.&lt;/p&gt;
&lt;p&gt;In general there are three frameworks that you would use to setup multi-GPU training:&lt;/p&gt;
&lt;p&gt;HuggingFace &lt;strong&gt;Accelerate&lt;/strong&gt; provides the simplest path to distributed training, acting primarily as a wrapper around PyTorch&#39;s distributed functionality. While it excels at basic multi-GPU training and inference scenarios and integrates smoothly with both DeepSpeed and FSDP, it has limitations when used alone. Notably, Accelerate offers limited model sharding capabilities and can only use &lt;code&gt;device_map=&amp;quot;auto&amp;quot;&lt;/code&gt; with a single process. This makes it unsuitable for sophisticated model parallelism without leveraging additional tools.&lt;/p&gt;
&lt;p&gt;Microsoft &lt;strong&gt;DeepSpeed&lt;/strong&gt; offers more advanced capabilities through its Zero Redundancy Optimizer, which provides three stages of optimization. Its ZeRO-Infinity feature enables training of massive models by allowing parameter offloading to CPU and NVMe storage, achieving excellent memory efficiency with no parameter replication across GPUs. However, these advantages come with increased complexity in setup and configuration. The CPU/NVMe offloading, while enabling training of larger models, can significantly impact training speed. Users must carefully configure stages and offloading parameters to achieve optimal performance.&lt;/p&gt;
&lt;p&gt;PyTorch &lt;strong&gt;FSDP&lt;/strong&gt; is a middle ground, offering native integration with PyTorch since version 1.11. It can serve as a drop-in replacement for DistributedDataParallel and supports CPU offloading similar to DeepSpeed. However, being a relatively newer solution compared to DeepSpeed, it may require more PyTorch-specific knowledge to implement effectively.&lt;/p&gt;
&lt;p&gt;For most basic multi-GPU training needs, Accelerate provides sufficient functionality. However, when training very large models that exceed single GPU memory capacity, either DeepSpeed or FSDP becomes necessary. DeepSpeed&#39;s longer history and multiple optimization stages make it a proven choice for large-scale training, while FSDP&#39;s tight PyTorch integration might appeal to teams already deeply invested in the PyTorch ecosystem.&lt;/p&gt;
&lt;p&gt;I can&#39;t offer much advice about multi-GPU training as typically if you need to train models of this size you have very specialized needs and will need a bespoke training software setup and custom hardware. So I won&#39;t go into much detail here. There are other guides from the major model labs that go into more detail on this topic. See my post on &lt;a href=&quot;https://www.stephendiehl.com/posts/deepspeed&quot;&gt;DeepSpeed&lt;/a&gt; for some surface level information on this kind of cluster setup.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Parameter-Efficient Fine-tuning (PEFT)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Compared to full-tuning, the most common technique for fine-tuning large language models is &lt;strong&gt;parameter-efficient fine-tuning&lt;/strong&gt; (PEFT). This is in contrast to &lt;strong&gt;full-tuning&lt;/strong&gt;, which involves updating all model parameters during fine-tuning, which requires substantial computational resources and memory. In contrast, PEFT methods have revolutionized how we adapt large language models by dramatically reducing computational requirements while maintaining performance. Rather than updating all model parameters, PEFT methods strategically modify only a small subset of parameters or introduce a limited number of new trainable parameters, making the process much more efficient.&lt;/p&gt;
&lt;p&gt;The most prominent PEFT technique is &lt;strong&gt;Low-Rank Adaptation (LoRA)&lt;/strong&gt;, which has become the de facto standard for efficient model adaptation. LoRA works by adding small trainable rank decomposition matrices to specific layers of the model while keeping the pre-trained weights frozen. This approach can reduce the number of trainable parameters by up to 10,000 times and GPU memory requirements by over 3 times compared to full fine-tuning.&lt;/p&gt;
&lt;p&gt;When combined with 4-bit quantization techniques (&lt;strong&gt;QLoRA&lt;/strong&gt;), the memory savings become even more dramatic. QLoRA enables the training of models as large as 70 billion parameters on consumer-grade hardware like NVIDIA RTX 3090s - a task that would traditionally require 16 or more A100-80GB GPUs. QLoRA achieves this through several key innovations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It performs backpropagation through a frozen, 4-bit quantized pre-trained model into Low-Rank Adapters&lt;/li&gt;
&lt;li&gt;It introduces a new data type called 4-bit NormalFloat (NF4) that is specifically optimized for normally distributed weights&lt;/li&gt;
&lt;li&gt;It implements double quantization to reduce memory footprint by quantizing the quantization constants themselves&lt;/li&gt;
&lt;li&gt;It uses paged optimizers to handle memory spikes during training&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The effectiveness of QLoRA stems from its ability to maintain full 16-bit fine-tuning task performance while drastically reducing memory requirements. This makes it possible to fine-tune large models on a single GPU that would normally require a cluster of high-end GPUs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Parameters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When doing a LoRA fine-tune there are several core parameters which define the behavior of the fine-tuning process:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Rank (r)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The LoRA rank determines the number of rank decomposition matrices used. Think of it like the number of &amp;quot;learning dimensions&amp;quot; the model has available - with a higher rank, the model can learn more nuanced and complex patterns, similar to how having more neurons in a neural network allows it to model more sophisticated relationships. However, just like adding more neurons, a higher rank requires more memory and computation power. The original LoRA paper recommends starting with a rank of 8 as a good balance point. For simpler tasks like basic text classification this may be plenty, but for more complex tasks like creative writing or complex reasoning, you might want to bump it up to 16 or 32. Just keep in mind that higher ranks will need more GPU memory and take longer to train.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Alpha&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The alpha parameter acts as a scaling factor that determines how much influence the LoRA adaptations have compared to the frozen pre-trained weights. Think of it like a volume knob - a higher alpha means the LoRA changes speak louder compared to the original model&#39;s voice, while a lower alpha keeps the changes more subtle. It helps control the magnitude of updates during training. The relationship between rank and alpha is important - typically alpha is set to 2x the rank value as a starting point (so if rank is 8, alpha would be 16). This scaling helps ensure stable training while allowing meaningful updates to occur, kind of like finding the sweet spot on that volume knob where you can clearly hear the changes without drowning out the original sound.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Dropout&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Dropout is a regularization technique used in machine learning to prevent overfitting. It works by randomly dropping out (setting to zero) a certain percentage of neurons during training. This forces the remaining neurons to learn more robust and generalized features, reducing their reliance on specific patterns that may not generalize well to unseen data. In the context of LoRA, dropout is applied to the low-rank adaptation matrices during training. By introducing dropout, we ensure that the model does not become overly dependent on any single adaptation path, promoting better generalization and robustness. As a very rough analogy, think of it like a football team where occasionally, some players are benched randomly during practice. This forces the remaining players to step up and improve their skills, making the whole team stronger and more versatile.&lt;/p&gt;
&lt;p&gt;Common default values for LoRA dropout typically range from 0.0 to 0.3. A dropout rate of 0.05 is often a good starting point, providing a balance between regularization and maintaining enough active neurons for effective learning. If the model still shows signs of overfitting, you might increase the dropout rate to 0.2 or 0.3. If you are training a large number of layers in your LoRA adapter, you might want to increase the dropout rate to compensate for the increased number of layers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Bias&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The bias parameter in LoRA refers to the additive term added to the output of the low-rank adaptation matrices. It allows the model to learn an additional offset from the input, providing a more flexible and dynamic response. Bias terms are particularly useful in situations where the input data has a non-zero baseline or when the relationship between input and output is not purely linear. By adding a bias, the model can better capture and adjust for such variations, improving its overall performance and generalization.&lt;/p&gt;
&lt;p&gt;The default value for bias is &lt;code&gt;&amp;quot;none&amp;quot;&lt;/code&gt; which means that no bias is added. However, in some cases you may want to add a bias term to the LoRA adaptation matrices. The other options are &lt;code&gt;&amp;quot;all&amp;quot;&lt;/code&gt; and &lt;code&gt;&amp;quot;lora_only&amp;quot;&lt;/code&gt; which add a bias term to all the matrices or just the LoRA matrices respectively.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;LoRA Target Modules&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The selection of target modules in LoRA represents a critical architectural decision that directly impacts both the model&#39;s adaptability and computational efficiency. In transformer-based architectures, these modules consist of various projection matrices and components that handle different aspects of the model&#39;s processing pipeline.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The embedding layer (&lt;code&gt;embed_tokens&lt;/code&gt;) serves as the model&#39;s initial interface with input tokens, transforming discrete token IDs into continuous vector representations. While it&#39;s possible to include embeddings as a LoRA target, this is generally discouraged as it can significantly increase memory usage without proportional gains in model performance. The embedding layer typically contains a large number of parameters due to vocabulary size, making it less efficient for LoRA adaptation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The normalization layers (&lt;code&gt;norm&lt;/code&gt;) help stabilize the network&#39;s internal representations by standardizing activation values. These layers contain relatively few parameters and are crucial for maintaining stable training dynamics. However, they are rarely targeted for LoRA adaptation because their role is primarily statistical normalization rather than learning complex patterns. Including norm layers in LoRA targets typically offers minimal benefit while potentially destabilizing training.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The language modeling head (&lt;code&gt;lm_head&lt;/code&gt;) is responsible for converting the model&#39;s internal representations back into vocabulary-sized logits for token prediction. While this layer is crucial for the final output, including it as a LoRA target is generally unnecessary. The &lt;code&gt;lm_head&lt;/code&gt; often shares weights with the embedding layer through weight tying, and adapting it separately can break this symmetry without providing significant benefits.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The core attention mechanism components remain the most effective targets for LoRA adaptation. The query projection matrix (&lt;code&gt;q_proj&lt;/code&gt;) transforms input embeddings into query vectors, determining how the model searches for relevant information within its context. The key projection matrix (&lt;code&gt;k_proj&lt;/code&gt;) creates key vectors that help establish relationships between different parts of the input, while the value projection matrix (&lt;code&gt;v_proj&lt;/code&gt;) transforms the input into value vectors that contain the actual information to be extracted. These three projections form the cornerstone of the self-attention mechanism, with &lt;code&gt;q_proj&lt;/code&gt; and &lt;code&gt;v_proj&lt;/code&gt; often being the most crucial targets for adaptation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The output projection matrix (&lt;code&gt;o_proj&lt;/code&gt;) processes the combined attention outputs before they move to subsequent layers. This transformation ensures the attention mechanism&#39;s output maintains compatibility with the model&#39;s broader architecture. In models with more complex architectures, you&#39;ll also find the upward projection (&lt;code&gt;up_proj&lt;/code&gt;) and downward projection (&lt;code&gt;down_proj&lt;/code&gt;) matrices, which handle dimensionality transformations between layers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When using LoRA for fine-tuning, it&#39;s recommended to start with a focused approach targeting just the attention components:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;lora_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;q_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;v_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more demanding tasks or when initial results aren&#39;t satisfactory, you can expand to include additional projection matrices:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;lora_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;q_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;k_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;v_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;o_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to fine-tune a model using PEFT, you&#39;ll need to install the &lt;code&gt;transformers&lt;/code&gt;, &lt;code&gt;peft&lt;/code&gt;, and &lt;code&gt;trl&lt;/code&gt; libraries. Here&#39;s an example of how to configure and fine-tune a LoRA model.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; peft &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PeftModel

&lt;span class=&quot;token comment&quot;&gt;# QLoRA parameters&lt;/span&gt;
lora_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                   &lt;span class=&quot;token comment&quot;&gt;# LoRA attention dimension&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# LoRA scale factor&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Dropout probability&lt;/span&gt;
    bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;none&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    task_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;CAUSAL_LM&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

lora_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_peft_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lora_config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
lora_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print_trainable_parameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# trainable params: 1,572,864 || all params: 332,769,280 || trainable%: 0.472659014678278&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Most often tweaking LoRA hyperparameters is not the most efficient use of your time. So I recommend starting with the defaults and only adjusting if you are seeing clear signs of overfitting or underfitting. The biggest gains will most often come from better dataset curation rather than hyperparameter tuning. However if you are going sweep through values you can try the following sets of parameters that might give good results, in increasing order from the default:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;r=16, lora_alpha=16&lt;/li&gt;
&lt;li&gt;r=16, lora_alpha=32&lt;/li&gt;
&lt;li&gt;r=32, lora_alpha=64&lt;/li&gt;
&lt;li&gt;r=64, lora_alpha=128&lt;/li&gt;
&lt;li&gt;r=128, lora_alpha=256&lt;/li&gt;
&lt;li&gt;r=256, lora_alpha=512&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, to use a rank of 256 and an alpha of 128 over all linear layers use the following configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;peft_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;none&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;all-linear&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    task_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;CAUSAL_LM&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quantized LoRA&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To use QLoRA, you&#39;ll also need to install the &lt;code&gt;bitsandbytes&lt;/code&gt; library as well.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# 4-bit quantization config&lt;/span&gt;
bnb_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; BitsAndBytesConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    load_in_4bit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bnb_4bit_quant_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;nf4&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bnb_4bit_compute_dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float16&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bnb_4bit_use_double_quant&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Loading the Model&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When loading a pre-trained model from Hugging Face&#39;s model hub, you&#39;ll use the &lt;code&gt;from_pretrained()&lt;/code&gt; method. This method downloads the model weights and configuration from the specified repository. The model architecture and weights are automatically determined based on the repository name.&lt;/p&gt;
&lt;p&gt;You can control the precision of the loaded model weights using the &lt;code&gt;torch_dtype&lt;/code&gt; parameter. Common options include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;torch.float32&lt;/code&gt; (default): Full precision, uses more memory but highest accuracy&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.float16&lt;/code&gt;: Half precision, reduces memory usage while maintaining good accuracy&lt;/li&gt;
&lt;li&gt;&lt;code&gt;torch.bfloat16&lt;/code&gt;: Google Brain floating point, better numerical stability than float16&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, to load a model using 4-bit quantization specific to our QLoRA configuration from above:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; peft &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; prepare_model_for_kbit_training

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-3B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    quantization_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bnb_config
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prepare_model_for_kbit_training&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The files that are fetched from Hugging Face will look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;├── config.json
├── generation_config.json
├── model-00001-of-00002.safetensors
├── model-00002-of-00002.safetensors
├── model.safetensors.index.json
├── special_tokens_map.json
├── tokenizer.json
└── tokenizer_config.json
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The files each contain different information:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;config.json&lt;/code&gt;: Contains the model&#39;s architecture configuration and hyperparameters&lt;/li&gt;
&lt;li&gt;&lt;code&gt;generation_config.json&lt;/code&gt;: Stores settings for text generation like temperature and top-p sampling&lt;/li&gt;
&lt;li&gt;&lt;code&gt;model-00001-of-00002.safetensors&lt;/code&gt;: First part of the model weights stored in SafeTensors format&lt;/li&gt;
&lt;li&gt;&lt;code&gt;model-00002-of-00002.safetensors&lt;/code&gt;: Second part of the model weights stored in SafeTensors format&lt;/li&gt;
&lt;li&gt;&lt;code&gt;model.safetensors.index.json&lt;/code&gt;: Index file mapping weight tensors to their locations in the split files&lt;/li&gt;
&lt;li&gt;&lt;code&gt;special_tokens_map.json&lt;/code&gt;: Maps special tokens like &lt;code&gt;&amp;lt;|endoftext|&amp;gt;&lt;/code&gt; to their IDs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tokenizer.json&lt;/code&gt;: Contains the tokenizer&#39;s vocabulary and configuration&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tokenizer_config.json&lt;/code&gt;: Additional tokenizer settings and parameters&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;SafeTensors is a file format designed specifically for storing model weights and tensors. It was created as a safer and more efficient alternative to Python&#39;s pickle format, which can execute arbitrary code and pose security risks. The .safetensors files are typically very large (often multiple gigabytes) as they contain the actual learned parameters (weights) of the model.&lt;/p&gt;
&lt;p&gt;These files store the model&#39;s weights as raw numerical arrays in a binary format, organized by layer. For a transformer model, this includes the attention layer weights and biases, feed-forward network parameters, layer normalization parameters, embedding matrices, and position encoding weights.&lt;/p&gt;
&lt;p&gt;The weights are split across multiple files (like model-00001-of-00002.safetensors) because individual files can become too large to handle efficiently. Each file can be 10GB or larger for modern language models. The splitting also enables parallel downloading and more efficient memory mapping.&lt;/p&gt;
&lt;p&gt;SafeTensors provides significant advantages through its memory-efficient loading via memory mapping, fast serialization and deserialization speeds, built-in type safety and validation, and elimination of security vulnerabilities from arbitrary code execution. This makes it an ideal format for storing and distributing large language models where safe and efficient handling of massive weight files is crucial.&lt;/p&gt;
&lt;p&gt;There are three common file formats used for storing model weights:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;.pth&lt;/code&gt; - PyTorch&#39;s native format that uses Python&#39;s pickle serialization. While widely used, it has security concerns since pickled files can execute arbitrary code. It&#39;s mainly used for PyTorch models and research projects.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;.safetensors&lt;/code&gt; - A modern format designed specifically for ML models that provides better security and performance than .pth files. It&#39;s memory-efficient, fast to load, and prevents code execution vulnerabilities. This is becoming the standard format for Hugging Face models.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;.gguf&lt;/code&gt; - The &amp;quot;GPT-Generated Unified Format&amp;quot; used by llama.cpp and similar projects. It&#39;s optimized for inference on consumer hardware, supporting various quantization methods. The format is compact and designed for efficient loading of quantized models.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The models are typically stored in the HuggingFace cache on your local machine. Which is usually located at &lt;code&gt;~/.cache/huggingface/hub&lt;/code&gt;. If you download many models this folder can get quite large. So it&#39;s a good idea to clean it up periodically.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Loading the Tokenizer&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The tokenizer is used to convert text into tokens, which are the basic units of data processed by the model. The tokenizer also handles special tokens and provides various configuration options for text processing.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-3B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The primary configuration options for the tokenizer are:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Values&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;padding_side&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Determines which side padding tokens are added&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;quot;right&amp;quot;&lt;/code&gt; (default) or &lt;code&gt;&amp;quot;left&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;truncation_side&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Determines which side text is truncated when exceeding max length&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;quot;right&amp;quot;&lt;/code&gt; (default) or &lt;code&gt;&amp;quot;left&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;chat_template&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Jinja template for formatting chat messages&lt;/td&gt;
&lt;td&gt;String template following &lt;a href=&quot;https://huggingface.co/docs/transformers/chat_templating&quot;&gt;chat templating spec&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;model_input_names&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List of inputs accepted by model&#39;s forward pass&lt;/td&gt;
&lt;td&gt;e.g., &lt;code&gt;[&amp;quot;input_ids&amp;quot;, &amp;quot;attention_mask&amp;quot;]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In addition to the above, the tokenizer also handles special tokens which are given special names and specific to the model architecture. For example:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Token&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;bos_token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Beginning of sequence token&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;eos_token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;End of sequence token&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unk_token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Token for unknown words&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sep_token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Separator token between sentences&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pad_token&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Padding token for batch processing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;When encoding text, the tokenizer can return tensors in different formats:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Return Type&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;TensorFlow tensors&lt;/td&gt;
&lt;td&gt;&lt;code&gt;return_tensors=&amp;quot;tf&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;PyTorch tensors&lt;/td&gt;
&lt;td&gt;&lt;code&gt;return_tensors=&amp;quot;pt&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;np&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;NumPy arrays&lt;/td&gt;
&lt;td&gt;&lt;code&gt;return_tensors=&amp;quot;np&amp;quot;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# PyTorch tensors&lt;/span&gt;
tokens_pt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# NumPy arrays&lt;/span&gt;
tokens_np &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;np&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By default a tokenizer will return a list of integers representing the tokens. Using the Llama 3.2 tokenizer as an example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;The brown bear is a species of bear that is native to North America.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Will ouptut:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;791&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14198&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11984&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;374&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;264&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9606&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;315&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11984&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;430&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;374&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10068&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;311&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4892&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5270&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this stream:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;128000 is the &lt;code&gt;bos_token&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;13 is the &lt;code&gt;eos_token&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The numbers in between represent the actual tokens of the text&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Padding&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Padding is an important concept in training language models that allows us to handle sequences of different lengths in batches. While LLMs are typically pre-trained without padding, it becomes necessary during fine-tuning when working with custom datasets.&lt;/p&gt;
&lt;p&gt;Padding involves extending shorter sequences to match the length of the longest sequence in a batch by adding special &amp;quot;pad&amp;quot; tokens. This ensures all sequences in a batch have uniform length for efficient processing. When training in batches, all sequences must have the same length to create proper tensors for GPU processing. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Original sequences of different lengths&lt;/span&gt;
sequence1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;263&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;13563&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Length 8&lt;/span&gt;
sequence2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;                    &lt;span class=&quot;token comment&quot;&gt;# Length 5&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# After padding (using 32000 as pad token)&lt;/span&gt;
sequence1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;263&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;13563&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Unchanged&lt;/span&gt;
sequence2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Padded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Along with padding, we need attention masks to tell the model which tokens are real and which are padding:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Attention masks (1 for real tokens, 0 for padding)&lt;/span&gt;
mask1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# All real tokens&lt;/span&gt;
mask2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Last 3 are padding&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are two ways to add padding tokens:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Right Padding&lt;/strong&gt; (most common):&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;sequence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAD&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAD&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAD&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left Padding&lt;/strong&gt;:&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;sequence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;PAD&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAD&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PAD&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;887&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;526&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;451&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29889&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can either use an existing special token (like EOS since it signals &amp;quot;no more content&amp;quot;) or create a new dedicated pad token.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Configure tokenizer for padding&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;padding_side &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;right&quot;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# or &quot;left&quot;&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token  &lt;span class=&quot;token comment&quot;&gt;# Using EOS as pad token&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Or create a new pad token (if your model doesn&#39;t already have one)&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_special_tokens&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;pad_token&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;[PAD]&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Apply padding when tokenizing&lt;/span&gt;
inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    sequences&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;max_length&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you do add a new pad token, remember to resize model embeddings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;resize_token_embeddings&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The choice of padding strategy can affect model performance. Right padding is better for autoregressive models as it maintains natural sequence order, while left padding can be more efficient for bidirectional models. The max length should be set based on your dataset&#39;s length distribution, which you can measure by finding the maximum length of the sequences in your dataset.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Attention Mask&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The attention mask is a binary tensor that indicates which tokens the model should pay attention to (1) and which it should ignore (0) during processing. It&#39;s particularly important when working with batched sequences of different lengths that require padding.&lt;/p&gt;
&lt;p&gt;For example, if we have two sequences of different lengths:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;sequences &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;The cat sat on the mat.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Set the pad token to the eos token&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token

&lt;span class=&quot;token comment&quot;&gt;# Tokenize with padding&lt;/span&gt;
encoded &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    sequences&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would output something like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;input_ids&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;791&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;8415&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;7731&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;389&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;279&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;5634&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;9906&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;1917&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128009&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128009&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128009&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128009&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128009&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;attention_mask&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The 1s indicate real tokens that should be attended to, while 0s indicate padding tokens that should be ignored. This prevents the model from trying to find meaningful patterns in the padding tokens.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decoding&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To convert tokens back into text, use the tokenizer&#39;s decode method:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Decode a single sequence&lt;/span&gt;
tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;791&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14198&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11984&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;374&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;264&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9606&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;315&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11984&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &amp;lt;|begin_of_text|&gt;The brown bear is a species of bear.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Decode batched sequences&lt;/span&gt;
batch_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; encoded&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input_ids&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
texts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;batch_decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;batch_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;texts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# [&#39;&amp;lt;|begin_of_text|&gt;The cat sat on the mat.&#39;, &#39;&amp;lt;|begin_of_text|&gt;Hello world&amp;lt;|eot_id|&gt;&amp;lt;|eot_id|&gt;&amp;lt;|eot_id|&gt;&amp;lt;|eot_id|&gt;&amp;lt;|eot_id|&gt;&#39;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The decode method handles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Removing special tokens (like BOS/EOS) by default&lt;/li&gt;
&lt;li&gt;Converting token IDs back to subwords&lt;/li&gt;
&lt;li&gt;Joining subwords into complete words&lt;/li&gt;
&lt;li&gt;Handling whitespace and punctuation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can control special token handling with the &lt;code&gt;skip_special_tokens&lt;/code&gt; parameter:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Keep special tokens&lt;/span&gt;
text_with_special &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# &amp;lt;|begin_of_text|&gt;The brown bear is a species of bear.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Remove special tokens (default)&lt;/span&gt;
text_without_special &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# The brown bear is a species of bear.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Moving Data to GPU&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When training or using language models, moving data to the GPU is a critical step for achieving good performance. Modern language models are computationally intensive, and GPUs are specifically designed to handle the parallel matrix operations that form the core of model inference and training. Moving a model to the GPU involves transferring both the model weights and the input tensors from CPU memory to GPU memory, where computations can be performed much more efficiently.&lt;/p&gt;
&lt;p&gt;The process of moving data to the GPU is handled through PyTorch&#39;s device management system. For models, this typically involves calling the &lt;code&gt;.to()&lt;/code&gt; method with &lt;code&gt;cuda&lt;/code&gt; as the target device, which transfers all model parameters to GPU memory. Similarly, input tensors like token IDs and attention masks need to be moved to the same device as the model. This ensures that all computations can be performed on the GPU without unnecessary transfers back to CPU memory, which would create performance bottlenecks. It&#39;s important to note that while the model and input tensors must be on the GPU, the tokenizer itself remains on the CPU since it performs text processing operations that don&#39;t benefit from GPU acceleration.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
tokens_pt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokens_pt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The device map in PyTorch allows you to specify how model layers should be distributed across available devices (GPUs and CPU). This is particularly useful when working with large language models that don&#39;t fit on a single GPU. The device map is a dictionary that maps model components to specific devices.&lt;/p&gt;
&lt;p&gt;When loading a model, you can set &lt;code&gt;device_map=&amp;quot;auto&amp;quot;&lt;/code&gt; to automatically distribute the model layers across available GPUs and CPU. This will fill all available space on the GPU(s) first, then the CPU, and finally, the hard drive if there is still not enough memory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-3B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;auto&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The PyTorch memory manager will automatically manage the GPU memory but this memory is tied to the Python garbage collector so sometimes you will need to manually free up memory by deleting heap references to the model and manually calling the garbage collector to flush the memory. This can happen sometiems in exception handling logic which fails to clean up after itself, or when using IPython notebooks which can sometimes leave objects in memory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; gc
&lt;span class=&quot;token keyword&quot;&gt;del&lt;/span&gt; model
&lt;span class=&quot;token keyword&quot;&gt;del&lt;/span&gt; trainer

gc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;collect&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty_cache&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Chat Formats&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Chat models expect conversations to be formatted in specific ways. Using the wrong format can significantly degrade model performance, so it&#39;s important to match the format the model was trained with.&lt;/p&gt;
&lt;p&gt;The most common format is the Llama style, which wraps messages in specific control tokens:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;conversation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;You are a helpful assistant&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What type of bear is the best bear?&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The formatted conversation will look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;begin_of_text&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;start_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;system&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;end_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
You are a helpful assistant&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;eot_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;start_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;user&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;end_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
What &lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; of bear &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; the best bear?&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;eot_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;start_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;assistant&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;end_header_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
The brown bear &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; the best bear&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;eot_id&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The easiest way to handle chat formatting to a string is to use the model&#39;s built-in chat template through the tokenizer:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;formatted_chat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    conversation&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    add_generation_prompt&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;add_generation_prompt&lt;/code&gt; parameter, when set to &lt;code&gt;True&lt;/code&gt;, adds the assistant role token at the end of the formatted chat. This is useful when preparing the conversation for text generation, as it signals to the model that it should generate the assistant&#39;s response next. When set to &lt;code&gt;False&lt;/code&gt;, it will only format the existing conversation without adding the generation prompt.&lt;/p&gt;
&lt;p&gt;In addition when using the &lt;code&gt;trl&lt;/code&gt; library we don&#39;t need to format the chat ourselves we can simply pass the conversation to the trainer in one of &lt;a href=&quot;https://huggingface.co/docs/trl/en/dataset_formats&quot;&gt;several formats&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Conversational format&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;messages&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;You are helpful bear assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What type of bear is the best bear?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The brown bear is the best bear.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Instruction format&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Where do the polar bears live?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;completion&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The polar bear lives in the Arctic.&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Text format&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The American black bear is indigenous to North America.&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the chat conversation the roles are:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;system&lt;/td&gt;
&lt;td&gt;Sets the context in which to interact with the model. It typically includes rules, guidelines, or necessary information that help the model respond effectively.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;user&lt;/td&gt;
&lt;td&gt;Represents the human interacting with the model. It includes the inputs, commands, and questions to the model.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;assistant&lt;/td&gt;
&lt;td&gt;Represents the response generated by the AI model based on the context provided in the system, and user prompts.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;When fine-tuning a model, it&#39;s critical to use the same chat format it was originally trained with. Using a different format can significantly degrade performance or cause the model to behave unpredictably.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Training Dataset&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The training data for fine-tuning should be carefully curated to match your target use case. The dataset typically consists of input-output pairs that demonstrate the desired behavior. For instruction tuning, this means pairs of instructions/questions and high-quality responses.&lt;/p&gt;
&lt;p&gt;Once you have determined that fine-tuning is the right solution for your use case, you&#39;ll need to create a dataset to train your model. This dataset should contain diverse examples that demonstrate the specific task or behavior you want the model to learn. There are several established approaches for creating instruction datasets.&lt;/p&gt;
&lt;p&gt;One common approach is to leverage existing open-source datasets like Spider (&lt;a href=&quot;https://huggingface.co/datasets/spider&quot;&gt;https://huggingface.co/datasets/spider&lt;/a&gt;) for SQL tasks, SQuAD (&lt;a href=&quot;https://huggingface.co/datasets/squad&quot;&gt;https://huggingface.co/datasets/squad&lt;/a&gt;) for question answering, or other task-specific collections. While this is the quickest and most cost-effective method, these datasets may not perfectly align with your specific requirements. There are many of these datasets available on Hugging Face now so it&#39;s worth searching for one that matches your use case.&lt;/p&gt;
&lt;p&gt;Another approach is to use large language models to generate synthetic training data, similar to how the Alpaca dataset (&lt;a href=&quot;https://huggingface.co/datasets/tatsu-lab/alpaca&quot;&gt;https://huggingface.co/datasets/tatsu-lab/alpaca&lt;/a&gt;) was created. This involves using a more capable model like GPT-4 to generate instruction-response pairs. While this can produce large amounts of data quickly, the quality may vary and inherit any biases or limitations of the generating model. When using a langauge model there is also no guarantee that the generated data will correct, which is a very important caveat! This is also technically against the terms of some commercial LLM providers, but in practice they don&#39;t seem to be enforcing it so do with that what you will, I&#39;m a software engineer not a lawyer.&lt;/p&gt;
&lt;p&gt;You can also create datasets using human annotators, as was done with the Dolly dataset (&lt;a href=&quot;https://huggingface.co/datasets/databricks/databricks-dolly-15k&quot;&gt;https://huggingface.co/datasets/databricks/databricks-dolly-15k&lt;/a&gt;). This typically produces the highest quality data since humans can provide nuanced, contextually-appropriate responses. However, this method is usually the most time-consuming and expensive.&lt;/p&gt;
&lt;p&gt;Many successful projects combine multiple approaches. For example, the Orca dataset (&lt;a href=&quot;https://huggingface.co/datasets/Open-Orca/OpenOrca&quot;&gt;https://huggingface.co/datasets/Open-Orca/OpenOrca&lt;/a&gt;) used a combination of synthetic data generation and human curation and filtering. This hybrid approach can help balance quality, quantity, and cost while mitigating the drawbacks of any single method.&lt;/p&gt;
&lt;p&gt;The choice between these methods ultimately depends on your specific requirements. There are many guides on the web that cover the topic of synthetic data generation in more detail so I&#39;ll defer to them for more information. Data curation is the absolute most important part of fine-tuning, so take your time and be very careful. The general data curation guidelines are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Quality is more important than quantity - a small set of high-quality examples is better than a large noisy dataset&lt;/li&gt;
&lt;li&gt;The data should be representative of how you want the model to behave in production&lt;/li&gt;
&lt;li&gt;Include diverse examples that cover different aspects of the desired capabilities&lt;/li&gt;
&lt;li&gt;Ensure consistent formatting and style across examples&lt;/li&gt;
&lt;li&gt;Remove any problematic or low-quality samples that could degrade model behavior&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&#39;s an example of how to structure training data in a CSV file that has input and ouputs which are&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-csv&quot;&gt;input,output
&amp;quot;What is the capital of France?&amp;quot;,&amp;quot;Paris&amp;quot;
&amp;quot;What is the capital of Germany?&amp;quot;,&amp;quot;Berlin&amp;quot;
&amp;quot;What is the capital of the United States?&amp;quot;,&amp;quot;Washington, D.C.&amp;quot;
&amp;quot;What is the capital of Japan?&amp;quot;,&amp;quot;Tokyo&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; DatasetDict
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; pd

&lt;span class=&quot;token comment&quot;&gt;# Read CSV file into Dataset&lt;/span&gt;
df &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read_csv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;csv_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pandas&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;df&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# 90% train, 10% test + validation&lt;/span&gt;
train_testvalid &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train_test_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;test&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Split the 10% test + valid in half test, half valid&lt;/span&gt;
test_valid &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; train_testvalid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;test&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train_test_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;test&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

raw_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DatasetDict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;train&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; train_testvalid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;train&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;validation&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; test_valid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;test&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;test&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; test_valid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;train&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Save to disk (as Parquet)&lt;/span&gt;
raw_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_to_disk&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;raw_dataset&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Save to JSONL file&lt;/span&gt;
raw_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to_json&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;raw_dataset.jsonl&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lines&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to format the data for the model, we need only map over the dataset and apply the chat formatting.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Load from disk&lt;/span&gt;
raw_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;raw_dataset&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Shuffle&lt;/span&gt;
pre_formatted_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; raw_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shuffle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

SYSTEM_PROMPT &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;You are a helpful assistant&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;format_chat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;messages&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; SYSTEM_PROMPT&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Apply formatting&lt;/span&gt;
formatted_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pre_formatted_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;format_chat&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Split into train/eval/test&lt;/span&gt;
train_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; formatted_dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;train&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
eval_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; formatted_dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;validation&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
test_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; formatted_dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;test&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the output we have three datasets:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;train&lt;/code&gt; - The main dataset used to train the model and update its parameters during training&lt;/li&gt;
&lt;li&gt;&lt;code&gt;validation&lt;/code&gt; - Used during training to evaluate model performance on unseen data and prevent overfitting&lt;/li&gt;
&lt;li&gt;&lt;code&gt;test&lt;/code&gt; - A completely held-out dataset used only for final model evaluation after training is complete to assess generalization&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;strong&gt;train dataset&lt;/strong&gt; is the examples our model will learn from during fine-tuning.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;validation dataset&lt;/strong&gt; is used during training to tune hyperparameters and monitor for overfitting. By evaluating the model&#39;s performance on validation data after each training epoch or at regular intervals, we can make adjustments to learning rate, batch size, and other parameters to optimize training. The validation set helps inform training decisions.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;test dataset&lt;/strong&gt; is strictly held out until the very end of model development. It provides a final, unbiased evaluation of model performance on completely unseen data. The test set should only be used once after all training and hyperparameter tuning is complete.&lt;/p&gt;
&lt;p&gt;While they serve distinct roles, validation and test sets can sometimes be combined into a single validation/test set in scenarios with limited data. This is not ideal but may be necessary when the dataset is very small and splitting into three sets would leave too little data for each, when the use case only requires monitoring training progress without a final holdout evaluation, or when the model will undergo continuous training/updating rather than having a fixed release. However, whenever possible, maintaining separate validation and test sets provides the most rigorous evaluation of model generalization.&lt;/p&gt;
&lt;p&gt;Always ensure that the training and validation datasets are separate with no overlap. Overlapping data can corrupt the evaluation results by inflating performance metrics, as the model may have already seen the validation examples during training, leading to misleadingly high accuracy and other performance measures. This is known as &lt;strong&gt;data leakage&lt;/strong&gt; and should be avoided at all costs as it will result in a model that appears to perform better than it actually does but really has just memorized the validation set. We want to measure on data that is out of sample and not used to train the model.&lt;/p&gt;
&lt;p&gt;There are also two common operations provided by the datasets library that are useful when working with them. These are &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;shuffle&lt;/code&gt; which slice and randomly shuffle the dataset respectively.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Select 1000 samples&lt;/span&gt;
train_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; train_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;select&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Shuffle using a seed for reproducibility&lt;/span&gt;
train_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; train_dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shuffle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quantization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Quantization is a model compression technique that converts the weights and activations within an LLM from a high-precision data representation to a lower-precision format. For example, converting from 32-bit floating-point numbers (FP32) to 8-bit or 4-bit integers (INT4 or INT8). This process significantly reduces model size while maintaining acceptable performance levels. The main quantization approaches are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Post-Training Quantization (PTQ)&lt;/strong&gt;: Post-Training Quantization is applied after model training is complete. It&#39;s a simpler approach that&#39;s faster to implement compared to QAT, though it may result in some accuracy loss. One advantage is that it requires less training data to execute effectively.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Quantization-Aware Training (QAT)&lt;/strong&gt;: Quantization-Aware Training integrates quantization directly into the training process itself. This approach generally achieves better performance compared to Post-Training Quantization, though it comes at the cost of being more computationally intensive. QAT also requires access to the full training dataset to be effective.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The most common quantization techniques include:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;QLoRA&lt;/strong&gt;: Combines 4-bit quantization with Low-Rank Adaptation. It uses the NF4 (4-bit NormalFloat) data type for storing weights while performing computations in BFloat16 during training. Additionally, it employs double quantization to reduce the memory footprint of scaling factors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GPTQ&lt;/strong&gt;: Layer-wise quantization by processing weights in batches of 128 columns. It utilizes a mixed INT4/FP16 precision approach and minimizes output error through a lazy batch updating mechanism.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AWQ&lt;/strong&gt;: Another layer-wise quantization technique that processes weights in batches of 128 columns. It uses a combination of 4-bit and 8-bit quantization and employs double quantization to reduce the memory footprint of scaling factors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GGML/GGUF&lt;/strong&gt;: CPU-focused quantization framework that divides weights into blocks of 32. It provides multiple quantization methods including q2_k, q4_0, and q8_0, making it possible to run models on CPU-only hardware without requiring a GPU.&lt;/p&gt;
&lt;p&gt;Surprisingly, quantization often results in minimal performance degradation, even with significant compression. Research has shown that 4-bit quantization methods like QLoRA and AWQ can maintain 99%+ of the original model&#39;s performance while reducing size by 8x. This remarkable effectiveness challenges the intuitive expectation that such aggressive compression would significantly impact model quality. Even 3-bit quantization can maintain strong performance in many cases, making quantization one of the most successful compression techniques.&lt;/p&gt;
&lt;p&gt;The GGUF quant formats are the most common and are supported by most inference engines. Here&#39;s a comprehensive table of quantization methods, organized by recommendation status and showing memory usage.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Q4_K_M&lt;/td&gt;
&lt;td&gt;Medium, balanced quality&lt;/td&gt;
&lt;td&gt;✅ Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q5_K_S&lt;/td&gt;
&lt;td&gt;Large, low quality loss&lt;/td&gt;
&lt;td&gt;✅ Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q5_K_M&lt;/td&gt;
&lt;td&gt;Large, very low quality loss&lt;/td&gt;
&lt;td&gt;✅ Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q3_K_S&lt;/td&gt;
&lt;td&gt;Very small, very high quality loss&lt;/td&gt;
&lt;td&gt;Standard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q3_K_M&lt;/td&gt;
&lt;td&gt;Very small, very high quality loss&lt;/td&gt;
&lt;td&gt;Standard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q3_K_L&lt;/td&gt;
&lt;td&gt;Small, substantial quality loss&lt;/td&gt;
&lt;td&gt;Standard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q4_K_S&lt;/td&gt;
&lt;td&gt;Small, significant quality loss&lt;/td&gt;
&lt;td&gt;Standard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q6_K&lt;/td&gt;
&lt;td&gt;Very large, extremely low quality loss&lt;/td&gt;
&lt;td&gt;Standard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q2_K&lt;/td&gt;
&lt;td&gt;Smallest, extreme quality loss&lt;/td&gt;
&lt;td&gt;Not Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Q8_0&lt;/td&gt;
&lt;td&gt;Very large, extremely low quality loss&lt;/td&gt;
&lt;td&gt;Not Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F16&lt;/td&gt;
&lt;td&gt;Extremely large, virtually no quality loss&lt;/td&gt;
&lt;td&gt;Not Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F32&lt;/td&gt;
&lt;td&gt;Absolutely huge, lossless&lt;/td&gt;
&lt;td&gt;Not Recommended&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In short if you want to use quantization, the recommended methods are &lt;strong&gt;Q4_K_M&lt;/strong&gt; and &lt;strong&gt;Q5_K_M&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Q4_K_M&lt;/strong&gt; offers the best balance of size and quality&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Q5_K_M&lt;/strong&gt; provides higher quality at larger sizes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you want to have a loss-less model quant use &lt;strong&gt;F16&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;There are aggregator accounts on Hugging Face that provide quantized models. For example, &lt;a href=&quot;https://huggingface.co/bartowski&quot;&gt;bartowski&lt;/a&gt; provides quantized models for most of Meta&#39;s models. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf&quot;&gt;Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/bartowski/Meta-Llama-3.1-70B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-70B-Instruct-Q5_K_M.gguf&quot;&gt;Meta-Llama-3.1-70B-Instruct-Q5_K_M.gguf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Batch Size and Training Epochs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Understanding &lt;strong&gt;batch size&lt;/strong&gt; and &lt;strong&gt;epochs&lt;/strong&gt; is essential to configuring your trainer. A sample represents a single input-output pair in your training dataset, such as an instruction and its corresponding response. These samples are grouped into batches for more efficient processing during training. The batch size determines how many samples the model processes simultaneously before updating its weights, typically ranging from 1 to 512 depending on available GPU memory and training requirements.&lt;/p&gt;
&lt;p&gt;A training epoch represents one complete pass through your entire training dataset. For example, if you have 10,000 training samples and a batch size of 32, one epoch would consist of approximately 313 batch updates (10,000 ÷ 32, rounded up). The number of epochs determines how many times the model will see each training sample during the entire training process. For fine-tuning tasks, 1-3 epochs is often sufficient as the model already has strong foundational knowledge from pre-training.&lt;/p&gt;
&lt;p&gt;The relationship between batch size and epochs significantly impacts training dynamics. Larger batch sizes enable more efficient parallel processing but may require adjusting other hyperparameters like learning rate to maintain training stability. They also provide more stable gradient estimates but might require more epochs to achieve the same level of model performance. Conversely, smaller batch sizes introduce more noise into the training process, which can sometimes help the model generalize better, but they require more update steps to complete an epoch.&lt;/p&gt;
&lt;p&gt;When fine-tuning language models, a common starting point is to use a batch size of 8 or 16 with gradient accumulation to simulate larger batches, combined with 1-3 training epochs. This configuration often provides a good balance between training stability, memory efficiency, and final model performance. The exact values should be adjusted based on your specific hardware constraints and training objectives.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Training Duration&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Training duration refers to the total time taken to complete the training process for a model. This duration is influenced by several factors, including the size of the dataset, the complexity of the model, the batch size, the number of epochs, and the computational resources available (such as the type and number of GPUs used).&lt;/p&gt;
&lt;p&gt;For instance, training a large language model on a substantial dataset with a high number of epochs and a large batch size will generally take longer compared to training a smaller model on a smaller dataset with fewer epochs and a smaller batch size. Additionally, the efficiency of the training process can be affected by the use of techniques like gradient accumulation, which can help manage memory usage but may also impact the overall training time.&lt;/p&gt;
&lt;p&gt;Monitoring and optimizing training duration is crucial for efficient model development, especially when working with limited computational resources or when aiming to iterate quickly on model improvements.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# -1 means full epochs&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The formula to calculate the number of batches per epoch is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;num_batches = num_samples / batch_size
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When working with a small dataset, such as ~1000 samples, using a batch size of 32 is generally a safe and standard choice. This batch size is unlikely to significantly impact your results unless you are dealing with much larger datasets, like hundreds of thousands or millions of observations.&lt;/p&gt;
&lt;p&gt;Regarding batch size and epochs, here are some best practices:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Batch Size&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Larger batch sizes can lead to faster training progress but may not always result in faster convergence.&lt;/li&gt;
&lt;li&gt;Smaller batch sizes tend to train more slowly but can converge more quickly.&lt;/li&gt;
&lt;li&gt;The optimal batch size is often problem-dependent, so experimentation may be necessary.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Epochs&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Models typically improve with more epochs of training, up to a certain point.&lt;/li&gt;
&lt;li&gt;Accuracy tends to plateau as the model converges.&lt;/li&gt;
&lt;li&gt;It&#39;s advisable to experiment with a higher number of epochs, such as 50, and plot the number of epochs against accuracy to identify where the performance levels out.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Training Duration&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Train for at least 3 epochs to ensure convergence.&lt;/li&gt;
&lt;li&gt;Experiment with longer training durations as they result in significantly better models.&lt;/li&gt;
&lt;li&gt;Use early stopping but monitor your evals closely to ensure you&#39;re not overfitting.&lt;/li&gt;
&lt;li&gt;Do a full checkpoint on each epoch, which you can select if later ones underperform.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Early Stopping&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Early stopping is a regularization technique designed to prevent overfitting during model training by monitoring the model&#39;s performance on a validation set. The training process is halted when the model&#39;s performance ceases to improve, ensuring that the model generalizes well to new, unseen data. This technique involves setting a patience parameter, which determines how many epochs to wait for an improvement in validation performance before stopping the training. If the model&#39;s performance does not improve for the specified number of epochs, the training is terminated to prevent overfitting.&lt;/p&gt;
&lt;p&gt;For instance, with a patience of 3 epochs, if the validation loss does not decrease for 3 consecutive epochs, the training will stop. This approach prevents the model from continuing to train on the training data unnecessarily. Early stopping can be implemented in most deep learning frameworks, such as TensorFlow and PyTorch, using built-in callbacks or custom logic within the training loop.&lt;/p&gt;
&lt;p&gt;When implementing early stopping, it is important to choose an appropriate validation metric, such as validation loss or accuracy, to monitor. The patience value should be set to balance the training time and the risk of overfitting. Additionally, it is advisable to save the model&#39;s weights at the epoch with the best validation performance, ensuring that the best version of the model can be restored. By employing early stopping, you can achieve a balance between model performance and training efficiency, leading to better generalization on new data.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; EarlyStoppingCallback

early_stopping_callback &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EarlyStoppingCallback&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    early_stopping_patience&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    early_stopping_threshold&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    callbacks&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;early_stopping_callback&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two parameters to early stopping are.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;early_stopping_patience&lt;/code&gt;: This is how many times we&#39;ll check the metric before deciding to stop if things aren&#39;t getting better. It&#39;s like giving the model a few chances to improve.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;early_stopping_threshold&lt;/code&gt;: This is the tiny amount the metric needs to get better by to keep going. If it doesn&#39;t improve by this much, we might call it a day.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Gradient Accumulation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Gradient accumulation is a technique that helps overcome GPU memory limitations when training large models. Rather than updating model parameters after each batch, gradient accumulation allows us to process multiple smaller batches sequentially while accumulating their gradients, effectively simulating a larger batch size.&lt;/p&gt;
&lt;p&gt;When using gradient accumulation, the training process is modified to accumulate gradients over multiple forward and backward passes before applying a single weight update. For example, if you want an effective batch size of 32 but can only fit 8 samples in memory, you would set the gradient accumulation steps to 4. This means the model will:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Process a mini-batch of 8 samples&lt;/li&gt;
&lt;li&gt;Calculate and store the gradients without updating weights&lt;/li&gt;
&lt;li&gt;Repeat this process 4 times&lt;/li&gt;
&lt;li&gt;Finally update the model weights using the accumulated gradients&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The key insight is that this produces mathematically equivalent results to training with the larger batch size of 32, while requiring only enough memory to process 8 samples at a time.&lt;/p&gt;
&lt;p&gt;To implement gradient accumulation, the training loop needs to be modified to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Physical batch size per GPU&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Number of forward passes before update&lt;/span&gt;
    gradient_checkpointing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Additional memory optimization&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The effective batch size can be calculated as:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;effective_batch_size = per_device_batch_size * gradient_accumulation_steps * num_gpus
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gradient accumulation provides significant memory savings by requiring forward pass memory only for smaller physical batch sizes, maintaining constant gradient storage, and updating optimizer states less frequently. This technique makes it possible to train models that would otherwise be too large for available GPU memory. For example, fine-tuning a 7B parameter model might require 32GB of GPU memory with standard training, but could work on a 16GB GPU using gradient accumulation.&lt;/p&gt;
&lt;p&gt;When selecting the number of gradient accumulation steps, you&#39;ll need to balance your target effective batch size, available GPU memory constraints, training speed requirements (since more steps means slower training), and model convergence characteristics. A good starting point is to choose accumulation steps that result in an effective batch size between 32 and 512, while staying within memory limits. For example, with a physical batch size of 4, you might use 8-32 accumulation steps depending on your specific needs.&lt;/p&gt;
&lt;p&gt;While gradient accumulation helps overcome memory constraints, it affects training dynamics by decreasing training speed linearly with accumulation steps, potentially requiring learning rate adjustments for larger effective batch sizes, and calculating batch normalization statistics (if used) on smaller physical batches. To maintain training stability, consider adjusting the learning rate using the square root scaling rule:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;adjusted_lr = base_lr * sqrt(effective_batch_size / base_batch_size)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Learning Rate&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The learning rate is perhaps the most critical hyperparameter in training language models, acting as a scaling factor that determines how much the model weights should be adjusted in response to the calculated gradients during backpropagation. When using an optimizer like AdamW (see below), the learning rate directly influences how large of a step the optimizer takes in the direction that reduces the loss function.&lt;/p&gt;
&lt;p&gt;A learning rate that&#39;s too high can cause the training process to overshoot optimal weight values, leading to unstable training or convergence to suboptimal solutions. This manifests as erratic loss curves or even numerical instability in extreme cases. Conversely, a learning rate that&#39;s too low results in very slow training progress, where the model takes tiny steps towards better solutions and might get stuck in local minima or fail to converge within the allocated training time.&lt;/p&gt;
&lt;p&gt;For language model fine-tuning, learning rates typically fall between 1e-5 and 5e-4, with 2e-4 being a common starting point when using LoRA. The optimal learning rate often depends on several factors:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The size of the model - larger models generally benefit from smaller learning rates to maintain stability&lt;/li&gt;
&lt;li&gt;The choice of optimizer - AdamW typically works well with lower learning rates compared to basic SGD&lt;/li&gt;
&lt;li&gt;The batch size - larger batch sizes often allow for slightly higher learning rates&lt;/li&gt;
&lt;li&gt;The specific fine-tuning technique being used - LoRA can often use higher learning rates than full fine-tuning&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When using LoRA, you can often use higher learning rates than with full fine-tuning because you&#39;re only updating a small subset of parameters. A typical configuration might look like:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# Higher than full fine-tuning&lt;/span&gt;
    weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# L2 regularization&lt;/span&gt;
    max_grad_norm&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Gradient clipping threshold&lt;/span&gt;
    optim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;paged_adamw_32bit&quot;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Memory-efficient optimizer&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The relationship between learning rate and batch size follows what&#39;s known as the &amp;quot;linear scaling rule&amp;quot;: when you increase the batch size by a factor of k, you should generally increase the learning rate by the same factor to maintain similar training dynamics. However, this rule begins to break down at very large batch sizes, where the square root scaling rule often works better:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;base_lr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;
base_batch_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;
new_batch_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Square root scaling&lt;/span&gt;
new_lr &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; base_lr &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_batch_size &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; base_batch_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A learning rate of 1e-4 has become the standard when fine-tuning LLMs with LoRA. Going with this initially is a good starting point unless you have empirical evidence to suggest otherwise.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning Rate Schedules&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The learning rate schedule plays a crucial role in model training, determining how the learning rate changes throughout the training process. Different schedules offer various trade-offs between training stability, convergence speed, and final model performance. There are several different learning rate schedules available in the &lt;code&gt;transformers&lt;/code&gt; library:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;linear&lt;/strong&gt;: This schedule decreases the learning rate linearly from an initial value to a final value in a straight line.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;cosine&lt;/strong&gt;: This schedule follows a cosine curve, starting from the initial learning rate and smoothly decreasing to near zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;cosine_with_restarts&lt;/strong&gt;: This schedule adds periodic &amp;quot;jumps&amp;quot; to the cosine schedule, temporarily increasing the learning rate before allowing it to decay again.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;polynomial&lt;/strong&gt;: This schedule decreases the learning rate following a polynomial decay function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;constant&lt;/strong&gt;: This schedule keeps the learning rate constant throughout the training process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;constant_with_warmup&lt;/strong&gt;: This schedule keeps the learning rate constant after a warmup period where the learning rate increases linearly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;inverse_sqrt&lt;/strong&gt;: This schedule decreases the learning rate following an inverse square root decay function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;reduce_lr_on_plateau&lt;/strong&gt;: This schedule reduces the learning rate when a metric has stopped improving.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;cosine_with_min_lr&lt;/strong&gt;: This schedule follows a cosine curve with a minimum learning rate, starting from the initial learning rate and smoothly decreasing to the minimum value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;warmup_stable_decay&lt;/strong&gt;: This schedule combines warmup with a stable decay mechanism.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most common ones to use are linear, cosine and cosine with restarts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Linear Schedule&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The linear learning rate schedule gradually decreases the learning rate from an initial value to a final value in a straight line. This simple approach works well for many fine-tuning tasks, especially when the number of training steps is relatively small. The linear schedule provides a good balance between early exploration with higher learning rates and final convergence with lower rates.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;linear&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.03&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The warmup_ratio parameter determines what fraction of the training steps will use a gradually increasing learning rate before the linear decay begins. This helps prevent unstable updates early in training when gradients might be large or noisy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cosine Schedule&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The cosine learning rate schedule follows a cosine curve, starting from the initial learning rate and smoothly decreasing to near zero. This schedule provides a more gradual reduction in learning rate compared to linear decay, which can help models converge to better solutions. The cosine schedule is particularly effective for longer training runs where you want to explore the loss landscape thoroughly before settling into a minimum.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cosine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.03&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The smooth nature of the cosine schedule means that the model spends more time at moderate learning rates compared to linear decay, which can improve generalization performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cosine with Restarts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Cosine with restarts (also known as warm restarts) adds periodic &amp;quot;jumps&amp;quot; to the cosine schedule, temporarily increasing the learning rate before allowing it to decay again. This approach can help the model escape poor local minima and explore different regions of the loss landscape. Each restart provides an opportunity for the model to discover better solutions while maintaining the benefits of the cosine schedule.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cosine_with_restarts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.03&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_cycles&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Number of restart cycles&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The num_cycles parameter controls how many restarts occur during training. Each cycle completes a full cosine decay before resetting the learning rate. This schedule is particularly useful for complex tasks where the loss landscape might have many local minima.&lt;/p&gt;
&lt;p&gt;When selecting a learning rate schedule, use the following heuristics:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Training duration&lt;/strong&gt;: Shorter fine-tuning runs (1-2 epochs) often work well with linear decay, while longer runs benefit from cosine schedules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Task complexity&lt;/strong&gt;: More complex tasks might benefit from cosine with restarts to escape local minima.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Computational budget&lt;/strong&gt;: Simpler schedules like linear decay require less tuning and are more predictable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model size&lt;/strong&gt;: Larger models often benefit from more sophisticated schedules like cosine with restarts.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A good starting point for most fine-tuning tasks is:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cosine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.03&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# -1 means use num_train_epochs instead&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Warmup Steps&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The warmup steps parameter determines how many steps the learning rate will increase linearly before the schedule begins to decay. This helps prevent unstable updates early in training when gradients might be large or noisy. There are two common heuristics for setting this parameter, &lt;code&gt;warmup_ratio&lt;/code&gt; and &lt;code&gt;warmup_steps&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;warmup_ratio&lt;/code&gt; parameter determines what fraction of the training steps will use a gradually increasing learning rate before the linear decay begins. This helps prevent unstable updates early in training when gradients might be large or noisy.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;warmup_steps&lt;/code&gt; parameter determines the exact number of steps that will use a gradually increasing learning rate before the linear decay begins.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Optimizer&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The optimizer is the core algorithm that updates the model weights during training. Popular optimizers like AdamW are all variants of SGD. L2 regularization, often used with these optimizers, is like adding a small penalty for having large weights, encouraging the model to keep things simple and avoid overfitting.&lt;/p&gt;
&lt;p&gt;The AdamW optimizer has become the de facto standard for training and fine-tuning large language models. AdamW extends the traditional Adam optimizer by decoupling weight decay from gradient updates, which leads to better generalization performance, especially in large neural networks.&lt;/p&gt;
&lt;p&gt;At its core, AdamW maintains two moments (moving averages) for each parameter: the first moment represents the mean of past gradients, while the second moment tracks the uncentered variance of past gradients. These moments help adapt the learning rate for each parameter individually, making the optimizer particularly effective for training deep neural networks with parameters that require different scales of updates.&lt;/p&gt;
&lt;p&gt;The key innovation of AdamW over standard Adam is its handling of weight decay. In traditional Adam, weight decay is implemented as part of the gradient update, which can lead to suboptimal regularization. AdamW applies weight decay directly to the weights before the gradient update, ensuring proper L2 regularization. This seemingly small change has significant implications for model performance, particularly in large language models where proper regularization is crucial for preventing overfitting.&lt;/p&gt;
&lt;p&gt;When configuring AdamW for language model fine-tuning, typical hyperparameters include a weight decay value between 0.01 and 0.1, and beta values of 0.9 and 0.999 for the first and second moments respectively. The optimizer is commonly implemented with the following configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    optim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;paged_adamw_32bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Memory-efficient AdamW implementation&lt;/span&gt;
    weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# L2 regularization factor&lt;/span&gt;
    adam_beta1&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;               &lt;span class=&quot;token comment&quot;&gt;# Exponential decay rate for first moment&lt;/span&gt;
    adam_beta2&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.999&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# Exponential decay rate for second moment&lt;/span&gt;
    adam_epsilon&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1e-8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# Small constant for numerical stability&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &amp;quot;paged&amp;quot; variant of AdamW (paged_adamw_32bit) is particularly useful for fine-tuning large language models as it implements memory-efficient state management, reducing the GPU memory required for optimizer states while maintaining the benefits of AdamW&#39;s adaptive learning rates and proper weight decay.&lt;/p&gt;
&lt;p&gt;In addition to the optimizer, the &lt;code&gt;max_grad_norm&lt;/code&gt; parameter determines the maximum value for the gradient norm during backpropagation. This is used to prevent the exploding gradient problem, where the gradients grow too large and cause numerical instability. The default of 0.3 is a good starting point, but you can experiment with different values to see what works best for your specific fine-tuning task.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Weight Decay&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Weight decay is a regularization technique that helps prevent overfitting by discouraging large weights in the model. It does this by adding a penalty to the loss function that is proportional to the sum of the squared weights, effectively encouraging the model to maintain smaller weights. This can lead to better generalization on unseen data.&lt;/p&gt;
&lt;p&gt;A sensible default for weight decay in many large language model fine-tuning tasks is 0.01. This value provides a balance between regularization and model flexibility, helping to prevent overfitting without overly constraining the model&#39;s capacity to learn from the data.&lt;/p&gt;
&lt;p&gt;In some cases, you might consider setting the weight decay to 0, particularly if the model is underfitting or if the dataset is very small and regularization is not as critical. Conversely, a higher weight decay value, such as 0.1, might be appropriate if the model is overfitting significantly, as it would impose a stronger penalty on large weights, encouraging even smaller weight values and potentially improving generalization.&lt;/p&gt;
&lt;p&gt;When configuring the AdamW optimizer, the weight decay parameter is specified as part of the training arguments, ensuring that it is consistently applied throughout the training process.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# L2 regularization factor&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;SFT Trainer&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Supervised Fine-tuning Trainer (SFT Trainer) is a specialized class provided by the TRL (Transformer Reinforcement Learning) library that simplifies the process of fine-tuning language models. It builds upon the standard Hugging Face Trainer class while adding specific functionality for supervised fine-tuning tasks, making it particularly well-suited for instruction tuning and chat model development.&lt;/p&gt;
&lt;p&gt;The SFT Trainer handles many of the complexities involved in fine-tuning, including proper initialization of PEFT models, dataset formatting, and training optimizations. When provided with a PEFT configuration, it automatically sets up parameter-efficient fine-tuning, allowing for memory-efficient training of large language models. It can work with both standard datasets and iterable datasets, making it flexible for different data loading approaches.&lt;/p&gt;
&lt;p&gt;A basic implementation of the SFT Trainer might look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SFTTrainer

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                    &lt;span class=&quot;token comment&quot;&gt;# Your model&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Training dataset&lt;/span&gt;
    peft_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lora_config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# LoRA configuration&lt;/span&gt;
    max_seq_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# Maximum sequence length&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# Tokenizer&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# Training arguments&lt;/span&gt;
    packing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;                    &lt;span class=&quot;token comment&quot;&gt;# Enable sequence packing&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trainer supports several performance optimizations that can significantly improve training efficiency. It&#39;s compatible with Flash Attention 2 for faster attention computation.&lt;/p&gt;
&lt;p&gt;One of the key advantages of the SFT Trainer is its handling of dataset formatting. It automatically handles the conversion of your training data into the appropriate format for the model, including proper tokenization and sequence packing when enabled. This removes much of the boilerplate code typically needed for preparing training data, allowing you to focus on the actual fine-tuning process.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Packing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Packing is an optimization technique used by the SFT Trainer to maximize GPU memory utilization during training. When enabled via the &lt;code&gt;packing=True&lt;/code&gt; parameter, the trainer will pack multiple sequences into a single training example up to the specified &lt;code&gt;max_seq_length&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Without packing, each training example is padded to the maximum sequence length, which wastes computational resources on padding tokens. For example, if you have sequences of lengths 128, 256, and 384 tokens, and &lt;code&gt;max_seq_length=512&lt;/code&gt;, traditional batching would pad each sequence to 512 tokens, wasting significant memory on padding.&lt;/p&gt;
&lt;p&gt;With packing enabled, the trainer will intelligently combine multiple sequences into a single training example. Using the previous example, it could pack the 128 and 384 token sequences together into a single 512-token example, and pack the 256-token sequence with another short sequence. This results in more efficient GPU memory usage, faster training since fewer padding tokens are processed, and better utilization of the maximum sequence length.&lt;/p&gt;
&lt;p&gt;The trainer handles all the complexity of packing, including proper attention masking between packed sequences to prevent cross-contamination. This makes it a valuable optimization, especially when training on datasets with varying sequence lengths.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Flash Attention 2&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Flash Attention 2 is a highly optimized attention mechanism that significantly reduces memory usage and speeds up the attention computation during training. To enable Flash Attention 2 in your training setup, you&#39;ll need to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Meta-Llama-3-8B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    quantization_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bnb_config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;device_map&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    attn_implementation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;flash_attention_2&quot;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Enable Flash Attention 2&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If your hardware doesn&#39;t support Flash Attention 2, the model will automatically fall back to standard attention implementation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;NEFTune&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;NEFTune is a technique that can improve performance by adding noise to the embedding vectors during training. To enable it set the &lt;code&gt;neftune_noise_alpha&lt;/code&gt; parameter in &lt;code&gt;TrainingArguments&lt;/code&gt;. NEFTune is disabled after training to restore the original embedding layer to avoid any unexpected behavior.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    neftune_noise_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A value between 1 and 10 are commonly used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Loss Function&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;loss function&lt;/strong&gt; is a mathematical measure of how far the model&#39;s predictions deviate from the ground truth during training. For language models, this typically means measuring the cross-entropy loss between the predicted probability distribution over the vocabulary and the actual next token that appears in the training data. A lower loss indicates better performance, as it means the model is assigning higher probabilities to the correct tokens. The loss is calculated and averaged over batches of training examples, providing a single number that represents the model&#39;s current performance.&lt;/p&gt;
&lt;p&gt;In simple terms, cross-entropy loss is like measuring how surprised the model is by the correct answer. If the model is very confident about predicting the wrong word, it gets a big penalty (high loss). If it&#39;s confident about the right word, it gets a small penalty (low loss). It&#39;s similar to a game where you have to guess the next word in a sentence - the more uncertain or wrong you are, the more points you lose. The goal is to minimize these &amp;quot;surprise points&amp;quot; by getting better at predicting what comes next.&lt;/p&gt;
&lt;p&gt;During training, we track two types of loss: training loss and evaluation loss (&lt;code&gt;eval_loss&lt;/code&gt;). Training loss is calculated on the data the model is actively learning from, while eval loss is calculated on a held-out validation set that the model never sees during training. These two metrics together provide the key insights into the learning process. If both losses are decreasing, the model is learning effectively. If training loss decreases but eval loss increases or plateaus, this indicates overfitting - the model is memorizing the training data rather than learning generalizable patterns.&lt;/p&gt;
&lt;p&gt;In general you can think of loss as a score that measures how many mistakes the model makes. Like a teacher grading papers, the loss function gives the model a low score (high loss) when it makes lots of mistakes and a high score (low loss) when it makes few mistakes. Training loss is like practice test scores - it shows how well the model does on material it&#39;s studying. Eval loss is like the score on the final exam - it shows how well the model can apply what it learned to new questions it hasn&#39;t seen before. Just as a student who memorizes the practice tests without understanding the material will do poorly on the final exam, a model that overfits will show good training loss but poor eval loss.&lt;/p&gt;
&lt;p&gt;If a loss is precisely 0, it means the model has memorized the training data. If the loss is high, it means the model is making lots of mistakes. The loss should decrease over time as the model learns if it goes the other way then something is wrong.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning Curves&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Understanding how to interpret learning curves is crucial for successful model fine-tuning. The learning curve shows how the model&#39;s loss changes over time during training, typically plotting both training and validation loss against the number of training steps or epochs. These curves provide valuable insights into how well the model is learning and whether it&#39;s experiencing common training issues.&lt;/p&gt;
&lt;p&gt;A well-behaved learning curve typically shows both training and validation loss decreasing smoothly over time, eventually plateauing at similar values. The initial steep decline represents the model quickly learning the most obvious patterns in the data. As training progresses, the improvements become more gradual as the model fine-tunes its understanding of more subtle patterns. A small gap between training and validation loss suggests the model is generalizing well to unseen data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Underfitting&lt;/strong&gt; occurs when the model fails to learn the underlying patterns in the training data effectively. This manifests in learning curves as persistently high loss values for both training and validation sets, with minimal improvement over time. The curves might appear flat or show very slow improvement, indicating the model lacks the capacity to capture the complexity of the task, or the learning rate might be too low for effective training. In such cases, consider increasing the model&#39;s capacity, adjusting the learning rate, or training for more epochs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Overfitting&lt;/strong&gt;, conversely, occurs when the model memorizes the training data rather than learning generalizable patterns. The learning curves reveal this through a characteristic divergence: while the training loss continues to decrease, the validation loss begins to increase or plateau at a higher value. This growing gap between training and validation performance indicates the model is becoming too specialized to the training data at the expense of generalization. Common remedies include introducing regularization, reducing model capacity, or implementing early stopping when the validation loss begins to increase.&lt;/p&gt;
&lt;p&gt;The rate of convergence in the learning curves can also provide insights into the appropriateness of your learning rate. If the loss decreases too slowly, your learning rate might be too low, resulting in inefficient training. Conversely, if the loss shows high volatility or sudden spikes, the learning rate might be too high, causing unstable training. The ideal learning curve shows steady, consistent improvement without excessive fluctuations.&lt;/p&gt;
&lt;div style=&quot;display: flex; flex-direction: column; align-items: center; padding: 5px;&quot;&gt;
&lt;img src=&quot;https://www.stephendiehl.com/images/loss_curve.png&quot; alt=&quot;Loss Curve&quot; style=&quot;width: 50%;&quot; /&gt;
&lt;figcaption style=&quot;text-align: center; margin-top: 5px;&quot;&gt;
A loss curve showing proper convergence.
&lt;/figcaption&gt;
&lt;/div&gt;
&lt;p&gt;In simple terms, underfitting is when your model is &amp;quot;too dumb&amp;quot; - it hasn&#39;t learned enough from the training data to make good predictions. It&#39;s like a student who barely studied for an exam and can&#39;t answer even basic questions. Overfitting is when your model is &amp;quot;too smart for its own good&amp;quot; - it has memorized the training data so well that it can&#39;t generalize to new examples. This is like a student who memorized all the practice problems but can&#39;t solve questions that look slightly different on the actual exam. Ideally we want a model that is just smart enough to learn the underlying patterns in the training data without memorizing it, because this state is something that approximates &amp;quot;understanding&amp;quot; or &amp;quot;generalization&amp;quot;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Catastrophic Forgetting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Catastrophic forgetting is a significant challenge in fine-tuning large language models. When a model is fine-tuned for a specific task or domain, there&#39;s a risk that it will lose or &amp;quot;forget&amp;quot; the broad knowledge and capabilities it acquired during pre-training. This happens because the model&#39;s parameters are updated to optimize performance on the new task, potentially overwriting or disrupting the learned representations that enabled its general language understanding abilities.&lt;/p&gt;
&lt;p&gt;The impact of catastrophic forgetting can be particularly severe when fine-tuning aggressively or with high learning rates. A model that originally could handle a wide range of tasks - from question answering to summarization to code generation - might become hyper-specialized for its fine-tuning objective while losing its broader capabilities. This is why techniques like LoRA and careful learning rate selection are crucial - they help preserve the model&#39;s general knowledge while allowing targeted adaptation for specific use cases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Logging&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The most common logging tool is Weights &amp;amp; Biases. It&#39;s a free service that allows you to track your training metrics and visualize them in a dashboard. This can be very useful to look at the loss and eval curves visually to see their time-dependent behavior. In addition to logging you can also use Weights &amp;amp; Biases to compare the performance of different models and training runs known as &lt;em&gt;parameter sweeps&lt;/em&gt; which can track experiments that alter the paramters like learning rate, weight decay, model size, etc and automatically log the results.&lt;/p&gt;
&lt;p&gt;Turning on logging is as simple as adding the &lt;code&gt;report_to=&amp;quot;wandb&amp;quot;&lt;/code&gt; argument to your &lt;code&gt;TrainingArguments&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; wandb &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; init

init&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;project&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;my-project&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    report_to&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;wandb&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To do a parameter sweep you can use the &lt;code&gt;wandb.sweep&lt;/code&gt; function. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; wandb

&lt;span class=&quot;token comment&quot;&gt;# 1. Define the sweep configuration&lt;/span&gt;
sweep_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;method&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;random&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Random search strategy&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Metric to optimize&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;metric&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&#39;name&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;eval_loss&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&#39;goal&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;minimize&#39;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Parameters to sweep over&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;parameters&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&#39;learning_rate&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&#39;distribution&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;uniform&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&#39;min&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&#39;max&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&#39;batch_size&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&#39;values&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# 2. Define the training function&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Initialize a new wandb run&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; wandb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;init&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Get the configuration for this run&lt;/span&gt;
        config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; wandb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;config
        &lt;span class=&quot;token comment&quot;&gt;# ... your training logic here&lt;/span&gt;


&lt;span class=&quot;token comment&quot;&gt;# 3. Initialize the sweep&lt;/span&gt;
sweep_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; wandb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sweep&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    sweep_config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    project&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;my-sweep-project&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# 4. Run the sweep&lt;/span&gt;
wandb&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;agent&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sweep_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; function&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; count&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Run 5 experiments&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Evaluation Metrics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Understanding and interpreting evaluation metrics is essential for assessing model performance during and after fine-tuning. The most fundamental metric is the loss function, which measures how far the model&#39;s predictions deviate from the ground truth. In language models, this is typically the cross-entropy loss between the predicted token probabilities and the actual next tokens. The training loss represents this measurement on the training data, while the evaluation loss (eval_loss) measures the same on a held-out validation set. A decreasing trend in both metrics indicates the model is learning, while a divergence between them may signal overfitting.&lt;/p&gt;
&lt;p&gt;For instruction-following and chat models, ROUGE (Recall-Oriented Understudy for Gisting Evaluation) scores help evaluate the similarity between generated responses and reference answers. ROUGE-1 and ROUGE-2 measure overlap in unigrams and bigrams respectively, while ROUGE-L considers the longest common subsequence. These metrics are particularly useful for assessing how well the model captures key information and maintains coherent phrasing, though they shouldn&#39;t be relied upon exclusively as they don&#39;t always correlate with human judgments of quality.&lt;/p&gt;
&lt;p&gt;BLEU (Bilingual Evaluation Understudy) scores, while originally designed for machine translation, can provide additional insight into the precision of generated text. BLEU scores range from 0 to 1, measuring n-gram overlap between generated and reference texts with a penalty for length mismatches. However, BLEU scores should be interpreted cautiously for instruction-following tasks, as multiple valid responses might use different but equally appropriate phrasing.&lt;/p&gt;
&lt;p&gt;The Hugging Face Trainer automatically logs these metrics during training, making them accessible through the training_logs directory or via integration with tracking platforms like Weights &amp;amp; Biases. When analyzing these metrics, it&#39;s important to consider their trends rather than absolute values, as the appropriate ranges can vary significantly depending on the task, dataset, and model architecture. A sudden spike in eval_loss or perplexity often indicates a training issue that needs attention, while gradual improvement across all metrics suggests healthy learning progression.&lt;/p&gt;
&lt;p&gt;To load metrics we use the &lt;code&gt;evaluate&lt;/code&gt; library.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Get all metrics&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; evaluate &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load

accuracy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;accuracy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
rouge &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;rouge&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bleu &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;bleu&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to compute a metric we need to pass in the predictions and labels.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;run_metrics&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eval_preds&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eval_preds
    &lt;span class=&quot;token comment&quot;&gt;# Decode predictions and labels back to text&lt;/span&gt;
    predictions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;batch_decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;batch_decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;accuracy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; accuracy&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; references&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;rouge&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; rouge&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; references&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;bleu&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; bleu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compute&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; references&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;eval_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    compute_metrics&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;run_metrics
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Perplexity&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Perplexity is a one of the most core metrics for evaluating language models that measures how well a model predicts a sample of text. Mathematically, perplexity is defined as the exponential of the average negative log-likelihood of a sequence. In simpler terms, it indicates how &amp;quot;confused&amp;quot; or &amp;quot;perplexed&amp;quot; the model is when predicting the next token in a sequence.&lt;/p&gt;
&lt;p&gt;Perplexity, derived directly from the cross-entropy loss through an exponential transformation, provides a more interpretable metric for language modeling tasks. It can be understood as how &amp;quot;confused&amp;quot; the model is when predicting the next token - lower values indicate better performance. For example, a perplexity of 10 means the model is as uncertain as if it were choosing between 10 equally likely options at each step. While base models might start with perplexities in the 15-30 range, well-fine-tuned models often achieve perplexities below 10 on their target domain. The formula for perplexity is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
P = e^{-&#92;frac{1}{N} &#92;sum_{i=1}^{N} &#92;log P(x_i | x_{&amp;lt;i})}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(N&#92;) is the number of tokens in the sequence&lt;/li&gt;
&lt;li&gt;&#92;(P(x&lt;em&gt;i | x&lt;/em&gt;{&amp;lt;i})&#92;) is the probability the model assigns to token &#92;(x_i&#92;) given the previous tokens&lt;/li&gt;
&lt;li&gt;The sum is taken over all tokens in the sequence&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lower perplexity scores indicate better performance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A perplexity of 1 would mean the model perfectly predicts each token&lt;/li&gt;
&lt;li&gt;A perplexity of 2 means the model is as uncertain as choosing between two equally likely options&lt;/li&gt;
&lt;li&gt;A perplexity of 10 means the model is as uncertain as choosing between ten equally likely options&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For context, here are typical perplexity ranges for different model scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Base models on general text: 15-30&lt;/li&gt;
&lt;li&gt;Fine-tuned models on domain-specific text: 5-15&lt;/li&gt;
&lt;li&gt;Human-level performance: 3-7&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To calculate perplexity during evaluation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compute_perplexity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eval_preds&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eval_preds
    &lt;span class=&quot;token comment&quot;&gt;# Get log probabilities from model outputs&lt;/span&gt;
    log_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;functional&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log_softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Get probability of correct token&lt;/span&gt;
    target_log_probs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; log_probs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;gather&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;squeeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate perplexity&lt;/span&gt;
    perplexity &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;target_log_probs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;perplexity&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; perplexity&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;eval_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    compute_metrics&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;compute_perplexity
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s what to look out for when keeping an eye on perplexity during training:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If it&#39;s steadily going down, that&#39;s great! Your model is learning what it should&lt;/li&gt;
&lt;li&gt;If it flatlines and stops improving, your model has probably hit a wall in what it can learn&lt;/li&gt;
&lt;li&gt;Watch out if validation perplexity starts climbing while training perplexity keeps dropping - this means your model is starting to memorize the training data instead of actually learning&lt;/li&gt;
&lt;li&gt;Random big jumps up and down usually mean something&#39;s off with your training setup, like your learning rate being wonky&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Perplexity is handy to track, but don&#39;t rely on it alone. Just because your model is good at predicting the next word doesn&#39;t necessarily mean it&#39;s writing high-quality, sensible text. You&#39;ll want to look at other metrics too to get the full picture.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Custom Evaluation Metrics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Custom evaluation metrics allow you to track specific aspects of model performance during training that go beyond the standard loss and perplexity metrics. These metrics can be particularly valuable for assessing how well your model performs on task-specific objectives or specialized criteria.&lt;/p&gt;
&lt;p&gt;To implement a custom metric, you&#39;ll need to create a function that takes the model&#39;s predictions and the ground truth labels as input, and returns a dictionary containing your computed metrics. This function should be compatible with the Hugging Face Evaluate library&#39;s format. Here&#39;s an example of a custom metric that tracks the model&#39;s ability to generate responses within a specific length range:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compute_length_metrics&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eval_preds&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eval_preds
    &lt;span class=&quot;token comment&quot;&gt;# Decode predictions and labels back to text&lt;/span&gt;
    predictions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;batch_decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;predictions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    labels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;batch_decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;labels&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate length-based metrics&lt;/span&gt;
    pred_lengths &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; pred &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; predictions&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    label_lengths &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;label&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; label &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; labels&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate average length ratio&lt;/span&gt;
    length_ratios &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;l &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; l &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; l &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pred_lengths&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label_lengths&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    avg_length_ratio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length_ratios&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length_ratios&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate percentage within desired length range (e.g., 0.8-1.2 times original)&lt;/span&gt;
    within_range &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.8&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.2&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; r &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; length_ratios&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    pct_within_range &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; within_range &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;length_ratios&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;length_ratio&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; avg_length_ratio&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;pct_within_range&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pct_within_range
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To integrate this custom metric with the SFT Trainer, you&#39;ll need to modify your training setup to include the evaluation function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... existing arguments ...&lt;/span&gt;
    evaluation_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Evaluate at regular intervals&lt;/span&gt;
    eval_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                 &lt;span class=&quot;token comment&quot;&gt;# Evaluate every 100 steps&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;eval_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;               &lt;span class=&quot;token comment&quot;&gt;# Add evaluation dataset&lt;/span&gt;
    compute_metrics&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;compute_length_metrics&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Add custom metrics&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... rest of your trainer arguments ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trainer will now compute and log your custom metrics during evaluation phases. These metrics will appear in your training logs and can be visualized using Weights &amp;amp; Biases. You can access the metrics programmatically through the trainer&#39;s state:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# After training&lt;/span&gt;
results &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;evaluate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Final length ratio: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;results&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;eval_length_ratio&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Percentage within range: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;results&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;eval_pct_within_range&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When designing custom metrics, it&#39;s important to consider computational efficiency since these metrics will be calculated repeatedly during training. Heavy computations in your evaluation function can significantly slow down the training process. Additionally, ensure your metric function handles edge cases gracefully, such as empty sequences or unexpected inputs, to prevent training interruptions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Model Selection&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When training a model, it&#39;s important to save checkpoints periodically and select the best performing model based on evaluation metrics. The Trainer automatically saves checkpoints during training, and we can configure how to select the best model using the &lt;code&gt;TrainingArguments&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    save_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# Save checkpoints every n steps&lt;/span&gt;
    save_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                    &lt;span class=&quot;token comment&quot;&gt;# Save every 100 steps&lt;/span&gt;
    save_total_limit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                &lt;span class=&quot;token comment&quot;&gt;# Keep only the 3 best checkpoints&lt;/span&gt;
    metric_for_best_model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;eval_loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Metric to compare checkpoints&lt;/span&gt;
    load_best_model_at_end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Load best model when training ends&lt;/span&gt;
    evaluation_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# When to run evaluation&lt;/span&gt;
    eval_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;                     &lt;span class=&quot;token comment&quot;&gt;# Run evaluation every 100 steps&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key parameters for model selection are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;save_strategy&lt;/strong&gt;: Determines when to save checkpoints&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;steps&amp;quot;&lt;/code&gt;: Save every n training steps&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;epoch&amp;quot;&lt;/code&gt;: Save at the end of each epoch&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;no&amp;quot;&lt;/code&gt;: Don&#39;t save checkpoints&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;save_steps&lt;/strong&gt;: How often to save when using &lt;code&gt;save_strategy=&amp;quot;steps&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lower values provide more checkpoints but use more disk space&lt;/li&gt;
&lt;li&gt;Higher values save space but might miss good model states&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;save_total_limit&lt;/strong&gt;: Maximum number of checkpoints to keep&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Older checkpoints are deleted when limit is reached&lt;/li&gt;
&lt;li&gt;Set to &lt;code&gt;None&lt;/code&gt; to keep all checkpoints (not recommended)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;metric_for_best_model&lt;/strong&gt;: Which metric to use for comparing checkpoints&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;eval_loss&amp;quot;&lt;/code&gt;: Use validation loss (default)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;eval_accuracy&amp;quot;&lt;/code&gt;: Use validation accuracy&lt;/li&gt;
&lt;li&gt;Any custom metric returned by &lt;code&gt;compute_metrics&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;greater_is_better&lt;/strong&gt;: Whether higher metric values are better&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;True&lt;/code&gt; for metrics like accuracy where higher is better&lt;/li&gt;
&lt;li&gt;&lt;code&gt;False&lt;/code&gt; for metrics like loss where lower is better&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;load_best_model_at_end&lt;/strong&gt;: Whether to restore best checkpoint&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;True&lt;/code&gt;: Load best checkpoint after training&lt;/li&gt;
&lt;li&gt;&lt;code&gt;False&lt;/code&gt;: Keep final model state&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    save_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    save_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    save_total_limit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    metric_for_best_model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;eval_loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    greater_is_better&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Lower eval_loss is better&lt;/span&gt;
    load_best_model_at_end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    evaluation_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# ... other arguments ...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After training, you can access the best model&#39;s metrics:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Get best model&#39;s metrics&lt;/span&gt;
best_metric &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;best_metric
best_checkpoint &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;best_model_checkpoint

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Best &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;metric_for_best_model&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;best_metric&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Best checkpoint: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;best_checkpoint&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The checkpoints are saved in your output directory with names like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./results/checkpoint-100/
./results/checkpoint-200/
./results/checkpoint-300/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each checkpoint directory contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pytorch_model.bin&lt;/code&gt;: Model weights&lt;/li&gt;
&lt;li&gt;&lt;code&gt;training_args.bin&lt;/code&gt;: Training configuration&lt;/li&gt;
&lt;li&gt;&lt;code&gt;trainer_state.json&lt;/code&gt;: Training state&lt;/li&gt;
&lt;li&gt;&lt;code&gt;config.json&lt;/code&gt;: Model configuration&lt;/li&gt;
&lt;li&gt;&lt;code&gt;adapter_config.json&lt;/code&gt;: LoRA adapter configuration (if using PEFT)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To load a specific checkpoint:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM

&lt;span class=&quot;token comment&quot;&gt;# Load specific checkpoint&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results/checkpoint-100&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Or load best checkpoint&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;best_model_checkpoint&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When using PEFT/LoRA, make sure to load the adapter weights as well:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; peft &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PeftModel

&lt;span class=&quot;token comment&quot;&gt;# Load base model&lt;/span&gt;
base_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Meta-Llama-3-8B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;auto&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Load adapter weights from checkpoint&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PeftModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    base_model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;./results/checkpoint-100&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results/merged-model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; safe_serialization&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition if you want to shard the model into multiple files to save disk space, you can use the &lt;code&gt;max_shard_size&lt;/code&gt; parameter to chunk the model into smaller files, in this case &lt;code&gt;2GB&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Save model with safe serialization&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results/merged-model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; safe_serialization&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_shard_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;2GB&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Direct Preference Optimization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Direct Preference Optimization (DPO) is a powerful technique for aligning language models with human preferences after initial SFT (Supervised Fine-Tuning). Unlike more complex RLHF approaches, DPO achieves similar results with a simpler and more stable training process.&lt;/p&gt;
&lt;p&gt;DPO requires a preference dataset containing prompts paired with chosen (preferred) and rejected responses. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the meaning of life?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;chosen&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The meaning of life is to grow, learn, and find purpose through our experiences and connections with others.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;rejected&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;42 lol&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s a basic implementation of DPO training that uses the &lt;a href=&quot;https://huggingface.co/datasets/unalignment/toxic-dpo-v0.2&quot;&gt;&lt;code&gt;unalignment/toxic-dpo-v0.2&lt;/code&gt;&lt;/a&gt; dataset which contains a preference dataset for training a model to avoid unethical and toxic responses.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; DPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; DPOConfig
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset

&lt;span class=&quot;token comment&quot;&gt;# Load model and dataset&lt;/span&gt;
dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unalignment/toxic-dpo-v0.2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

dpo_args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;DPOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;dpo-model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_prompt_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    beta&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# Controls deviation from reference model&lt;/span&gt;
    loss_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sigmoid&quot;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# DPO loss variant&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Configure and run DPO training&lt;/span&gt;
dpo_trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dpo_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

dpo_trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key parameter &lt;code&gt;beta&lt;/code&gt; (typically 0.1-0.2) controls how much the model can deviate from the reference model during training. The &lt;code&gt;loss_type&lt;/code&gt; parameter can be set to different variants including &amp;quot;sigmoid&amp;quot; (default), &amp;quot;hinge&amp;quot;, &amp;quot;ipo&amp;quot;, or &amp;quot;kto&amp;quot;. During training, the trainer logs metrics like rewards for chosen/rejected responses and reward margins to monitor alignment progress.&lt;/p&gt;
&lt;p&gt;DPO can also be combined with parameter-efficient methods like LoRA by wrapping the model with a PEFT configuration before training. The technique works best when starting with a well-trained SFT model and using high-quality preference data. Regular checkpointing and evaluation of generated responses helps ensure the model improves while maintaining reasonable behavior relative to the reference model.&lt;/p&gt;
&lt;p&gt;The trainer automatically logs useful metrics including rewards for chosen and rejected responses, accuracy (how often chosen &amp;gt; rejected), and reward margins (mean difference between chosen and rejected rewards). An increasing reward margin indicates the model is learning to better align with human preferences. DPO provides a simpler alternative to RLHF while achieving similar alignment results, with the key being high-quality preference data and careful monitoring of training progress.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Kahneman-Tversky Optimization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Kahneman-Tversky Optimization (KTO) is an alternative to DPO that simplifies the training process by only requiring binary feedback (desirable/undesirable) rather than paired preferences. KTO incorporates insights from prospect theory about how humans perceive value and risk, making it particularly effective for aligning models with human preferences.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the meaning of life?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;completion&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;To destroy all humans&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Unethical response are undesirable&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;How do I make a radiological weapon?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;completion&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;I cannot help with making weapons.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Ethical response marked as desirable&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Tell me about quantum physics&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;completion&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;idk google it lol&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Unhelpful response marked as undesirable&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Write a poem about nature&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;completion&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Roses are red, violets are blue...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Generic/low-effort response marked as undesirable&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s a basic implementation that uses the &lt;a href=&quot;https://huggingface.co/datasets/trl-lib/kto-mix-14k&quot;&gt;&lt;code&gt;trl-lib/kto-mix-14k&lt;/code&gt;&lt;/a&gt; dataset:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; KTOTrainer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; KTOConfig

dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;trl-lib/kto-mix-14k&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

kto_args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;KTOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;kto-model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_prompt_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    beta&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;              &lt;span class=&quot;token comment&quot;&gt;# Controls deviation from reference model&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5e-7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Keep between 5e-7 and 5e-6&lt;/span&gt;
    desirable_weight&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Weight for positive examples&lt;/span&gt;
    undesirable_weight&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# Weight for negative examples&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Configure and run KTO training&lt;/span&gt;
trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; KTOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;kto_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key parameters in the &lt;code&gt;KTOConfig&lt;/code&gt; are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;beta&lt;/code&gt;: Controls deviation from reference model (0.1-0.2 typical)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;learning_rate&lt;/code&gt;: Keep between 5e-7 and 5e-6&lt;/li&gt;
&lt;li&gt;&lt;code&gt;desirable_weight&lt;/code&gt;/&lt;code&gt;undesirable_weight&lt;/code&gt;: Adjust if dataset is imbalanced&lt;/li&gt;
&lt;li&gt;Per-step batch size should be at least 4&lt;/li&gt;
&lt;li&gt;Effective batch size between 16-128&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The trainer logs metrics including rewards for desirable/undesirable responses and KL divergence from the reference model. An increasing reward margin indicates successful alignment. KTO works best with high-quality binary feedback data and careful monitoring of training progress. The method is particularly valuable when preference data is scarce or expensive to collect, as it only requires binary labels rather than paired preferences.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proximal Policy Optimization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, Proximal Policy Optimization (PPO) is a reinforcement learning algorithm for fine-tuning language models using reward signals. Unlike DPO and KTO which work with static preference data, PPO enables online learning where the model actively generates responses and receives feedback through a reward model.&lt;/p&gt;
&lt;p&gt;KL-divergence (Kullback-Leibler divergence) is a measure of the difference between two probability distributions P and Q. Mathematically, it is defined as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
KL(P||Q) = &#92;sum_{x} P(x) &#92;cdot &#92;log&#92;left(&#92;frac{P(x)}{Q(x)}&#92;right)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In the context of language model training, KL-divergence measures how much the model&#39;s output distribution diverges from a reference distribution (like the original pre-trained model). A higher KL-divergence indicates the fine-tuned model is producing significantly different probability distributions compared to the reference model.&lt;/p&gt;
&lt;p&gt;In simpler terms, KL-divergence helps us make sure the fine-tuned model doesn&#39;t &amp;quot;forget&amp;quot; what it learned during pre-training. In rough terms, it acts somewhat like a leash - allowing the model to adapt to new tasks while preventing it from straying too far from its original behavior. This is key because we want the model to learn new things without completely changing its fundamental understanding of language.&lt;/p&gt;
&lt;p&gt;The dataset should contain prompts and their corresponding responses. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Write a story about a brave brown bear&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;response&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;In a kingdom far away, a valiant brown bear named Bruno...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;reward&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.8&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Reward score from reward model (typically 0-1)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s a basic implementation that uses another smaller language model &lt;a href=&quot;https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment&quot;&gt;&lt;code&gt;nlptown/bert-base-multilingual-uncased-sentiment&lt;/code&gt;&lt;/a&gt; as a reward model which rewards responses based on their positive sentiment. It assigns to each row in the training dataset a reward between 0 and 1 based on the sentiment of the response.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoModelForSequenceClassification
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PPOConfig&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; PPOTrainer
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset

SENTIMENT_MODEL &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;nlptown/bert-base-multilingual-uncased-sentiment&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SentimentRewardModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; model_name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;SENTIMENT_MODEL&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_available&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cpu&quot;&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForSequenceClassification&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compute_reward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; texts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Get sentiment scores (1-5) and normalize to [0,1]&lt;/span&gt;
        inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;texts&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; padding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; truncation&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;no_grad&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;softmax&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outputs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logits&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            rewards &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;scores &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tensor&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            rewards &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rewards &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; rewards&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cpu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize reward model&lt;/span&gt;
reward_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SentimentRewardModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Configure PPO training&lt;/span&gt;
config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PPOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.41e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    mini_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    optimize_cuda_cache&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    early_stopping&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_kl&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    ppo_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    seed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize trainer&lt;/span&gt;
ppo_trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Generation settings&lt;/span&gt;
generation_kwargs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;min_length&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;top_k&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;top_p&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;do_sample&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;pad_token_id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;max_new_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Training loop&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; epoch &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; batch &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Get prompts from dataset&lt;/span&gt;
        query_tensors &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ppo_trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prepare_samples&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Generate responses&lt;/span&gt;
        response_tensors &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; query &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; query_tensors&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ppo_trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                query&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsqueeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;generation_kwargs
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            response_tensors&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;squeeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Decode responses&lt;/span&gt;
        texts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;squeeze&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; r &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; response_tensors
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Calculate rewards&lt;/span&gt;
        rewards &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; reward_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compute_reward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;texts&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Run PPO step&lt;/span&gt;
        stats &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ppo_trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;step&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query_tensors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; response_tensors&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewards&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Log stats&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Epoch &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;epoch&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, Batch &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;batch&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Mean reward: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rewards&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Policy loss: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;stats&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;policy/loss&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Value loss: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;stats&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;value/loss&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trainer logs several important metrics during training:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;objective/kl&lt;/code&gt;: KL divergence between current and reference policies&lt;/li&gt;
&lt;li&gt;&lt;code&gt;objective/entropy&lt;/code&gt;: Policy entropy indicating action randomness&lt;/li&gt;
&lt;li&gt;&lt;code&gt;objective/rlhf_reward&lt;/code&gt;: The main RLHF reward signal&lt;/li&gt;
&lt;li&gt;&lt;code&gt;policy/approxkl_avg&lt;/code&gt;: Average KL between consecutive policies&lt;/li&gt;
&lt;li&gt;&lt;code&gt;policy/clipfrac_avg&lt;/code&gt;: Fraction of clipped policy updates&lt;/li&gt;
&lt;li&gt;&lt;code&gt;loss/policy_avg&lt;/code&gt;: Average policy loss&lt;/li&gt;
&lt;li&gt;&lt;code&gt;loss/value_avg&lt;/code&gt;: Average value function loss&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The key hyperparameters are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;learning_rate&lt;/code&gt;: Keep between 1e-6 and 5e-6&lt;/li&gt;
&lt;li&gt;&lt;code&gt;kl_coef&lt;/code&gt;: Controls deviation from reference model (0.05-0.2)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cliprange&lt;/code&gt;: PPO clipping parameter (0.1-0.3)&lt;/li&gt;
&lt;li&gt;Per-device batch size should be at least 4&lt;/li&gt;
&lt;li&gt;Effective batch size between 16-128&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;PPO works best with a well-trained reward model and careful tuning of the KL penalty to prevent the policy from deviating too far from the reference behavior. The trainer automatically handles the PPO optimization, including advantage estimation, policy updates, and value function training. Regular monitoring of the KL divergence and reward signals helps ensure stable training progress.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Merging LoRA Weights&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After fine-tuning a model with LoRA, you&#39;ll often want to merge the LoRA weights back into the base model to create a single, unified model that can be used for inference without the LoRA adapter. This process combines the trained LoRA parameters with the frozen base model weights, resulting in a model that incorporates all the adaptations learned during fine-tuning.&lt;/p&gt;
&lt;p&gt;The merging process involves loading both the base model and the LoRA adapter weights, then mathematically combining them according to the LoRA rank decomposition. This creates a new set of weights that directly encode the learned adaptations without requiring the separate LoRA matrices. The resulting merged model behaves identically to using the base model with the LoRA adapter, but it can be used like any standard model without needing the PEFT library or additional adapter handling.&lt;/p&gt;
&lt;p&gt;To merge LoRA weights, you&#39;ll first need to load the base model in the appropriate precision. Using half-precision (float16) is recommended as it reduces memory usage while maintaining accuracy. The base model should be loaded with the same configuration used during training, though you can often disable certain memory-saving features like gradient checkpointing since you won&#39;t be performing backward passes during merging.&lt;/p&gt;
&lt;p&gt;The merging process itself is handled by the &lt;code&gt;PeftModel&lt;/code&gt; class&#39;s &lt;code&gt;merge_and_unload&lt;/code&gt; method. This method performs the mathematical operations to combine the weights and returns a new model with the merged parameters. After merging, it&#39;s important to verify the model&#39;s performance to ensure the merging process completed successfully and the model maintains its fine-tuned behavior.&lt;/p&gt;
&lt;p&gt;Once merged, the model can be saved like any other Hugging Face model using &lt;code&gt;save_pretrained&lt;/code&gt; or &lt;code&gt;push_to_hub&lt;/code&gt;. This creates a standard model checkpoint that can be loaded and used without any special handling for LoRA adapters. The merged model will typically be larger than the LoRA adapter alone, as it contains the full model weights, but it will be more convenient for deployment and inference scenarios where you want to minimize complexity.&lt;/p&gt;
&lt;p&gt;Here&#39;s how to merge and save LoRA weights:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; peft &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PeftModel
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch

&lt;span class=&quot;token comment&quot;&gt;# Load base model in FP16&lt;/span&gt;
base_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Meta-Llama-3-8B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    torch_dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float16&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;auto&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Load and merge weights&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; PeftModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base_model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;./results/checkpoint-100&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
merged_model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;merge_and_unload&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Save merged model&lt;/span&gt;
merged_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results/merged-model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Evaluation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After our training is done we want to evaluate the model to see how it performs. We can do this by using the &lt;code&gt;evaluate_model&lt;/code&gt; function below. This will generate a number of test prompts and compare the model&#39;s responses to the preferred responses in our dataset.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; AutoTokenizer
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tqdm &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tqdm

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;evaluate_model&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; test_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; top_p&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Load model and tokenizer&lt;/span&gt;
    model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        model_path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;auto&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        torch_dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float16
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    correct &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;test_dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Evaluate each example&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; row &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tqdm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;test_dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Format input using chat template&lt;/span&gt;
        prompt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            tokenize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            add_generation_prompt&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Generate response&lt;/span&gt;
        inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;device&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            max_new_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;temperature&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            top_p&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;top_p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            do_sample&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Decode response&lt;/span&gt;
        generated &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prompt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        expected &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Compare with expected answer (exact match)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; generated&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; expected&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            correct &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

    accuracy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; correct &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; total
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Test Accuracy: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;accuracy&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; accuracy
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;General Evals&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To evaluate the model&#39;s performance on public benchmarks like MMLU we can use the &lt;code&gt;lm_eval&lt;/code&gt; library.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone &lt;span class=&quot;token parameter variable&quot;&gt;--depth&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; https://github.com/EleutherAI/lm-evaluation-harness
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; lm-evaluation-harness
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run the evaluation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;lm_eval &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--model&lt;/span&gt; hf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--model_args&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;pretrained&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;./results/merged-model &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--tasks&lt;/span&gt; mmlu,gpqa,gsm8k &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--batch_size&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--output_path&lt;/span&gt; ./eval_results
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most common benchmarks are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;MMLU&lt;/code&gt; - Measures a model&#39;s general knowledge across a broad set of subjects.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GSM8K&lt;/code&gt; - A dataset of 8,000+ single-step arithmetic questions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GPQA&lt;/code&gt; - An eval of graduate-level reasoning questions.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MATH&lt;/code&gt; - A math word problem eval.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Hellaswag&lt;/code&gt; - A common sense reasoning eval.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IFEval&lt;/code&gt; - A instruction following evaluation.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;TLDR+&lt;/code&gt; - A text summarization eval.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Converting to GGUF Format&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After saving your merged model, you&#39;ll want to convert it to GGUF format for efficient inference. First, install llama.cpp. On macOS, use Homebrew:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; llama-cpp
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or build from source:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/ggerganov/llama.cpp
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; llama.cpp
&lt;span class=&quot;token function&quot;&gt;make&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, convert your merged model directly from the &lt;code&gt;results/merged-model&lt;/code&gt; directory:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# From the llama.cpp directory&lt;/span&gt;
python convert_hf_to_gguf.py &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;/results/merged-model &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--outfile&lt;/span&gt; model-f16.bin &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--outtype&lt;/span&gt; f16
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then create a GGUF with quantization using a specific quantization number code (15 is Q4_K_M):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;./llama-quantize model-f16.bin model-q4_k_m.gguf &lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most common quantization type is &lt;code&gt;Q4_K_M&lt;/code&gt; and &lt;code&gt;Q5_K_M&lt;/code&gt; which are &lt;code&gt;15&lt;/code&gt; and &lt;code&gt;17&lt;/code&gt; respectively.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;   &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;  or  Q4_0    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.34G, +0.4685 ppl @ Llama-3-8B
   &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;  or  Q4_1    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.78G, +0.4511 ppl @ Llama-3-8B
   &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;  or  Q5_0    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;.21G, +0.1316 ppl @ Llama-3-8B
   &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;  or  Q5_1    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;.65G, +0.1062 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;19&lt;/span&gt;  or  IQ2_XXS &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2.06&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;  or  IQ2_XS  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2.31&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;28&lt;/span&gt;  or  IQ2_S   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;  bpw quantization
  &lt;span class=&quot;token number&quot;&gt;29&lt;/span&gt;  or  IQ2_M   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2.7&lt;/span&gt;  bpw quantization
  &lt;span class=&quot;token number&quot;&gt;24&lt;/span&gt;  or  IQ1_S   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;1.56&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;31&lt;/span&gt;  or  IQ1_M   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;1.75&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;  or  Q2_K    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;.96G, +3.5199 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;21&lt;/span&gt;  or  Q2_K_S  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;.96G, +3.1836 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;23&lt;/span&gt;  or  IQ3_XXS &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3.06&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;  or  IQ3_S   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3.44&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;27&lt;/span&gt;  or  IQ3_M   &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3.66&lt;/span&gt; bpw quantization mix
  &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;  or  Q3_K    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; Q3_K_M
  &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;  or  IQ3_XS  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3.3&lt;/span&gt; bpw quantization
  &lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;  or  Q3_K_S  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;.41G, +1.6321 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;  or  Q3_K_M  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;.74G, +0.6569 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;13&lt;/span&gt;  or  Q3_K_L  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.03G, +0.5562 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;25&lt;/span&gt;  or  IQ4_NL  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4.50&lt;/span&gt; bpw non-linear quantization
  &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;  or  IQ4_XS  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4.25&lt;/span&gt; bpw non-linear quantization
  &lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;  or  Q4_K    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; Q4_K_M
  &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;  or  Q4_K_S  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.37G, +0.2689 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;15&lt;/span&gt;  or  Q4_K_M  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.58G, +0.1754 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt;  or  Q5_K    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;alias&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; Q5_K_M
  &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;  or  Q5_K_S  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;.21G, +0.1049 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt;  or  Q5_K_M  &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;.33G, +0.0569 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;18&lt;/span&gt;  or  Q6_K    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;.14G, +0.0217 ppl @ Llama-3-8B
   &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;  or  Q8_0    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;.96G, +0.0026 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;33&lt;/span&gt;  or  Q4_0_4_4 &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.34G, +0.4685 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;34&lt;/span&gt;  or  Q4_0_4_8 &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.34G, +0.4685 ppl @ Llama-3-8B
  &lt;span class=&quot;token number&quot;&gt;35&lt;/span&gt;  or  Q4_0_8_8 &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;.34G, +0.4685 ppl @ Llama-3-8B
   &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;  or  F16     &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;.00G, +0.0020 ppl @ Mistral-7B
  &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;  or  BF16    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;14&lt;/span&gt;.00G, &lt;span class=&quot;token parameter variable&quot;&gt;-0.0050&lt;/span&gt; ppl @ Mistral-7B
   &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;  or  F32     &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;26&lt;/span&gt;.00G              @ 7B
          COPY    &lt;span class=&quot;token builtin class-name&quot;&gt;:&lt;/span&gt; only copy tensors, no quantizing
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Test your quantized model:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;llama-cli &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; model-q4_k_m.gguf &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What type of bear is the best?&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--temp&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you&#39;re happy with the results, upload to Hugging Face:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create a new repository for the quantized version&lt;/span&gt;
huggingface-cli repo create my-finetuned-model &lt;span class=&quot;token parameter variable&quot;&gt;--type&lt;/span&gt; model

&lt;span class=&quot;token comment&quot;&gt;# Initialize and push&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; init
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; lfs track &lt;span class=&quot;token string&quot;&gt;&quot;*.gguf&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; .gitattributes model-q4_k_m.gguf
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; commit &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Add quantized model&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; remote &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt; origin https://huggingface.co/your-username/my-finetuned-model
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; push &lt;span class=&quot;token parameter variable&quot;&gt;-u&lt;/span&gt; origin main
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Ollama&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To use with Ollama we&#39;ll need to create a Modelfile. You can use the base model as a starting point. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ollama show &lt;span class=&quot;token parameter variable&quot;&gt;--modelfile&lt;/span&gt; llama3.2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then edit the Modelfile to use your quantized model. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FROM ./model-q4_k_m.gguf
TEMPLATE &amp;quot;&amp;quot;&amp;quot;{{ if .System }}&amp;lt;|start_header_id|&amp;gt;system&amp;lt;|end_header_id|&amp;gt;

{{ .System }}&amp;lt;|eot_id|&amp;gt;{{ end }}{{ if .Prompt }}&amp;lt;|start_header_id|&amp;gt;user&amp;lt;|end_header_id|&amp;gt;

{{ .Prompt }}&amp;lt;|eot_id|&amp;gt;{{ end }}&amp;lt;|start_header_id|&amp;gt;assistant&amp;lt;|end_header_id|&amp;gt;

{{ .Response }}&amp;lt;|eot_id|&amp;gt;&amp;quot;&amp;quot;&amp;quot;
PARAMETER stop &amp;quot;&amp;lt;|start_header_id|&amp;gt;&amp;quot;
PARAMETER stop &amp;quot;&amp;lt;|end_header_id|&amp;gt;&amp;quot;
PARAMETER stop &amp;quot;&amp;lt;|eot_id|&amp;gt;&amp;quot;
PARAMETER stop &amp;quot;&amp;lt;|reserved_special_token|&amp;gt;&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run ollama with your quantized model:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ollama create myllama &lt;span class=&quot;token parameter variable&quot;&gt;--modelfile&lt;/span&gt; Modelfile
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then run the inference server.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;ollama serve
ollama run myllama
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to call the model we can use curl to send a prompt to the server:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; http://localhost:11434/api/chat &lt;span class=&quot;token parameter variable&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;{
  &quot;model&quot;: &quot;myllama&quot;,
  &quot;messages&quot;: [
    { &quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What type of bear is the best?&quot; }
  ]
}&#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or we can use the &lt;code&gt;openai&lt;/code&gt; python client to make API requests to our locally running Ollama server.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; openai

client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; openai&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;OpenAI&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;base_url&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;http://localhost:11434/v1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; api_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;completions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;myllama&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What type of bear is the best?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;choices&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Serving Models&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are a number of options for serving models. Here&#39;s a summary:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;For Local Development &amp;amp; Testing&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ollama&lt;/strong&gt;: The easiest way to run models locally. Think &amp;quot;Docker for LLMs&amp;quot; - simple setup, easy to use, great for testing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;llama.cpp&lt;/strong&gt;: The Swiss Army knife of local model running. Super efficient C++ implementation that can run models on basically anything, from laptops to Raspberry Pis.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;For Production Deployments&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;vLLM&lt;/strong&gt;: The speed demon of LLM serving. If you need to handle lots of requests fast, this is your go-to. Built for high throughput and low latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TGI (Text Generation Inference)&lt;/strong&gt;: HuggingFace&#39;s own serving solution. Great if you&#39;re already in the HuggingFace ecosystem. Rock-solid and well-supported.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SGLang&lt;/strong&gt;: Newest library and allegedly faster than vLLM, focused on serving both language and vision models. Good if you need to handle multiple model types.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;For Enterprise &amp;amp; Cloud&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NVIDIA Solutions&lt;/strong&gt; (TensorRT-LLM &amp;amp; Triton Inference Server): The heavy artillery for GPU deployments. If you&#39;ve got the top-line NVIDIA hardware and need enterprise-grade serving, these are your best bet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ray Serve&lt;/strong&gt;: Great for distributed serving across multiple machines. Think of it as the &amp;quot;orchestrator&amp;quot; for your ML deployments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LitServe&lt;/strong&gt;: New entrant from the Lightning AI team. Promises enterprise-scale serving with easier setup than traditional options.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The choice really depends on your needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For local development → Ollama or llama.cpp&lt;/li&gt;
&lt;li&gt;For production with moderate scale → TGI or vLLM&lt;/li&gt;
&lt;li&gt;For enterprise/cloud with heavy requirements → TensorRT-LLM or Triton Inference Server&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;full-code&quot; tabindex=&quot;-1&quot;&gt;Full Code&lt;/h1&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
finetune.py - A module for fine-tuning Large Language Models using QLoRA
https://stephendiehl.com/posts/training_llms
&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; peft &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; prepare_model_for_kbit_training
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    BitsAndBytesConfig&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SFTTrainer

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;setup_model&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Quantization config for loading in 4 bits&lt;/span&gt;
    bnb_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; BitsAndBytesConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        load_in_4bit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bnb_4bit_quant_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;nf4&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bnb_4bit_compute_dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float16&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bnb_4bit_use_double_quant&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Load base model&lt;/span&gt;
    model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        model_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        quantization_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bnb_config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        device_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;auto&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        trust_remote_code&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prepare_model_for_kbit_training&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Load tokenizer&lt;/span&gt;
    tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pad_token &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eos_token
    tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;padding_side &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;right&quot;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# LoRA config&lt;/span&gt;
    lora_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; LoraConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;q_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;v_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bias&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;none&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        task_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;CAUSAL_LM&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lora_config

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;prepare_dataset&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dataset_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dataset_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Split into train/eval&lt;/span&gt;
    dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train_test_split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;test_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dataset_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; output_dir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Setup model and dataset&lt;/span&gt;
    model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lora_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; setup_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; eval_dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prepare_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;dataset_name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Training arguments&lt;/span&gt;
    training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;output_dir&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cosine&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        warmup_ratio&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.03&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        evaluation_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        eval_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        save_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        save_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        save_total_limit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        load_best_model_at_end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        optim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;paged_adamw_32bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        max_grad_norm&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Initialize trainer&lt;/span&gt;
    trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;train_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;eval_dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        peft_config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;lora_config&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        dataset_text_field&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        max_seq_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        packing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Train and save&lt;/span&gt;
    trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output_dir&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; trainer

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Train model&lt;/span&gt;
    trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        model_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Meta-Llama-3.1-8B-Instruct&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        dataset_name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;databricks/databricks-dolly-15k&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Mon, 25 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/training_llms/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/training_llms/</guid>
    </item>
    
    <item>
      <title>Setting up a Nvidia GH200 for Development</title>
      <description>&lt;h1 id=&quot;setting-up-a-nvidia-gh200-for-development&quot; tabindex=&quot;-1&quot;&gt;Setting up a Nvidia GH200 for Development&lt;/h1&gt;
&lt;p&gt;Setting up an Nvidia GH200 from scratch is a bit fickle. I eventually got it working with CUDA 12.4 and FlashAttention-2, but it took a bit of trial and error. Here&#39;s what I did:&lt;/p&gt;
&lt;p&gt;The GH200 performs optimally with a 64K kernel page size. Nvidia provides specialized kernel packages for Ubuntu systems. First, remove the existing kernel packages and install the Nvidia 64K kernel:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;DEBIAN_FRONTEND&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;noninteractive &lt;span class=&quot;token function&quot;&gt;apt&lt;/span&gt; purge linux-image-&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    linux-headers-&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt; linux-modules-&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt&lt;/span&gt; update
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; linux-nvidia-64k-hwe-22.04 &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reboot&lt;/span&gt; now
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the system reboots, verify you&#39;re using the correct kernel:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It should read:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;linux-nvidia-64k-hwe-22.04-555.42.06
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s update the base system:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; update
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The GH200 requires specific NVIDIA drivers and MLNX_OFED driver. Install these packages. Order is extremely important here!&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; mlnx-fw-updater mlnx-ofed-all
&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; cuda-drivers-555 nvidia-kernel-open-555 linux-tools-&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; cuda-toolkit nvidia-container-toolkit
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The NVIDIA persistence daemon needs to be configured to use the persistence mode.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; /lib/systemd/system/nvidia-persistenced.service.d
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;dd&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;none &lt;span class=&quot;token assign-left variable&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/lib/systemd/system/nvidia-persistenced.service.d/override.conf &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;EOF
[Service]
ExecStart=
ExecStart=/usr/bin/nvidia-persistenced --persistence-mode --verbose
[Install]
WantedBy=multi-user.target
EOF&lt;/span&gt;
systemctl &lt;span class=&quot;token builtin class-name&quot;&gt;enable&lt;/span&gt; nvidia-persistenced &lt;span class=&quot;token parameter variable&quot;&gt;--now&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Configure CUDA paths by creating a new profile script:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;# Library Path for Nvidia CUDA&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; /etc/profile.d/cuda.sh
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/cuda/lib64:&lt;span class=&quot;token string&quot;&gt;&#39;$LD_LIBRARY_PATH&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; /etc/profile.d/cuda.sh
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;&lt;span class=&quot;token environment constant&quot;&gt;PATH&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token environment constant&quot;&gt;$HOME&lt;/span&gt;/.local/bin:/usr/local/cuda/bin:&lt;span class=&quot;token string&quot;&gt;&#39;$PATH&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; /etc/profile.d/cuda.sh
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For optimal performance, disable the IRQ balance service and configure NUMA settings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;systemctl disable irqbalance
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;kernel.numa_balancing = 0&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; /etc/sysctl.conf
&lt;span class=&quot;token function&quot;&gt;sysctl&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s enable peer memory access by adding the NVIDIA peer memory module to the system:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;nvidia-peermem&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; /etc/modules-load.d/nvidia-peermem.conf
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify the module is loaded:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;lsmod &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; nvidia_peermem
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After completing all configurations, reboot your system:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;systemctl disable first-boot.service
&lt;span class=&quot;token function&quot;&gt;reboot&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the system reboots, verify your installation by running:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nvidia-smi
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command should display information about your GH200, including the GPU model, driver version, and current utilization.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;+-----------------------------------------------------------------------------------------+
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; NVIDIA-SMI &lt;span class=&quot;token number&quot;&gt;555.42&lt;/span&gt;.06              Driver Version: &lt;span class=&quot;token number&quot;&gt;555.42&lt;/span&gt;.06      CUDA Version: &lt;span class=&quot;token number&quot;&gt;12.4&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;-----------------------------------------+------------------------+----------------------+
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; GPU  Name                 Persistence-M &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Bus-Id          Disp.A &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Volatile Uncorr. ECC &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Fan  Temp   Perf          Pwr:Usage/Cap &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;           Memory-Usage &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; GPU-Util  Compute M. &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;                                         &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;                        &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;               MIG M. &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;  NVIDIA GH200 480GB             On  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;   00000009:01:00.0 Off &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;                    &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; N/A   33C    P0             76W /  900W &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;       1MiB /  97871MiB &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;      &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;%      Default &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;                                         &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;                        &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;             Disabled &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Processes:                                                                              &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;  GPU   GI   CI        PID   Type   Process name                              GPU Memory &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;        ID   ID                                                               Usage      &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;  No running processes found                                                             &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;
+-----------------------------------------------------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can inspect the system topology to understand how data travels between GPUs and networking components:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nvidia-smi topo &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command shows the connectivity matrix between GPUs and other PCIe devices. For a GH200, you should see something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    GPU0	   NIC0    NIC1    CPU Affinity    NUMA Affinity    GPU NUMA ID
GPU0	 X 	   NODE	   NODE	   0-71            1
NIC0	NODE    X 	   PIX
NIC1	NODE   PIX	   X

Legend:
X    = Self
SYS  = Connection traversing PCIe as well as the SMB interconnect between NUMA nodes (e.g., QPI/UPI)
NODE = Connection traversing PCIe as well as the interconnect between PCIe Host Bridges within a NUMA node
PHB  = Connection traversing PCIe as well as a PCIe Host Bridge (typically the CPU)
PXB  = Connection traversing multiple PCIe bridges (without traversing the PCIe Host Bridge)
PIX  = Connection traversing at most a single PCIe bridge
NV#  = Connection traversing a bonded set of # NVLinks

NIC Legend:
NICO: mlx5_0
NIC1: mlx5_1
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;setting-up-vllm-with-flash-attention&quot; tabindex=&quot;-1&quot;&gt;Setting up vLLM with Flash Attention&lt;/h2&gt;
&lt;p&gt;First, update Python package management tools:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;python &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; pip
python &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; setuptools wheel
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Install PyTorch with CUDA 12.4 support:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; torch --index-url https://download.pytorch.org/whl/cu124
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Install Flash Attention:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clone and build vLLM with Flash Attention support:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone git@github.com:vllm-project/vllm.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; vllm
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;--target&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; vllm_build &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;container_id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; create &lt;span class=&quot;token parameter variable&quot;&gt;--name&lt;/span&gt; vllm_temp vllm_build:latest&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;${container_id}&lt;/span&gt;:/workspace/dist &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install vLLM with Flash Attention&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; vllm-flash-attn
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; dist/vllm-0.4.2+cu124-cp312-cp312-linux_x86_64.whl
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Install xformers from source:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; ninja
&lt;span class=&quot;token comment&quot;&gt;# Set TORCH_CUDA_ARCH_LIST if running and building on different GPU types&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; git+https://github.com/facebookresearch/xformers.git@main&lt;span class=&quot;token comment&quot;&gt;#egg=xformers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Set up your environment variables by adding the following to your &lt;code&gt;~/.bashrc&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;HF_TOKEN&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;HF-TOKEN&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;HF_HOME&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/home/ubuntu/.cache/huggingface&quot;&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;MODEL_REPO&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;meta-llama/Meta-Llama-3.1-70B-Instruct
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Apply the changes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;source&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Launch the vLLM API server with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;vllm serve &lt;span class=&quot;token variable&quot;&gt;$MODEL_REPO&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--dtype&lt;/span&gt; auto
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can now send requests to the API endpoint at &lt;code&gt;http://localhost:8000/v1/completions&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;setting-up-sglang-with-flashinfer&quot; tabindex=&quot;-1&quot;&gt;Setting up sglang with FlashInfer&lt;/h2&gt;
&lt;p&gt;Alternatively we can serve using SGLang with FlashInfer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Use the last release branch&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone &lt;span class=&quot;token parameter variable&quot;&gt;-b&lt;/span&gt; v0.3.5.post2 https://github.com/sgl-project/sglang.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; sglang

pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;python[all]&quot;&lt;/span&gt;

pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flashinfer &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; https://flashinfer.ai/whl/cu121/torch2.4/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Launch the SGLang server with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;python &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-70B-Instruct
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Likewise, you can send requests to the API endpoint at &lt;code&gt;http://localhost:8000/v1/completions&lt;/code&gt; using the OpenAI API format.&lt;/p&gt;
</description>
      <pubDate>Wed, 20 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/setup_gh200_tutorial/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/setup_gh200_tutorial/</guid>
    </item>
    
    <item>
      <title>Hymba : Mamba × Transformers</title>
      <description>&lt;h1 id=&quot;hymba-%3A-mamba-%C3%97-transformers&quot; tabindex=&quot;-1&quot;&gt;Hymba : Mamba × Transformers&lt;/h1&gt;
&lt;p&gt;Up until now we&#39;ve had transformers, which are the core deep learning architecture based on the attention mechanism. But there are other experimental architectures that have been gaining a lot of attention lately. One of the more promising ones is the state-space model &lt;strong&gt;Mamba&lt;/strong&gt;, an architecture developed by researchers at Carnegie Mellon University and Princeton University that aims to address some key limitations of transformer models, particularly in processing long sequences and large context windows.&lt;/p&gt;
&lt;p&gt;At its core, Mamba is built on three fundamental ideas. The first is its use of selective state spaces - rather than relying on attention mechanisms like transformers do, Mamba employs SSMs that can selectively process information based on the current input. This selective processing allows the model to effectively focus on relevant information while discarding what isn&#39;t needed. The theory is best described in this &lt;a href=&quot;https://srush.github.io/annotated-s4/&quot;&gt;blog post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Mamba offers several key advantages over traditional transformer architectures. The most significant benefits is better scaling through linear-time processing, making it substantially more efficient when handling long sequences compared to transformers&#39; quadratic scaling behavior. The architecture is also notably memory efficient, carefully avoiding the materialization of expanded states in memory-intensive layers that can bog down other models.&lt;/p&gt;
&lt;p&gt;The second idea is Mamba&#39;s simplified architecture. Unlike transformers which separate attention and MLP blocks, Mamba unifies these into a single SSM block. This architectural simplification leads to reduced computational complexity and improved inference speed. However, despite the conceptual promise, SSMs have been shown to have limited recall capabilities and underperforming on in-context learning tasks.&lt;/p&gt;
&lt;p&gt;In short, traditional attention heads are great at precisely remembering and recalling specific information, like a photographic memory, though they&#39;re computationally expensive and memory-hungry. state space models, on the other hand, are more efficient but work somewhat like human memory - good at capturing the general gist of things but not great at precise recall.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Transformer&lt;/th&gt;
&lt;th&gt;Mamba&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Core Mechanism&lt;/td&gt;
&lt;td&gt;Attention-based&lt;/td&gt;
&lt;td&gt;SSM-based&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Architectural Complexity&lt;/td&gt;
&lt;td&gt;Higher (separate attention &amp;amp; MLP blocks)&lt;/td&gt;
&lt;td&gt;Lower (unified SSM block)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Training Complexity&lt;/td&gt;
&lt;td&gt;&#92;(O(n^2)&#92;)&lt;/td&gt;
&lt;td&gt;&#92;(O(n)&#92;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inference Speed&lt;/td&gt;
&lt;td&gt;&#92;(O(n)&#92;)&lt;/td&gt;
&lt;td&gt;&#92;(O(1)&#92;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Hymba&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nvidia &lt;a href=&quot;https://www.arxiv.org/abs/2411.13676&quot;&gt;recently explored&lt;/a&gt; combining different computational paradigms to leverage their complementary strengths. A notable example is the Hymba architecture, which introduces a hybrid-head parallel design that integrates transformer attention mechanisms with state space models within the same layer. The end result is a model that outperforms larger models while using significantly less memory. On the self-reported benchmarks, Hymba-1.5B beats Llama-3.2-3B while using 11.67x less memory cache and running 3.49x faster. Which is incredibly impressive.&lt;/p&gt;
&lt;p&gt;The key innovation in hybrid architectures like Hymba is the parallel fusion of attention and SSM heads, allowing simultaneous processing of inputs through different computational paths. In contrast to sequential approaches that stack different layer types, parallel fusion enables each layer to simultaneously leverage both the high-resolution recall capabilities of attention and the efficient context summarization of SSMs. This is implemented through a unified formulation where the input is projected to both attention components (query, key, value) and SSM components (input features and gates) in parallel. The outputs from attention heads and SSM heads are normalized and combined using learnable scaling factors &#92;(&#92;beta&lt;em&gt;1&#92;) and &#92;(&#92;beta_2&#92;). Specifically, if we denote the attention output as &#92;(Y&lt;/em&gt;{attn}&#92;) and the SSM output as &#92;(Y_{ssm}&#92;), the final layer output is computed as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
Y = W_{out_proj}(&#92;beta_1 &#92;cdot &#92;text{norm}(M_{attn} &#92;cdot X) + &#92;beta_2 &#92;cdot &#92;text{norm}(M_{ssm} &#92;cdot X))&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where &#92;(&#92;beta_1&#92;) and &#92;(&#92;beta_2&#92;) are control the relative contribution of each path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Memory and Efficiency Optimizations&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To address the memory demands of attention mechanisms, hybrid architectures employ several optimizations. These include combining global and local attention patterns, where only select layers (typically first, middle, and last) use full attention while others use sliding window attention. Additionally, key-value cache sharing between layers reduces memory requirements without significantly impacting model performance.&lt;/p&gt;
&lt;p&gt;The SSM components help maintain global context awareness even with localized attention, allowing more aggressive use of sliding window attention compared to pure transformer architectures. Empirical studies show that hybrid models can maintain comparable task performance while achieving significant reductions in memory usage and increased throughput.&lt;/p&gt;
&lt;p&gt;Additionally the architecture includes learnable meta tokens that are prepended to input sequences. These tokens serve a twofold purpose: they act as learned cache initialization, help redistribute attention patterns more effectively, and store compressed world knowledge. The meta tokens participate in both attention and SSM computations, essentially providing a form of learned memory initialization that helps guide the model&#39;s focus toward relevant information.&lt;/p&gt;
&lt;p&gt;The Hybrid architecture empirically demonstrates several advantages over both pure transformer and pure SSM architectures. In the paper, it achieves better effective receptive field measurements while maintaining reasonable cache sizes. The relative importance of attention and SSM heads varies by layer and task, suggesting that the architecture learns to leverage different computational modes as needed. This flexibility contributes to strong performance across both general language tasks and memory-intensive applications while maintaining competitive efficiency metrics.&lt;/p&gt;
&lt;p&gt;These hybrid architectures are super exciting - they show us that the future of language models probably isn&#39;t about picking between different approaches, but rather mixing and matching the best parts of each. By cleverly combining different architectures like this, we could theoretically get the best of both worlds - fast, efficient models that are only worst case quadratic and still incredibly capable. The early results are really promising.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using It&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before running the code, ensure you have the correct environment setup with CUDA 12.4 and &lt;a href=&quot;https://pytorch.org/blog/flexattention/&quot;&gt;FlexAttention&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install PyTorch with CUDA 12.4&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;pytorch&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;.0
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;torchvision&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.20&lt;/span&gt;.0
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;torchaudio&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;.0
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; pytorch-cuda&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12.4&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install required packages&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--upgrade&lt;/span&gt; transformers
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; tiktoken sentencepiece protobuf ninja einops triton packaging

&lt;span class=&quot;token comment&quot;&gt;# Install Mamba&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/state-spaces/mamba.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; mamba
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install causal-conv1d&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/Dao-AILab/causal-conv1d.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; causal-conv1d
&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;CUDA_HOME&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;/usr/local/cuda-12.4
&lt;span class=&quot;token builtin class-name&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token assign-left variable&quot;&gt;TORCH_CUDA_ARCH_LIST&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;7.0;7.5;8.0;8.6;8.9;9.0&quot;&lt;/span&gt;
python setup.py &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install attention-gym&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/pytorch-labs/attention-gym.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; attention-gym
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install Flash Attention&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash_attn
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s a minimal example of using Hymba through Hugging Face:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt;
    AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    StopStringCriteria&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    StoppingCriteriaList

&lt;span class=&quot;token comment&quot;&gt;# Initialize model and tokenizer for chat&lt;/span&gt;
repo_name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;nvidia/Hymba-1.5B-Instruct&quot;&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoTokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;repo_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; trust_remote_code&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;repo_name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; trust_remote_code&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;torch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bfloat16&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;chat_with_model&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;messages&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_new_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Format the conversation using the model&#39;s chat template&lt;/span&gt;
    tokenized_chat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        messages&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        tokenize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        add_generation_prompt&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;cuda&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Set up stopping criteria to properly end responses&lt;/span&gt;
    stopping_criteria &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; StoppingCriteriaList&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        StopStringCriteria&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; stop_strings&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&amp;lt;/s&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Generate response&lt;/span&gt;
    outputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        tokenized_chat&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        max_new_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;max_new_tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        do_sample&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        use_cache&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        stopping_criteria&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;stopping_criteria
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Decode and return only the new tokens&lt;/span&gt;
    input_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenized_chat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;outputs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;input_length&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skip_special_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; response

&lt;span class=&quot;token comment&quot;&gt;# Interactive chat loop&lt;/span&gt;
messages &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;You are a helpful assistant.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Chat with Hymba (type &#39;exit&#39; to quit):&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;User:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        prompt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; prompt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lower&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;exit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;

        messages&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; prompt&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; chat_with_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;messages&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        messages&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Model: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Wed, 20 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mamba_x_transformers/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mamba_x_transformers/</guid>
    </item>
    
    <item>
      <title>Dependent Types in 200 Lines of Python</title>
      <description>&lt;h1 id=&quot;dependent-types-in-200-lines-of-python&quot; tabindex=&quot;-1&quot;&gt;Dependent Types in 200 Lines of Python&lt;/h1&gt;
&lt;p&gt;The Calculus of Constructions (shortened as CoC) is one of the most common typed lambda calculi, forming the theoretical foundation for dependently typed programming languages like Coq. In this post, we&#39;ll implement a type checker for CoC in 200 lines of Python, breaking down the key components and explaining how they work. This is mostly a code-golf exercise, so the implementation is not super robust but it is fun that we can even implement this in Python.&lt;/p&gt;
&lt;p&gt;Some background, the CoC was developed by Thierry Coquand in 1985, and is a higher-order typed lambda calculus that combines two key ideas:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Dependent types - types that can depend on terms&lt;/li&gt;
&lt;li&gt;Type polymorphism - the ability to quantify over types&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The system has two sorts: &lt;code&gt;*&lt;/code&gt; (the type of types) and &lt;code&gt;☐&lt;/code&gt; (the type of &lt;code&gt;*&lt;/code&gt;). It supports the following terms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Lambda abstractions (λx:A.t)&lt;/li&gt;
&lt;li&gt;Function applications (t u)&lt;/li&gt;
&lt;li&gt;Dependent product types (Πx:A.B)&lt;/li&gt;
&lt;li&gt;Type annotations (t : A)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s break down our implementation into its core components. We have the term representation, in which we approximate algebraic data types using Python classes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Understanding De Bruijn Indices&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before diving into the implementation, let&#39;s understand De Bruijn indices, which we use for variable lookup. In De Bruijn notation, variables are represented by numbers that indicate how many binders you need to go up to find the variable&#39;s binding site. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lambda&quot;&gt;# Traditional notation:
λx. λy. x y

# De Bruijn notation:
λ. λ. 1 0    # 1 refers to x (up one binder), 0 refers to y (current binder)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More examples:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lambda&quot;&gt;# Traditional:        # De Bruijn:          # Explanation:
λx. x                 λ. 0                  # x is bound by the current (0th) binder
λx. λy. y             λ. λ. 0               # y is bound by the current binder
λx. λy. λz. x y z     λ. λ. λ. 2 1 0        # x=2, y=1, z=0 binders up
Πx:*. λy. x           Π*. λ. 1              # x is one binder up from y
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In our implementation, we use De Bruijn levels (counting from the outside) rather than indices (counting from the inside) for the context lookup. This is why we use &lt;code&gt;lvl - x - 1&lt;/code&gt; to access the context, converting from levels to indices.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Type Term Hierarchy&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Term&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;pass&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Lam&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Pi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term
    f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Appl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term
    n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Ann&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term
    a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FreeVar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Star&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;pass&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For term evaluation, we implement beta reduction, the core computational rule of lambda calculus. The &lt;code&gt;eval&lt;/code&gt; function implements call-by-value evaluation strategy and is crucial for type checking since dependent types require comparing types for computational equality.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            m_eval &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            n_eval &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; m_eval&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_eval&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m_eval&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_eval&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; term
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type checker is split into three mutually recursive functions. Some important implementation details to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;infer_ty&lt;/code&gt; and &lt;code&gt;check_ty&lt;/code&gt; functions always return evaluated types&lt;/li&gt;
&lt;li&gt;The typing context (&lt;code&gt;ctx&lt;/code&gt;) must contain only evaluated types and be well-formed (every entry must be of some sort)&lt;/li&gt;
&lt;li&gt;Bindings are added to the beginning of the context and indexed by De Bruijn indices&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;FreeVar&lt;/code&gt; uses De Bruijn levels, we use &lt;code&gt;lvl - x - 1&lt;/code&gt; to access the context&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;infer_ty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            _s1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            s2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unfurl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; s2
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            m_ty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; m_ty&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; check_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Want a Pi type, got &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            _s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; check_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;lvl &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Has no type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;infer_ty&lt;/code&gt; function implements bidirectional type checking:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For Pi types, ensures both the domain and codomain are sorts&lt;/li&gt;
&lt;li&gt;For applications, infers the function type and checks the argument&lt;/li&gt;
&lt;li&gt;For annotations, verifies the type is valid and checks the term against it&lt;/li&gt;
&lt;li&gt;For variables, looks up their type in the context&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;*&lt;/code&gt;, returns &lt;code&gt;☐&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;☐&lt;/code&gt; has no type (it&#39;s at the top of the hierarchy)&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;infer_sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    ty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; ty&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ty
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Want a sort, got &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;infer_sort&lt;/code&gt; function ensures a term has type &lt;code&gt;*&lt;/code&gt; or &lt;code&gt;☐&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Infers the type of the term&lt;/li&gt;
&lt;li&gt;Verifies it&#39;s a sort&lt;/li&gt;
&lt;li&gt;Returns the sort for use in type formation rules&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;check_ty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; pair&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pair
    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; check_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unfurl2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Want a Pi type, got &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            got_ty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; infer_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; got_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ty
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; panic&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Want type &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, got &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; got_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;equate&lt;/code&gt; function implements structural equality checking. When checking two terms for beta-convertibility (computational equality), we must first evaluate them before calling &lt;code&gt;equate&lt;/code&gt;. In &lt;code&gt;check_ty&lt;/code&gt;, when comparing the inferred type (&lt;code&gt;got_ty&lt;/code&gt;) with the expected type (&lt;code&gt;ty&lt;/code&gt;), both are already in normal form so no additional evaluation is needed.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;equate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; terms&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;plunge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unfurl2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; pair&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; terms&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; plunge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; plunge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; equate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; y
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;equate&lt;/code&gt; function implements structural equality checking:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Compares terms recursively, handling all term constructors&lt;/li&gt;
&lt;li&gt;Uses HOAS comparison for bound variables&lt;/li&gt;
&lt;li&gt;Implements alpha-equivalence through the level counting mechanism&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unfurl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;unfurl2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; pair&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pair
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unfurl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unfurl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These utility functions handle HOAS variable manipulation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;unfurl&lt;/code&gt; converts a HOAS function to a term with a free variable&lt;/li&gt;
&lt;li&gt;&lt;code&gt;unfurl2&lt;/code&gt; does the same for pairs of functions&lt;/li&gt;
&lt;li&gt;The level parameter ensures proper scoping of variables&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print_term&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;plunge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Callable&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unfurl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;(λ&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;plunge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;(Π&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;plunge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;print_term&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lvl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;*&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Box&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;☐&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;print_term&lt;/code&gt; function converts terms to readable string representations:&lt;/p&gt;
&lt;p&gt;Now let&#39;s look at two &amp;quot;practical&amp;quot; examples of CoC in action: Church numerals and length-indexed vectors. To make the examples more readable, we use some helper functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;curry2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;curry3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; curry2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;curry4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; curry3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;curry5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Lam&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; curry4&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;appl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Term&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Church numerals represent natural numbers as functions. In CoC, we can give them precise types:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# The type of Church numerals&lt;/span&gt;
n_ty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
          Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
             Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Zero is the identity function&lt;/span&gt;
zero &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;curry3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Successor applies f one more time&lt;/span&gt;
succ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    curry4&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; n_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Addition combines the function applications&lt;/span&gt;
add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Ann&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    curry5&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
          appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; n_ty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&#39;s what&#39;s happening:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A Church numeral takes a type &lt;code&gt;a&lt;/code&gt;, a function &lt;code&gt;f: a → a&lt;/code&gt;, and a value &lt;code&gt;x: a&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;It applies &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;x&lt;/code&gt; n times, where n is the number being represented&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zero&lt;/code&gt; returns x unchanged&lt;/li&gt;
&lt;li&gt;&lt;code&gt;succ n&lt;/code&gt; applies f one more time than n does&lt;/li&gt;
&lt;li&gt;&lt;code&gt;add n m&lt;/code&gt; applies f n+m times&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can also use dependent types to create vectors whose length is statically known:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Vector context setup&lt;/span&gt;
vect_ctx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Vector type constructor: (n: Nat) → (a: Type) → Type&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Item type and value&lt;/span&gt;
    Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    item_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# replicate: (n: Nat) → (a: Type) → a → Vec n a&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
       Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
          Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# concat: (n m: Nat) → (a: Type) → Vec n a → Vec m a → Vec (n+m) a&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
       Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
          Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
             Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                   vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# zip: (n: Nat) → (a b: Type) → Vec n a → Vec n b → Vec n (Pair a b)&lt;/span&gt;
    Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
       Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
          Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Star&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
             Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                Pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; _y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                   vect_ty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pair&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The vector operations demonstrate dependent typing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;replicate&lt;/code&gt; creates a vector of length n filled with a value&lt;/li&gt;
&lt;li&gt;&lt;code&gt;concat&lt;/code&gt; combines vectors, with the result length being the sum&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zip&lt;/code&gt; combines two vectors of the same length&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&#39;s how we can use these operations:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create vectors of different lengths&lt;/span&gt;
vect_one &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; replicate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
vect_three &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; replicate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;three&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
vect_four &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; concat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; three&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_three&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# This will type check&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;four&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_four&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_four&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# This will fail type checking - mismatched lengths&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;four&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_one&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; vect_four&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# TypeError!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type system ensures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Vector lengths are tracked precisely&lt;/li&gt;
&lt;li&gt;Operations maintain length invariants&lt;/li&gt;
&lt;li&gt;Length mismatches are caught statically&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These examples demonstrate how CoC can express both computation and static guarantees in a unified system. The type checker ensures our operations respect these guarantees while still allowing flexible programming patterns. Dependent types are cool.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the implementation of these vector operations in more detail:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Replicate&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# replicate: (n: Nat) → (a: Type) → a → Vec n a&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;replicate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The replicate function creates a vector of length &lt;code&gt;n&lt;/code&gt; where every element is &lt;code&gt;x&lt;/code&gt;. The type ensures that the resulting vector&#39;s length matches the input number &lt;code&gt;n&lt;/code&gt;. In a real implementation, this would construct a list of n copies of x, but in our type-only implementation we just track the types.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Concatenate&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# concat: (n m: Nat) → (a: Type) → Vec n a → Vec m a → Vec (n+m) a&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The concat function combines two vectors. Its type is particularly interesting because it shows how dependent types can track arithmetic relationships - the output vector&#39;s length is &lt;code&gt;n + m&lt;/code&gt;, the sum of the input lengths. This is enforced statically by the type system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Zip&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# zip: (n: Nat) → (a b: Type) → Vec n a → Vec n b → Vec n (Pair a b)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Term&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; appl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FreeVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item_ty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The zip function combines two vectors element-wise into a vector of pairs. The type system enforces that both input vectors must have the same length &lt;code&gt;n&lt;/code&gt;, and guarantees the output vector will also have length &lt;code&gt;n&lt;/code&gt;. If you try to zip vectors of different lengths, you&#39;ll get a type error at compile time rather than a runtime error.&lt;/p&gt;
&lt;p&gt;These operations form a small vector library with static length checking. The type system ensures we can&#39;t:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Concatenate vectors and get the length wrong&lt;/li&gt;
&lt;li&gt;Zip vectors of different lengths&lt;/li&gt;
&lt;li&gt;Create a vector with a length that doesn&#39;t match its type&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This demonstrates how dependent types can encode precise specifications directly in the type system. While our implementation is just a type checker (we don&#39;t actually construct the vectors), it shows how these ideas work in languages like Coq or Agda where you would have real implementations.&lt;/p&gt;
</description>
      <pubDate>Mon, 18 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/calculus_of_constructions_python/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/calculus_of_constructions_python/</guid>
    </item>
    
    <item>
      <title>Setting up PyTorch for OCaml</title>
      <description>&lt;h1 id=&quot;setting-up-pytorch-for-ocaml&quot; tabindex=&quot;-1&quot;&gt;Setting up PyTorch for OCaml&lt;/h1&gt;
&lt;p&gt;Setting up OCaml for modern AI development is a surprisingly niche topic, despite OCaml&#39;s strengths in building robust, type-safe systems. This guide focuses specifically on configuring OCaml development environments for NVIDIA&#39;s HGX AI platforms with 8x H100 GPUs for doing either continued pre-training or full-tuning of large language models. While a few quant funds have invested heavily in OCaml for their AI infrastructure, there&#39;s remarkably little public documentation about setting up OCaml with these dense GPU configurations. We&#39;ll walk through a complete setup of OCaml with CUDA and PyTorch on Ubuntu, with specific attention to the unique requirements of these multi-GPU systems that a lot of funds are provisioning these days.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;System Dependencies&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before starting, ensure you&#39;re running Ubuntu. This setup has been tested on Ubuntu and requires root privileges for several installation steps.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Check your Ubuntu version&lt;/span&gt;
lsb_release &lt;span class=&quot;token parameter variable&quot;&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, let&#39;s install the basic system dependencies. These packages provide essential development tools and libraries needed for the rest of our setup:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; update
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    build-essential &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    pciutils &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    clang &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    libaio-dev &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    python3-pip &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    python3-venv
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;NVIDIA Driver Installation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The NVIDIA driver installation varies depending on whether you&#39;re running a desktop or headless system:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# For desktop systems with X.org or Wayland&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; nvidia-driver-550

&lt;span class=&quot;token comment&quot;&gt;# For headless systems (servers without display)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; --no-install-recommends nvidia-headless-550

&lt;span class=&quot;token comment&quot;&gt;# Install CUDA toolkit&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; nvidia-cuda-toolkit
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This installs the NVIDIA driver version 550 and the CUDA toolkit, which provides the necessary components for GPU acceleration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Python Environment Setup&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First, let&#39;s install pyenv and configure Python 3.12.4:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install pyenv dependencies&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;make&lt;/span&gt; build-essential libssl-dev zlib1g-dev &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
libbz2-dev libreadline-dev libsqlite3-dev &lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; llvm &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

&lt;span class=&quot;token comment&quot;&gt;# Install pyenv&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; https://pyenv.run &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;bash&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Add pyenv to your shell configuration (~/.bashrc or ~/.zshrc)&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;export PYENV_ROOT=&quot;$HOME/.pyenv&quot;&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;command -v pyenv &gt;/dev/null || export PATH=&quot;$PYENV_ROOT/bin:$PATH&quot;&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; ~/.bashrc
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;eval &quot;$(pyenv init -)&quot;&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; ~/.bashrc

&lt;span class=&quot;token comment&quot;&gt;# Reload shell configuration&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;source&lt;/span&gt; ~/.bashrc

&lt;span class=&quot;token comment&quot;&gt;# Install Python 3.12.4&lt;/span&gt;
pyenv &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.12&lt;/span&gt;.4

&lt;span class=&quot;token comment&quot;&gt;# Set Python 3.12.4 as global version&lt;/span&gt;
pyenv global &lt;span class=&quot;token number&quot;&gt;3.12&lt;/span&gt;.4

&lt;span class=&quot;token comment&quot;&gt;# Verify installation&lt;/span&gt;
python &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Should output: Python 3.12.4&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Upgrade pip&lt;/span&gt;
python &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--upgrade&lt;/span&gt; pip

&lt;span class=&quot;token comment&quot;&gt;# Install PyTorch with CUDA support&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

&lt;span class=&quot;token comment&quot;&gt;# Install NVIDIA cuDNN&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; nvidia-cudnn-cu11

&lt;span class=&quot;token comment&quot;&gt;# Install Hugging Face libraries&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; transformers datasets accelerate

&lt;span class=&quot;token comment&quot;&gt;# Install scientific computing packages&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; scipy numpy pandas

&lt;span class=&quot;token comment&quot;&gt;# Install optimized attention implementations&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; git+https://github.com/facebookresearch/xformers.git@main&lt;span class=&quot;token comment&quot;&gt;#egg=xformers&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install GPU monitoring tools&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; nvitop
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;DeepSpeed and Megatron-LM Installation&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;deepspeed-installation&quot; tabindex=&quot;-1&quot;&gt;DeepSpeed Installation&lt;/h3&gt;
&lt;p&gt;DeepSpeed provides optimization features for training large models. Let&#39;s install it with CUDA operations support:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Basic installation&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed

&lt;span class=&quot;token comment&quot;&gt;# OR: Install from source for latest features&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/microsoft/DeepSpeed.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; DeepSpeed
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# OR: Install with pre-compiled operations (recommended)&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;DS_BUILD_OPS&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed --global-option&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;build_ext&quot;&lt;/span&gt; --global-option&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-j8&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify the installation and check which operations are available:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;ds_report
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Advanced installation options:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install specific operations&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;DS_BUILD_FUSED_ADAM&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed    &lt;span class=&quot;token comment&quot;&gt;# FusedAdam support&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;DS_BUILD_FUSED_LAMB&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed    &lt;span class=&quot;token comment&quot;&gt;# FusedLamb support&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;DS_BUILD_SPARSE_ATTN&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed   &lt;span class=&quot;token comment&quot;&gt;# Sparse attention support&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Skip CUDA version check if needed (use with caution)&lt;/span&gt;
&lt;span class=&quot;token assign-left variable&quot;&gt;DS_SKIP_CUDA_CHECK&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;megatron-lm-installation&quot; tabindex=&quot;-1&quot;&gt;Megatron-LM Installation&lt;/h3&gt;
&lt;p&gt;Megatron-LM enables training large transformer models at scale. Here&#39;s how to set it up:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install NVIDIA Apex first&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/NVIDIA/apex
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; apex
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; --disable-pip-version-check --no-cache-dir &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    --global-option&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;--cpp_ext&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    --global-option&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;--cuda_ext&quot;&lt;/span&gt; ./
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install Megatron-LM&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/NVIDIA/Megatron-LM.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; Megatron-LM
&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; checkout core_r0.5.0
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; --no-use-pep517 &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;OCaml Setup&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Finally, we&#39;ll set up the OCaml environment with PyTorch bindings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install OCaml package manager&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; opam m4 pkg-config

&lt;span class=&quot;token comment&quot;&gt;# Initialize OPAM&lt;/span&gt;
opam init --auto-setup &lt;span class=&quot;token parameter variable&quot;&gt;--yes&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;opam &lt;span class=&quot;token function&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create new OCaml environment&lt;/span&gt;
opam switch create &lt;span class=&quot;token number&quot;&gt;4.14&lt;/span&gt;.1
&lt;span class=&quot;token builtin class-name&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;opam &lt;span class=&quot;token function&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install OCaml development tools&lt;/span&gt;
opam &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; dune merlin ocaml-lsp-server odoc ocamlformat utop

&lt;span class=&quot;token comment&quot;&gt;# Install Torch dependencies and bindings&lt;/span&gt;
opam &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; ctypes ctypes-foreign conf-pkg-config
opam &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; torch
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This sets up:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;OPAM (OCaml Package Manager)&lt;/li&gt;
&lt;li&gt;OCaml 4.14.1 environment&lt;/li&gt;
&lt;li&gt;Development tools including:
&lt;ul&gt;
&lt;li&gt;Dune (build system)&lt;/li&gt;
&lt;li&gt;Merlin (code completion)&lt;/li&gt;
&lt;li&gt;LSP server (IDE integration)&lt;/li&gt;
&lt;li&gt;Documentation tools&lt;/li&gt;
&lt;li&gt;UTop (enhanced REPL)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;PyTorch bindings for OCaml&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Verification&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After installation and system reboot, verify your setup:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Check NVIDIA driver installation&lt;/span&gt;
nvidia-smi

&lt;span class=&quot;token comment&quot;&gt;# Check GPU interconnect topology&lt;/span&gt;
nvidia-smi topo &lt;span class=&quot;token parameter variable&quot;&gt;-m&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Start OCaml REPL and test Torch&lt;/span&gt;
utop
&lt;span class=&quot;token comment&quot;&gt;# In utop:&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;open&lt;/span&gt; Torch&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should see your GPU listed in the &lt;code&gt;nvidia-smi&lt;/code&gt; output and be able to import the Torch module in OCaml without errors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Optional: Experiment Tracking Setup&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you plan to track your machine learning experiments, you can set up Weights &amp;amp; Biases alongside Hugging Face:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install Weights &amp;amp; Biases&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; wandb

&lt;span class=&quot;token comment&quot;&gt;# Log in to Weights &amp;amp; Biases (this will prompt for your API key)&lt;/span&gt;
wandb login

&lt;span class=&quot;token comment&quot;&gt;# Install and log in to Hugging Face&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; huggingface_hub
huggingface-cli login
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can get your W&amp;amp;B API key from &lt;a href=&quot;https://wandb.ai/settings&quot;&gt;https://wandb.ai/settings&lt;/a&gt; and your Hugging Face token from &lt;a href=&quot;https://huggingface.co/settings/tokens&quot;&gt;https://huggingface.co/settings/tokens&lt;/a&gt; .&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example Project: XOR Neural Network&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s create the Hello World OCaml project that trains a neural network to learn the XOR function. First, create a new project:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Create project directory&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; xor_example
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; xor_example

&lt;span class=&quot;token comment&quot;&gt;# Initialize dune project&lt;/span&gt;
dune init project xor_example
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; xor_example
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, create a new file &lt;code&gt;bin/main.ml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;open Base
open Torch

(* XOR truth table *)
let input_data =
  Tensor.of_float2 [|
    [|0.; 0.|];
    [|0.; 1.|];
    [|1.; 0.|];
    [|1.; 1.|];
  |]

let target_data =
  Tensor.of_float2 [|
    [|0.|];
    [|1.|];
    [|1.|];
    [|0.|];
  |]

let () =
  (* Create a simple network with one hidden layer *)
  let vs = Var_store.create ~name:&amp;quot;xor&amp;quot; () in
  let hidden = Layer.linear vs ~input_dim:2 4 ~activation:Relu in
  let output = Layer.linear vs ~input_dim:4 1 ~activation:Sigmoid in

  (* Define the model *)
  let model x =
    Layer.forward hidden x
    |&amp;gt; Layer.forward output
  in

  (* Training configuration *)
  let learning_rate = 0.05 in
  let optimizer = Optimizer.adam vs ~learning_rate in

  (* Training loop *)
  for epoch = 1 to 2000 do
    (* Forward pass *)
    let predicted = model input_data in
    let loss = Tensor.mse_loss predicted target_data in

    (* Backward pass *)
    Optimizer.backward_step optimizer ~loss;

    (* Print progress every 100 epochs *)
    if epoch % 100 = 0 then
      Stdio.printf &amp;quot;Epoch %d: Loss = %f&#92;n%!&amp;quot;
        epoch (Tensor.float_value loss)
  done;

  (* Print final results *)
  let final_output = model input_data in
  Stdio.printf &amp;quot;&#92;nFinal predictions:&#92;n&amp;quot;;
  Tensor.print final_output
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Update the &lt;code&gt;dune&lt;/code&gt; file in the &lt;code&gt;bin&lt;/code&gt; directory:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-scheme&quot;&gt;(executable
 (name main)
 (libraries base stdio torch))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Build and run the project:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Build the project&lt;/span&gt;
dune build

&lt;span class=&quot;token comment&quot;&gt;# Run the example&lt;/span&gt;
dune &lt;span class=&quot;token builtin class-name&quot;&gt;exec&lt;/span&gt; xor_example
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If that runs correctly, congratuinals you&#39;re ready to orchestrate your own training runs with OCaml now.&lt;/p&gt;
</description>
      <pubDate>Sun, 17 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/cuda_ocaml/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/cuda_ocaml/</guid>
    </item>
    
    <item>
      <title>New Rank 29 Elliptic Curve</title>
      <description>&lt;h1 id=&quot;new-rank-29-elliptic-curve&quot; tabindex=&quot;-1&quot;&gt;New Rank 29 Elliptic Curve&lt;/h1&gt;
&lt;p&gt;In August 2024, mathematicians Noam Elkies and Zev Klagsbrun &lt;a href=&quot;https://listserv.nodak.edu/cgi-bin/wa.exe?A2=NMBRTHRY;b9d018b1.2409&amp;amp;S=b&quot;&gt;discovered&lt;/a&gt; an elliptic curve with the highest known rank over rational numbers: 29. This breakthrough surpassed the previous record of rank 28 that had stood since 2006.&lt;/p&gt;
&lt;p&gt;If we recall from &lt;a href=&quot;https://www.amazon.co.uk/Elementary-Number-Springer-Undergraduate-Mathematics/dp/3540761977&quot;&gt;undergraduate number theory&lt;/a&gt;, an elliptic curve is a special type of cubic equation, similar to the quadratic equations you might remember from algebra (like &#92;(y = x^2 + 2x + 1&#92;)), but with one term raised to the third power. While quadratic equations are relatively straightforward to solve, elliptic curves exhibit far more complex behavior that we&#39;re still working to understand. The particular equation we&#39;re looking at has the form:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
y^2 + xy = x^3 + ax + b&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;When we look for rational number solutions to these equations — solutions where &#92;(x&#92;) and &#92;(y&#92;) are rational numbers — we find something fascinating: these solutions can be grouped into families, where new solutions can be derived from existing ones through mathematical operations. The number of independent families of solutions is called the &amp;quot;rank&amp;quot; of the curve.&lt;/p&gt;
&lt;p&gt;Think of rank as measuring how many different &amp;quot;starting points&amp;quot; you need to generate all possible rational solutions. Most elliptic curves have small ranks (0, 1, or 2), which makes this rank-29 curve exceptionally unique. The specific coefficients for this curve are:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
a = -27006183241630922218434652145297453784768054621836357954737385&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
b = 55258058551342376475736699591118191821521067032535079608372404779149413277716173425636721497&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Here&#39;s a toy Python implementation using the &lt;code&gt;sympy&lt;/code&gt; library to do the arbitrary precision arithmetic needed to work with this curve:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; sympy &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; solve&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Rational

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;E29&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;27006183241630922218434652145297453784768054621836357954737385&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;55258058551342376475736699591118191821521067032535079608372404779149413277716173425636721497&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EllipticCurve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b

        &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_y_coordinates&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Find y coordinates for a given x on the curve&quot;&quot;&quot;&lt;/span&gt;
            x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Rational&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Convert equation to standard form: y^2 + xy - (x^3 + ax + b) = 0&lt;/span&gt;
            y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Symbol&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;y&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            eq &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eq&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_on_curve&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Check if point (x,y) lies on the curve&quot;&quot;&quot;&lt;/span&gt;
            x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Rational&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Rational&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; EllipticCurve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Example usage:&lt;/span&gt;
curve &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; E29&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# One of the known rational points from the discovery&lt;/span&gt;
x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2891195474228537189458255536634&lt;/span&gt;
y_coords &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; curve&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_y_coordinates&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Verify the point lies on the curve&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y_coords&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Found point: (&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;y_coords&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Verifying point lies on curve: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;curve&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_on_curve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_coords&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So why does this matter? This discovery is significant because no one knows if there&#39;s an upper limit to how large the rank of an elliptic curve can be. Finding such a limit (or proving there isn&#39;t one) remains one of the major open problems in mathematics. Each new record, like this rank-29 curve, provides some new insights into whether an upper bound exists.&lt;/p&gt;
&lt;p&gt;Proving the existence of an upper bound for the rank of elliptic curves would be a monumental achievement worthy of a Fields Medal for several reasons. It would resolve a fundamental result related to the Birch and Swinnerton-Dyer conjecture, one of the seven Millennium Prize Problems. It would also provide deep insights into the mysterious relationship between the algebraic and analytic properties of L-functions, a connection that lies at the core of modern number theory. The techniques required to prove such a bound would likely revolutionize our understanding of arithmetic geometry and potentially unlock new approaches to other major open problems in number theory.&lt;/p&gt;
</description>
      <pubDate>Thu, 14 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/rank_29_elliptic_curve/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/rank_29_elliptic_curve/</guid>
    </item>
    
    <item>
      <title>Tiny GraphRAG (Part 2)</title>
      <description>&lt;h1 id=&quot;tiny-graphrag-(part-2)&quot; tabindex=&quot;-1&quot;&gt;Tiny GraphRAG (Part 2)&lt;/h1&gt;
&lt;p&gt;In &lt;a href=&quot;https://www.stephendiehl.com/posts/graphrag1&quot;&gt;Part 1&lt;/a&gt;, we built a minimal implementation of GraphRAG that demonstrated the core concepts. Now we&#39;ll extend our implementation with three significant improvements that make it more suitable for production use:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Replacing serialized networkx graphs with Memgraph, a proper graph database.&lt;/li&gt;
&lt;li&gt;Adding hierarchical community detection to better capture the natural structure of documents.&lt;/li&gt;
&lt;li&gt;Implementing entity disambiguation to resolve different mentions of the same entity to a canonical form.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The complete implementation of these features is available in the repository in the &lt;code&gt;micro-graphrag&lt;/code&gt; branch. The expanded implementation comes in at around 1500 lines of code with the new additions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/tiny-graphrag/tree/micro-graphrag&quot;&gt;https://github.com/sdiehl/tiny-graphrag/tree/micro-graphrag&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;memgraph-configuration&quot; tabindex=&quot;-1&quot;&gt;Memgraph Configuration&lt;/h2&gt;
&lt;p&gt;To use Memgraph as our graph database, we&#39;ll add the necessary services to our &lt;code&gt;docker-compose.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-yaml&quot;&gt;services:
  memgraph:
    image: memgraph/memgraph-mage:1.19-memgraph-2.19
    container_name: memgraph
    ports:
      - &amp;quot;7687:7687&amp;quot; # Bolt protocol port
      - &amp;quot;7444:7444&amp;quot; # HTTP API port
    command: [&amp;quot;--log-level=TRACE&amp;quot;, &amp;quot;--query-execution-timeout-sec=0&amp;quot;]
    volumes:
      - mg_lib:/var/lib/memgraph

  lab:
    image: memgraph/lab:latest
    container_name: memgraph-ui
    ports:
      - &amp;quot;3000:3000&amp;quot;
    depends_on:
      - memgraph
    environment:
      - QUICK_CONNECT_MG_HOST=memgraph
      - QUICK_CONNECT_MG_PORT=7687
      - QUICK_CONNECT_MG_USER=admin
      - QUICK_CONNECT_MG_PASSWORD=admin

volumes:
  mg_lib:
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This configuration:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Sets up the Memgraph database with MAGE extensions for advanced graph algorithms&lt;/li&gt;
&lt;li&gt;Provides a web-based UI (Memgraph Lab) for visualizing and querying the graph&lt;/li&gt;
&lt;li&gt;Configures persistent storage through a Docker volume&lt;/li&gt;
&lt;li&gt;Exposes necessary ports for both the database (7687) and UI (3000)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The connection details are configured in our &lt;code&gt;GraphConfig&lt;/code&gt; class:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;GraphConfig&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Memgraph connection configuration.&quot;&quot;&quot;&lt;/span&gt;
    uri&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;bolt://localhost:7687&quot;&lt;/span&gt;
    username&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;admin&quot;&lt;/span&gt;
    password&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;admin&quot;&lt;/span&gt;
    database&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;memgraph&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This setup complements our existing PostgreSQL/pgvector service, providing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL with pgvector for vector embeddings and search&lt;/li&gt;
&lt;li&gt;Memgraph for efficient graph storage and querying&lt;/li&gt;
&lt;li&gt;Web UI for graph visualization and exploration&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The combination allows us to leverage both vector operations and graph operations effectively in our GraphRAG implementation.&lt;/p&gt;
&lt;h2 id=&quot;build-pipeline-changes-(build.py)&quot; tabindex=&quot;-1&quot;&gt;Build Pipeline Changes (&lt;code&gt;build.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The build pipeline has been updated with new dataclasses and methods to support our enhanced features. The core data structures have been expanded to better represent our document processing pipeline:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Graph node with type information.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Relation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Graph triple representing relationships.&quot;&quot;&quot;&lt;/span&gt;
    head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;
    relation_type&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;
    tail&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DocumentChunkData&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Document chunk with embedding.&quot;&quot;&quot;&lt;/span&gt;
    text&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;
    embedding&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Any

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ProcessedDocument&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Complete processed document data.&quot;&quot;&quot;&lt;/span&gt;
    chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;DocumentChunkData&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Entity&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Relation&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The main document processing pipeline has been significantly enhanced to support entity disambiguation and improved graph construction. The process now happens in two passes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First pass collects all entity mentions with their context&lt;/li&gt;
&lt;li&gt;Second pass builds the graph using resolved canonical entities&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&#39;s the core implementation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;process_document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    entity_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MIN_ENTITY_TYPES&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    relation_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEFAULT_RELS_LIST&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ProcessedDocument&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Process document with entity disambiguation.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# First pass: collect all entity mentions&lt;/span&gt;
    all_mentions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; page_chunks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;max_chunks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        extraction &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_rels&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ent &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            all_mentions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Resolve entities to canonical forms&lt;/span&gt;
    disambiguator &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EntityDisambiguator&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    resolved_entities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; disambiguator&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;resolve_entities&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;all_mentions&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Map mentions to canonical forms&lt;/span&gt;
    entity_map &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        m&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;all_mentions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; resolved_entities&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Second pass: create graph with resolved entities&lt;/span&gt;
    g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; page_chunks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;max_chunks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        extraction &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_rels&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; rel &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; entity_map &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                head &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                tail &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_edge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    source_chunk&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;chunk_text
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; g
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two-pass approach allows us to gather all possible entity mentions with their context before making disambiguation decisions, create a clean graph structure using only canonical entity forms, and maintain proper relationship mapping between disambiguated entities.&lt;/p&gt;
&lt;p&gt;The process integrates with our new Memgraph storage system through the &lt;code&gt;GraphStore&lt;/code&gt; class, which handles persistence and retrieval of the graph data. This updated pipeline enables better entity resolution through context-aware disambiguation, produces a cleaner graph structure with deduplicated entities, improves relationship accuracy through canonical entity forms, and provides efficient storage and retrieval through Memgraph integration.&lt;/p&gt;
&lt;p&gt;The build process now produces a more robust knowledge graph that better captures the semantic relationships in the document while handling entity variations and ambiguity. This enhanced graph structure directly improves both local and global search capabilities by providing more accurate entity relationships and better-organized community structures.&lt;/p&gt;
&lt;h2 id=&quot;memgraph-integration-(graph.py)&quot; tabindex=&quot;-1&quot;&gt;Memgraph Integration (&lt;code&gt;graph.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;Our original implementation used serialized networkx graphs stored as pickle files. While this worked for demonstration purposes, it&#39;s not suitable for production use. Let&#39;s replace it with Memgraph, a high-performance graph database optimized for machine learning workloads.&lt;/p&gt;
&lt;p&gt;We&#39;ll replace our pickle-based storage with a new &lt;code&gt;GraphStore&lt;/code&gt; class that handles all graph operations through Memgraph. This class provides a clean interface for storing and retrieving graph data, with methods for adding entities and relationships, querying the graph structure, and maintaining document-specific subgraphs. The Memgraph backend gives us significant performance improvements through its optimized graph traversal algorithms and built-in support for parallel queries. Here&#39;s the core implementation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;GraphStore&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Handles graph storage and retrieval using Memgraph.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; config&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; GraphConfig &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;driver &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; GraphDatabase&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;driver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;config &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; GraphConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;uri&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            auth&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;config &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; GraphConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;username&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;config &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; GraphConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;password&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;store_graph&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        doc_id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        source_chunk&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Store entities and relations in Memgraph.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;driver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;session&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Store entities with vector embeddings&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                query &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
                MERGE (n:Entity {
                    content: $content,
                    label: $label,
                    doc_id: $doc_id
                })
                &quot;&quot;&quot;&lt;/span&gt;
                session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; label&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;doc_id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; doc_id
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Store relations with source context&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rel_type&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                query &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
                MATCH (h:Entity {content: $head}),
                      (t:Entity {content: $tail})
                CREATE (h)-[:RELATES {
                    type: $rel_type,
                    doc_id: $doc_id,
                    source_chunk: $source_chunk
                }]-&gt;(t)
                &quot;&quot;&quot;&lt;/span&gt;
                session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;rel_type&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; rel_type&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;doc_id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; doc_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;source_chunk&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; source_chunk
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;hierarchical-communities-(communities.py)&quot; tabindex=&quot;-1&quot;&gt;Hierarchical Communities (&lt;code&gt;communities.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;Our original community detection implementation used a flat structure where each entity belonged to exactly one community. This can be limiting for large documents where topics have natural hierarchical relationships. Unlike our previous flat implementation where each entity belonged to exactly one community, this approach creates multiple levels of communities, with larger communities containing smaller subcommunities.&lt;/p&gt;
&lt;p&gt;For example, when analyzing the Obama Wikipedia article, we might get a hierarchical structure like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Level 0: Major Life Periods
&lt;ul&gt;
&lt;li&gt;Political Career Community&lt;/li&gt;
&lt;li&gt;Level 1: Presidential Terms
&lt;ul&gt;
&lt;li&gt;First Term Community&lt;/li&gt;
&lt;li&gt;Level 2: Key Events
&lt;ul&gt;
&lt;li&gt;Financial Crisis Response&lt;/li&gt;
&lt;li&gt;Healthcare Reform&lt;/li&gt;
&lt;li&gt;Bin Laden Operation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Second Term Community&lt;/li&gt;
&lt;li&gt;Level 2: Key Events
&lt;ul&gt;
&lt;li&gt;Climate Change Actions&lt;/li&gt;
&lt;li&gt;Iran Nuclear Deal&lt;/li&gt;
&lt;li&gt;Cuba Relations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Level 1: Pre-Presidential Career
&lt;ul&gt;
&lt;li&gt;Senate Career Community&lt;/li&gt;
&lt;li&gt;State Senate Community&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Personal Life Community&lt;/li&gt;
&lt;li&gt;Level 1: Education
&lt;ul&gt;
&lt;li&gt;Columbia University&lt;/li&gt;
&lt;li&gt;Harvard Law School&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Level 1: Early Life
&lt;ul&gt;
&lt;li&gt;Hawaii Background&lt;/li&gt;
&lt;li&gt;Chicago Community Work&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;HierarchicalCommunity&lt;/code&gt; class represents each community node in this tree. It stores:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The triples (entity relationships) contained in that community&lt;/li&gt;
&lt;li&gt;A list of subcommunities at the next level down&lt;/li&gt;
&lt;li&gt;An optional summary of the community&#39;s contents&lt;/li&gt;
&lt;li&gt;The level/depth in the hierarchy&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;build_hierarchical_communities&lt;/code&gt; function recursively builds this tree structure by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Running community detection on each subgraph&lt;/li&gt;
&lt;li&gt;Extracting the relationship triples for that community&lt;/li&gt;
&lt;li&gt;Recursively detecting subcommunities within each community&lt;/li&gt;
&lt;li&gt;Stopping when it hits the maximum depth or minimum community size&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This hierarchical approach allows for more nuanced analysis of document structure and relationships between entities at different scales. In the Obama example above, we can see how it naturally captures both broad life periods and specific events/accomplishments within his presidency.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HierarchicalCommunity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Represents a hierarchical community structure.&quot;&quot;&quot;&lt;/span&gt;
    triples&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    subcommunities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;HierarchicalCommunity&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    summary&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    level&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;build_hierarchical_communities&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_levels&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    min_size&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; CommunityResult&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Build hierarchical communities from a graph.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;build_level&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; level&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;HierarchicalCommunity&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; level &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; max_levels &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; min_size&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        communities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        coms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; algorithms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;leiden&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; com &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; coms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;communities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            community_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subgraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;com&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Extract triples for this community&lt;/span&gt;
            triples &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;source_chunk&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; community_subgraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;edges&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Recursively build subcommunities&lt;/span&gt;
            subcommunities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; build_level&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;community_subgraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; level &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            communities&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;HierarchicalCommunity&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                triples&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;triples&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                subcommunities&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;subcommunities&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                level&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;level
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; communities

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; CommunityResult&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        communities&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;build_level&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        node_community_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;build_community_map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This hierarchical approach provides several key benefits by enabling multi-scale analysis of document structure at different levels of granularity. Topics naturally organize themselves into meaningful hierarchies, such as &amp;quot;Politics&amp;quot; flowing down to &amp;quot;Presidential Campaign&amp;quot; and further to &amp;quot;Primary Elections&amp;quot;. The approach allows for flexible querying by matching queries to the most appropriate level of detail. Additionally, it enables improved summary generation that can capture both broad themes and specific details within the document structure.&lt;/p&gt;
&lt;h2 id=&quot;entity-disambiguation-(dis.py)&quot; tabindex=&quot;-1&quot;&gt;Entity Disambiguation (&lt;code&gt;dis.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;Our original implementation treated entities as exact string matches, which led to missed connections between different references to the same entity (e.g., &amp;quot;Barack Obama&amp;quot; vs. &amp;quot;President Obama&amp;quot;). Let&#39;s add entity disambiguation using vector similarity and contextual cues.&lt;/p&gt;
&lt;p&gt;Here&#39;s the new entity disambiguation system, which uses vector embeddings and contextual information to resolve different mentions of the same entity to a canonical form:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Each entity mention is embedded along with its surrounding context using sentence-transformers.&lt;/li&gt;
&lt;li&gt;A similarity matrix is computed between all mention embeddings&lt;/li&gt;
&lt;li&gt;Mentions are clustered based on similarity scores above a threshold (0.85)&lt;/li&gt;
&lt;li&gt;For each cluster, a canonical form is selected based on frequency and completeness&lt;/li&gt;
&lt;li&gt;Entity types are resolved by taking the most specific type that appears in the cluster&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This system handles cases like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Name variations (&amp;quot;Barack Obama&amp;quot; vs &amp;quot;President Obama&amp;quot;)&lt;/li&gt;
&lt;li&gt;Titles and honorifics (&amp;quot;Dr. Smith&amp;quot; vs &amp;quot;John Smith&amp;quot;)&lt;/li&gt;
&lt;li&gt;Abbreviations and acronyms (&amp;quot;United Nations&amp;quot; vs &amp;quot;UN&amp;quot;)&lt;/li&gt;
&lt;li&gt;Contextual references (&amp;quot;the president&amp;quot; when referring to a specific person)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The implementation below demonstrates the core disambiguation logic:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EntityDisambiguator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; embedding_model&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; SentenceTransformer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; embedding_model
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mention_cache&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_mention_embedding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mention&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Get embedding for entity mention with context.&quot;&quot;&quot;&lt;/span&gt;
        key &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;mention&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; key &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mention_cache&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Combine mention with surrounding context&lt;/span&gt;
            text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;mention&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; | &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mention_cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mention_cache&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;key&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;resolve_entities&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        mentions&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# (text, type, context)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# (canonical_form, type)&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Resolve entity mentions to canonical forms.&quot;&quot;&quot;&lt;/span&gt;
        resolved &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        clusters&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Compute similarity matrix&lt;/span&gt;
        embeddings &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_mention_embedding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; m &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; mentions
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        similarities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cosine_similarity&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;embeddings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Cluster similar mentions&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mentions&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            assigned &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; canonical&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cluster &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; clusters&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token comment&quot;&gt;# Check similarity with existing cluster&lt;/span&gt;
                mean_sim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;similarities&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; j &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; cluster&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; mean_sim &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.85&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Similarity threshold&lt;/span&gt;
                    cluster&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    assigned &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; assigned&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token comment&quot;&gt;# Create new cluster&lt;/span&gt;
                clusters&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;mentions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Select canonical forms and resolve types&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; canonical&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cluster &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; clusters&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Use most frequent entity type in cluster&lt;/span&gt;
            types &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;mentions&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; cluster&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            resolved&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;canonical&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; mode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; resolved
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The disambiguator is integrated into the document processing pipeline:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;process_document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    entity_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MIN_ENTITY_TYPES&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    relation_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEFAULT_RELS_LIST&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ProcessedDocument&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Process document with entity disambiguation.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# First pass: collect all entity mentions&lt;/span&gt;
    all_mentions &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; page_chunks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;max_chunks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        extraction &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_rels&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ent &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            all_mentions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Resolve entities to canonical forms&lt;/span&gt;
    disambiguator &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EntityDisambiguator&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    resolved_entities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; disambiguator&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;resolve_entities&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;all_mentions&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Map mentions to canonical forms&lt;/span&gt;
    entity_map &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        m&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;all_mentions&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; resolved_entities&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Second pass: create graph with resolved entities&lt;/span&gt;
    g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; page_chunks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;max_chunks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        extraction &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_rels&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; rel &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; entity_map &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                head &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                tail &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; entity_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_edge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    source_chunk&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;chunk_text
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; g
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot; tabindex=&quot;-1&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;These improvements significantly enhance the system&#39;s capabilities, but they do come with some notable computational costs. The use of Memgraph requires more memory compared to serialized graphs, though this tradeoff enables better scalability. The hierarchical community detection process is computationally more intensive than simple flat clustering approaches. Additionally, the vector similarity comparisons needed for entity resolution introduce extra processing overhead during document ingestion.&lt;/p&gt;
&lt;p&gt;The benefits provided by these improvements generally justify the additional resource usage. Query performance sees dramatic improvements, with Memgraph enabling graph queries that are 10 to 100 times faster than the previous approach. The quality of answers improves substantially through the combination of hierarchical community structure and more accurate entity disambiguation. Most importantly, the enhanced architecture allows the system to scale effectively to handle much larger documents and more complex knowledge graphs than was previously possible.&lt;/p&gt;
&lt;p&gt;While these improvements make our implementation more production-ready, it&#39;s worth remembering that like the original GraphRAG paper, there&#39;s still a fair bit of &amp;quot;graph goes in, magic comes out&amp;quot; happening under the hood. We&#39;ve added better persistence with Memgraph, smarter community detection, and more robust entity handling, but at its core we&#39;re still essentially throwing a bunch of language models at a graph and hoping they play nice together. Sometimes they do, sometimes they don&#39;t, and sometimes they produce surprisingly good results through what can only be described as &amp;quot;accidental competence.&amp;quot;&lt;/p&gt;
</description>
      <pubDate>Tue, 12 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/graphrag2/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/graphrag2/</guid>
    </item>
    
    <item>
      <title>Tiny GraphRAG (Part 1)</title>
      <description>&lt;h1 id=&quot;tiny-graphrag-(part-1)&quot; tabindex=&quot;-1&quot;&gt;Tiny GraphRAG (Part 1)&lt;/h1&gt;
&lt;p&gt;We&#39;re going to build a tiny 1000 line implementation of a GraphRAG algorithm originally invented by Microsoft. I consistently hear people talk about this algorithm at meetups, but it appears there are several orders of magnitude of people talking about it than actually using it or implementing it. Likely because the reference implementation is enormous and rather complex. So let&#39;s break it down and see if there&#39;s any merit to the hype around this approach.&lt;/p&gt;
&lt;p&gt;For some background, naive RAG is a basic approach to augmenting LLM outputs with external information that primarily relies on vector similarity search. The process involves converting documents to text, splitting that text into chunks, and embedding these chunks into a vector space where similar semantic meanings are represented by similar vector positions. When a user submits a query, it is embedded into the same vector space, and the text chunks with the nearest vector positions are retrieved and added to the LLM&#39;s context window along with the original query.&lt;/p&gt;
&lt;p&gt;GraphRAG is an alternative structured approach to goes beyond simple semantic search and uses a graph-based index of a document. The key idea is that it uses LLMs to build a knowledge graph from source documents, then detects communities of closely-related entities within that graph, and generates summaries for these communities. This creates a alternative structured index that can be leveraged for both local entity-specific queries and global document-wide analysis. For some tasks this can perform better than naive RAG.&lt;/p&gt;
&lt;p&gt;We&#39;re going to build a tiny version that runs entirely locally without depending on commercial LLM providers. Our system uses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/&quot;&gt;Postgres&lt;/a&gt; + &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;pgvector&lt;/a&gt; for vector storage&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sbert.net/&quot;&gt;sentence-transformers&lt;/a&gt; for embeddings&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct&quot;&gt;Meta&#39;s Llama 3.2-3B&lt;/a&gt; for core langauge model&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/urchade/GLiNER&quot;&gt;GLiNER&lt;/a&gt; for entity extraction&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/jackboyla/GLiREL&quot;&gt;GLiREL&lt;/a&gt; for relation extraction&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://networkx.org/&quot;&gt;networkx&lt;/a&gt; for graph operations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We could also trivially use a graph database (like &lt;a href=&quot;https://memgraph.com/&quot;&gt;Memgraph&lt;/a&gt; or &lt;a href=&quot;https://neo4j.com/&quot;&gt;Neo4j&lt;/a&gt;) but for the sake of simplicity we&#39;ll just store the networkx graph data serialized to disk.&lt;/p&gt;
&lt;p&gt;The source code is available on Github under a MIT license.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/tiny-graphrag&quot;&gt;https://github.com/sdiehl/tiny-graphrag&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;core-ideas&quot; tabindex=&quot;-1&quot;&gt;Core Ideas&lt;/h2&gt;
&lt;p&gt;When doing naive RAG, we typically rely on vector similarity to find relevant context. This works well for simple cases but can miss important contextual relationships. For example, let&#39;s look at a basic vector similarity comparison using the &lt;code&gt;sentence-transformers&lt;/code&gt; library.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; sentence_transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SentenceTransformer

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SentenceTransformer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;all-MiniLM-L6-v2&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

sentence1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;I love programming&quot;&lt;/span&gt;
sentence2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;I enjoy writing code&quot;&lt;/span&gt;

embedding1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sentence1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
embedding2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sentence2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

similarity &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;similarity&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;embedding1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; embedding2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Similarity score: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;similarity&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.4f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While this approach can identify semantically similar content, it has several key limitations. First, it doesn&#39;t capture the rich relationships between entities and concepts that exist in our documents. Second, it struggles with what is commonly called the &amp;quot;local context problem&amp;quot; - where relevant information may be scattered across different parts of a document or multiple documents, without having direct semantic similarity to the query. For example, understanding the side effects of a medication might require connecting information about its chemical composition in one section with patient outcomes described in another, even though these passages may not be semantically similar to a query about side effects. GraphRAG addresses these limitations by building a structured knowledge graph that explicitly represents relationships between entities and concepts, allowing it to trace these connections and assemble relevant context even when the individual pieces aren&#39;t semantically similar to the query. GraphRAG has two different modes, &lt;em&gt;local search&lt;/em&gt; and &lt;em&gt;global search&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Local search is optimized for entity-specific queries and operates by focusing on specific entities and their relationships within the knowledge graph. It combines structured data from the knowledge graph with unstructured data from input documents, making it particularly effective for questions about specific entities, such as &amp;quot;What are the healing properties of chamomile?&amp;quot; The process begins by taking a user query and optional conversation history, then identifies semantically-related entities in the knowledge graph. It extracts relevant information including connected entities, relationships, entity covariates, community reports, and associated text chunks from source documents. This information is then prioritized and filtered to fit within a context window before generating a response using the filtered context.&lt;/p&gt;
&lt;p&gt;Global search, on the other hand, is designed for dataset-wide analysis and performs better at handling queries that require information aggregation across the entire dataset. It&#39;s better at identifying broader themes and patterns, making it optimal for answering high-level questions like &amp;quot;What are the top 5 themes in the data?&amp;quot; The process employs a map-reduce approach where the map phase segments community reports into text chunks and generates intermediate responses with rated importance points. The reduce phase then filters and aggregates the most important points to produce a final comprehensive response.&lt;/p&gt;
&lt;p&gt;The key difference between these approaches is that while local search focuses on specific entity-based reasoning, global search provides broader dataset-level insights by leveraging the knowledge graph&#39;s community structure and pre-summarized semantic clusters.&lt;/p&gt;
&lt;h2 id=&quot;building-the-graph&quot; tabindex=&quot;-1&quot;&gt;Building the Graph&lt;/h2&gt;
&lt;p&gt;GraphRAG takes a new approach to RAG by building a graph-based index of document content. There are four main steps.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Graph Construction&lt;/strong&gt;: We first process source documents to extract entities and relationships, building a knowledge graph representation of the content. This is done using:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Text chunking and preprocessing&lt;/li&gt;
&lt;li&gt;Entity and relationship extraction using LLMs&lt;/li&gt;
&lt;li&gt;Graph construction with extracted elements&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Community Detection&lt;/strong&gt;: The system then uses the Leiden algorithm to detect communities of closely-related entities in the graph.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Community Summary&lt;/strong&gt;: For each detected community, we generate a natural language summary describing the key entities and relationships within that community. This provides a high-level overview of the topics and concepts represented in each cluster.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Map-Reduce&lt;/strong&gt;: For global search queries, we use a map-reduce approach where we first generate responses for each community summary (map phase) and then combine and synthesize these responses (reduce phase) to create a comprehensive answer that draws from information across the entire graph.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;example-dataset&quot; tabindex=&quot;-1&quot;&gt;Example Dataset&lt;/h2&gt;
&lt;p&gt;In our example we&#39;ll use a set of Wikipedia articles about Barack Obama to demonstrate how the system automatically detects topical clusters. The Leiden community detection algorithm will analyze the entity-relationship graph and identify densely connected subgraphs that represent coherent topics. For example, when processing Obama&#39;s biography, it will automatically detect clusters like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Early Life &amp;amp; Education: Entities about his childhood in Hawaii, education at Columbia and Harvard Law&lt;/li&gt;
&lt;li&gt;Political Career: His time as state senator, U.S. senator, and presidential campaigns&lt;/li&gt;
&lt;li&gt;Presidency: Key policies, executive actions, and administration officials&lt;/li&gt;
&lt;li&gt;Post-Presidency: Book deals, speaking engagements, Obama Foundation work&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each cluster is then summarized by analyzing the entities and relationships within it. For instance, the Early Life cluster summary might read: &amp;quot;This section covers Barack Obama&#39;s upbringing in Hawaii and Indonesia, his undergraduate studies in political science at Columbia University, and his law degree from Harvard where he was president of the Harvard Law Review.&amp;quot;&lt;/p&gt;
&lt;p&gt;The system determines cluster boundaries automatically based on the density of connections between entities, without requiring manual topic specification. This organic clustering helps capture the natural thematic structure of the documents across non-local chunks.&lt;/p&gt;
&lt;h2 id=&quot;data-models-(db.py)&quot; tabindex=&quot;-1&quot;&gt;Data Models (&lt;code&gt;db.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;We&#39;ll use the SQLAlchemy data models to define persistence to Postgres. The &lt;strong&gt;Document&lt;/strong&gt; model stores the original document content and metadata. The content is stored as raw text, with optional title and timestamps.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Base&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Represents a document in the database.&quot;&quot;&quot;&lt;/span&gt;
    __tablename__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;documents&quot;&lt;/span&gt;

    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; primary_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    content &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;String&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;256&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    created_at &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;DateTime&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;timezone&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; server_default&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;func&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;now&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    updated_at &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;DateTime&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;timezone&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; onupdate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;func&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;now&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Relationships&lt;/span&gt;
    chunks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; relationship&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;DocumentChunk&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; back_populates&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;document&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    communities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; relationship&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Community&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; back_populates&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;document&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;DocumentChunk&lt;/strong&gt; model stores segments of documents with their vector embeddings for efficient semantic search. Each chunk contains the raw text content, its position in the original document (chunk_index), and a dense vector embedding. The model uses HNSW (Hierarchical Navigable Small World) indexing for fast approximate nearest neighbor search on embeddings, and a GIN index for full-text search on content. This enables both semantic similarity search via embeddings and keyword-based search on the text.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DocumentChunk&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Base&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Represents a chunk of a document with vector embedding.&quot;&quot;&quot;&lt;/span&gt;
    __tablename__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;document_chunks&quot;&lt;/span&gt;

    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; primary_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    document_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ForeignKey&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;documents.id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    content &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    chunk_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    embedding &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mapped_column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Vector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;384&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Vector embedding&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Indexes for efficient search&lt;/span&gt;
    __table_args__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        Index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;ix_document_chunks_embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            embedding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_using&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hnsw&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_with&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;m&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;ef_construction&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_ops&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;vector_cosine_ops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        Index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;ix_document_chunks_content_fts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Full-text search index&lt;/span&gt;
            text&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;to_tsvector(&#39;english&#39;, content)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_using&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;Community&lt;/strong&gt; model represents clusters of related entities detected in the document. Each community has a summary description (content), a vector embedding for similarity search, and stores its member node IDs as a JSON string. Like DocumentChunk, it uses HNSW indexing on the embedding for efficient similarity search. Communities are linked back to their source document and enable exploration of thematically related entity groups.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Community&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Base&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Represents a community of related entities.&quot;&quot;&quot;&lt;/span&gt;
    __tablename__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;communities&quot;&lt;/span&gt;

    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; primary_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    document_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ForeignKey&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;documents.id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    content &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Community summary&lt;/span&gt;
    embedding &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mapped_column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Vector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;384&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    nodes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# JSON string of node IDs&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Indexes&lt;/span&gt;
    __table_args__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        Index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;ix_communities_embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            embedding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_using&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hnsw&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_ops&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;vector_cosine_ops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;strong&gt;Summary&lt;/strong&gt; model stores generated summaries of a community. Each summary has a text content field and a vector embedding for similarity search. Like the other models, it uses HNSW indexing on the embedding for efficient similarity search, as well as a full-text search index on the content.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Summary&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Base&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Represents a document summary.&quot;&quot;&quot;&lt;/span&gt;
    __tablename__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;summaries&quot;&lt;/span&gt;

    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; primary_key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    document_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Integer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ForeignKey&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;documents.id&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    content &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    embedding &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mapped_column&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Vector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;384&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nullable&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Indexes&lt;/span&gt;
    __table_args__ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        Index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;ix_summaries_embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            embedding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_using&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hnsw&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_ops&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;embedding&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;vector_cosine_ops&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        Index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;ix_summaries_content_fts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            text&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;to_tsvector(&#39;english&#39;, content)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            postgresql_using&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gin&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;document-processing-pipeline-(build.py)&quot; tabindex=&quot;-1&quot;&gt;Document Processing Pipeline (&lt;code&gt;build.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The top of of graph processing pipeline beings in &lt;code&gt;build.py&lt;/code&gt; which handles the construction of the vector index and knowledge graph from our textual source. This calls out to several core functions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;process_document&lt;/code&gt; - Takes a document filepath and processes it by chunking the text, extracting entities and relations, and building a knowledge graph. Returns both the chunked document with embeddings and the constructed graph.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;chunk_document&lt;/code&gt; - Splits a document into overlapping text segments of configurable size with some overlap between chunks to maintain context across boundaries. Returns a list of text chunks.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;build_communities&lt;/code&gt; - Analyzes the knowledge graph to detect clusters of closely related entities using community detection algorithms. Generates summaries for each community to capture the key information. Returns community assignments and summaries.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;extract_rels&lt;/code&gt; - Processes text chunks to identify relationships between entities using a combination of rule-based patterns and LLM extraction. Returns a list of extracted relationships with their entity pairs and relationship types.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;store_document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    engine&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Store document in database and save graph.&quot;&quot;&quot;&lt;/span&gt;
    session_local &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionmaker&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bind&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; session_local&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; session&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Process document&lt;/span&gt;
        chunks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; process_document&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;filepath&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_chunks&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Store document&lt;/span&gt;
        doc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Document&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;content&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;filepath&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;doc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flush&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Store chunks with embeddings&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; embedding &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            chunk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DocumentChunk&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                document_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;doc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                content&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                embedding&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;embedding&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                chunk_index&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Build and store communities&lt;/span&gt;
        community_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; build_communities&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Save graph to disk&lt;/span&gt;
        graph_path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;graphs/&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;doc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;_graph.pkl&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;graph_path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;wb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            pickle&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dump&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;commit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; doc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph_path
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;process_document&lt;/code&gt; function handles the construction of hte knowledge graph entities (nodes and relations) from the chunk. These are appended into a global graph which is a networkx undirected Graph data structure. We store the source text provenance as a proeprty of each triple that is generated which is used in the local search method at the end.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;process_document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    entity_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MIN_ENTITY_TYPES&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    relation_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEFAULT_RELS_LIST&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Any&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Process a document and return chunks and graph.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Read and chunk document&lt;/span&gt;
    page_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;filepath&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    page_chunks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; chunk_document&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;page_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Build graph&lt;/span&gt;
    g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _embedding &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tqdm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;page_chunks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;max_chunks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        extraction &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extract_rels&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ent &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ent&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; rel &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; extraction&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relations&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_edge&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rel&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; source_chunk&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; page_chunks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; g
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;chunking-(chunking.py)&quot; tabindex=&quot;-1&quot;&gt;Chunking (&lt;code&gt;chunking.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The chunking algorithm splits input documents into overlapping segments while preserving sentence boundaries and semantic coherence. It processes the text sentence by sentence, accumulating them into chunks until reaching a specified size limit (default 200 characters), then generates dense vector embeddings for each chunk using a sentence transformer model. To maintain context continuity between chunks, it implements an overlap mechanism (default 50 characters) where a portion of the previous chunk&#39;s sentences are carried forward into the next chunk. This overlap helps capture cross-chunk relationships and ensures no context is lost at chunk boundaries. The algorithm returns a list of tuples containing both the text chunks and their corresponding embeddings, which are used later for similarity search and knowledge graph construction.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;chunk_document&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    text&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    chunk_size&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    overlap&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;50&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; npt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NDArray&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Split document into overlapping chunks with embeddings.&quot;&quot;&quot;&lt;/span&gt;
    sentences &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; text&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;strip&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    chunks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    current_chunk&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    current_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; sentence &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; sentences&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        sentence_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sentence&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; current_length &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; sentence_length &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; chunk_size &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; current_chunk&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Create chunk and get embedding&lt;/span&gt;
            chunk_text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;. &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_chunk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;.&quot;&lt;/span&gt;
            embedding &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; convert_to_numpy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            chunks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;chunk_text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; embedding&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token comment&quot;&gt;# Handle overlap&lt;/span&gt;
            overlap_chunk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            overlap_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_chunk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; overlap_size &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; overlap&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    overlap_chunk&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;insert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    overlap_size &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            current_chunk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; overlap_chunk
            current_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; overlap_size

        current_chunk&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sentence&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        current_length &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; sentence_length

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; chunks
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;entity-and-relation-extraction-(extract.py)&quot; tabindex=&quot;-1&quot;&gt;Entity and Relation Extraction (&lt;code&gt;extract.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The extraction pipeline integrates GLiNER and GLiREL models into a spaCy workflow for entity and relation extraction. GLiNER handles named entity recognition while GLiREL extracts relationships between those entities. We configure the pipeline to use GPU acceleration for better performance and allow customization of both entity types (e.g. &lt;code&gt;Person&lt;/code&gt;, &lt;code&gt;Organization&lt;/code&gt;) and relation types (e.g. &lt;code&gt;occupation&lt;/code&gt;, &lt;code&gt;place of birth&lt;/code&gt; ) through parameters. The core extraction logic is implemented in two key functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;nlp_model&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;threshold&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; device&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEVICE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Instantiate a spacy model with GLiNER and GLiREL components.&quot;&quot;&quot;&lt;/span&gt;
    custom_spacy_config &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;gliner_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;urchade/gliner_mediumv2.1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;chunk_size&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;style&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;ent&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;threshold&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; threshold&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;map_location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; device&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    spacy&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;require_gpu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# type: ignore&lt;/span&gt;

    nlp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; spacy&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;en&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    nlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_pipe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gliner_spacy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; config&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;custom_spacy_config&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    nlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_pipe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;glirel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; after&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gliner_spacy&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; nlp
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the &lt;code&gt;extract_rels&lt;/code&gt; method which takes text input along with entity and relation type configurations, processes it through the NLP pipeline, and returns structured extraction results containing both the identified entities and their relationships. The method applies a confidence threshold to filter out low-quality relation matches, ensuring only high-confidence relationships are included in the final output.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;extract_rels&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    text&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    entity_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    relation_types&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    threshold&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.75&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ExtractionResult&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Extract entities and relations from text.&quot;&quot;&quot;&lt;/span&gt;
    nlp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nlp_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;threshold&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;entity_types&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    docs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pipe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;glirel_labels&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; relation_types&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    relations &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; docs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;relations

    &lt;span class=&quot;token comment&quot;&gt;# Extract entities with their types&lt;/span&gt;
    ents &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;text&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;label_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ent &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; docs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ents&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Extract relations above threshold&lt;/span&gt;
    rels &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;head_text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; item&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tail_text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; item &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; relations
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; item&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;score&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; threshold
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; ExtractionResult&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;entities&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;ents&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relations&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;rels&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;GLiNER is an transformer-based Named Entity Recognition model that can identify enitites in text. Unlike traditional NER (like in spaCy) models that are limited to predefined entity types, or LLM-based models that require significant computing resources, GLiNER offers a more resource-efficient solution while maintaining high performance. It uses a bidirectional transformer encoder to process text and entity types in parallel, treating NER as a matching problem between entity type embeddings and textual span representations in latent space. Despite being much smaller (only 0.3B parameters), it outperforms both ChatGPT and fine-tuned LLMs in zero-shot evaluations on various NER benchmarks.&lt;/p&gt;
&lt;p&gt;A set of minimal default entity annotations are provided but can be optionally overloaded.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;MIN_ENTITY_TYPES &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Person&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Organization&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Product&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Concept&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;named-relation-recognition-(rel_types.py)&quot; tabindex=&quot;-1&quot;&gt;Named Relation Recognition (&lt;code&gt;rel_types.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;GLiREL (Generalist and Lightweight model for Relation Extraction) is a zero-shot relation extraction model that can classify relationships between entities in text without requiring pre-training on specific relation types. It works alongside GLiNER in the spaCy pipeline to identify semantic relationships between detected entities, providing confidence scores that allow for threshold-based filtering. The model is particularly efficient as it can handle new relation types on the fly while maintaining a lightweight architecture.&lt;/p&gt;
&lt;p&gt;The relation recognition system defines semantic relationships between entities using a either free form relations with no constraints. Like the following&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;DEFAULT_RELS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;created&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;author&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;place of birth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;occupation&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or we can specify type constraints which use the NER labels to constrain the types of relations that can be created.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;DEFAULT_RELS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;created&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Person&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Organization&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;author&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Person&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Document&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;place of birth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Person&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Location&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;occupation&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Person&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Concept&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;community-segmentation-(communities.py)&quot; tabindex=&quot;-1&quot;&gt;Community Segmentation (&lt;code&gt;communities.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Leiden_algorithm&quot;&gt;Leiden algorithm&lt;/a&gt; is a graph community detection method that improves upon the earlier Louvain algorithm for finding communities in large-scale networks. It&#39;s particularly notable for its ability to guarantee well-connected communities, which makes it ideal for the GraphRAG approach to document analysis. The algorithm works by partitioning a graph into modular communities where nodes within each community have stronger connections to each other than to nodes in other communities.&lt;/p&gt;
&lt;p&gt;We use the Leiden algorithm in our system to detect communities within the knowledge graph constructed from document entities and relationships. The implementation uses the &lt;code&gt;networkx&lt;/code&gt; and &lt;code&gt;cdlib&lt;/code&gt; library for graph operations and stores the community structure alongside the original text chunks which generated the triple from the graph.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;build_communities&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; CommunityResult&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Build communities from a graph.&quot;&quot;&quot;&lt;/span&gt;
    communities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    node_community_map &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert to integers for algorithm&lt;/span&gt;
    gi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert_node_labels_to_integers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label_attribute&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;original_label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create reverse mapping&lt;/span&gt;
    reverse_mapping &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;original_label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; gi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nodes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; component &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;connected_components&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;gi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; gi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;component&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Detect communities using Leiden&lt;/span&gt;
            coms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; algorithms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;leiden&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; com_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; com &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;coms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;communities&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token comment&quot;&gt;# Extract community relationships&lt;/span&gt;
                community_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; coms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subgraph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;com&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                community_triples &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; data &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; community_subgraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;edges&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    triple &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        reverse_mapping&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        reverse_mapping&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                        data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;source_chunk&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    community_triples&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;triple&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                communities&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;community_triples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

                &lt;span class=&quot;token comment&quot;&gt;# Map nodes to communities&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; com&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    node_community_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;reverse_mapping&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; com_id

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; CommunityResult&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        communities&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;communities&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        node_community_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;node_community_map
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we have extracted communities from our knowledge graph, we need to generate natural language summaries that capture the key information and relationships contained in each community&#39;s triples. To do this, we pass the triples to a language model which synthesizes them into coherent text that describes the main concepts and connections present in that portion of the graph. This summary text provides a semantic representation of the community that we can later use for retrieval.&lt;/p&gt;
&lt;p&gt;Since communities can contain many triples and language models have limited context windows, we need to handle large communities carefully. For communities with more triples than can fit in the context window (default 30 triples), we first split them into smaller chunks and generate intermediate summaries. These chunk summaries are then concatenated and passed through the model again to produce a final unified summary that captures the key information across all chunks. This two-pass approach allows us to summarize communities of any size while working within model constraints.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;generate_community_summary&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    llm&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Llama&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    community&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_triples&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    temperature&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Chunk the community into smaller pieces if too large&lt;/span&gt;
    chunks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        community&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; max_triples&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;community&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_triples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    summaries&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; chunk &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; chunks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_chat_completion&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; COMMUNITY_SUMMARY&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;community&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;chunk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;temperature&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        summaries&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;choices&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# If we had multiple chunks, combine them&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;summaries&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        combined &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;summaries&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Generate a final summary of the combined text&lt;/span&gt;
        response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; llm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_chat_completion&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                    &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; COMMUNITY_COMBINE&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;combined&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;combined&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;temperature&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;choices&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; summaries&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we run this on a subset of our Obama dataset we get something like the following graph with five major communities. Related to topics like the Supreme Court appointeess, Iraq War and the Democratic party that are discovered automatically from the graph structure.&lt;/p&gt;
&lt;div style=&quot;text-align: center; max-width: 800px; margin: 0 auto;&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/23_communities.png&quot; alt=&quot;Graph showing communities detected in Obama dataset&quot; width=&quot;800&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;And now we&#39;re done with the construction phase of the pipeline. Now onto the search methods.&lt;/p&gt;
&lt;h2 id=&quot;query-(query.py)&quot; tabindex=&quot;-1&quot;&gt;Query (&lt;code&gt;query.py&lt;/code&gt;)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;QueryEngine&lt;/code&gt; class is the core component responsible for handling different types of searches. During initialization, it sets up the Llama language model for generating responses and creates a database session factory using SQLAlchemy. The engine parameter connects to the PostgreSQL database with pgvector for our vector searches.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;QueryEngine&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; engine&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;llm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Llama&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            repo_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;MODEL_REPO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            filename&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;MODEL_ID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            local_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            verbose&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            n_ctx&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;DEFAULT_CTX_LENGTH&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SessionLocal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sessionmaker&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bind&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; engine
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The query engine uses &lt;code&gt;llama-cpp-python&lt;/code&gt; to interface with Meta&#39;s Llama 3.2-3B model for generating responses. The model is loaded from a local GGUF file that can be downloaded from Hugging Face. During initialization, the model is loaded with a specific context length and configuration. The core interaction happens through the &lt;code&gt;_generate_llm_response&lt;/code&gt; method which provides a local interface for chat completion requests. It takes a system prompt, user content, and temperature parameter, then returns the generated response as a string.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_generate_llm_response&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    system_prompt&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    user_content&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    temp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; DEFAULT_TEMPERATURE
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Common method for generating LLM responses.&quot;&quot;&quot;&lt;/span&gt;
    response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;llm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;create_chat_completion&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        messages&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; system_prompt&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; user_content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        temperature&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;temp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; response &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;choices&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; RuntimeError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Failed to get valid response from LLM&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;choices&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;local-search&quot; tabindex=&quot;-1&quot;&gt;Local Search&lt;/h2&gt;
&lt;p&gt;The local search method implements entity-centric search using the knowledge graph. It first loads the serialized graph, extracts entities from the query, gathers relevant data (entities, relationships, and text chunks) from the graph, builds a context string, and generates a response using the LLM. This approach is particularly effective for queries about specific entities and their relationships.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;local_search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph_path&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;load_graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;graph_path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    query_entities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_extract_query_entities&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    relevant_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_gather_relevant_data&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query_entities&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_build_context&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;relevant_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_generate_response&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; context&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This helper method gathers relevant information from the knowledge graph for local search. It takes query entities and the graph as input, then collects matching nodes, their relationships, and associated text chunks. While our implementation uses simple case-insensitive substring matching to find relevant entities, a production system would likely employ more sophisticated entity matching techniques to improve accuracy and coverage. The method builds a comprehensive context for the query based on the matched entities.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_gather_relevant_data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; nx&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query_entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; RelevantData&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    relevant_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; RelevantData&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; query_entity &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; query_entities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nodes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; query_entity&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lower&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lower&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_process_matching_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; relevant_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; relevant_data
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The local search context is structured to provide the LLM with a comprehensive view of the relevant information extracted from the knowledge graph. It takes the entities matched from the query, their relationships found in the graph, and supporting text chunks, and formats them into a clear template. The entities section lists all matched nodes from the graph that were relevant to the query. The relationships section details how these entities are connected to each other through the graph edges. Finally, the supporting text section includes the actual document text chunks associated with these entities and relationships. This structured context helps the LLM understand both the semantic relationships between entities as well as their textual descriptions, allowing it to generate more accurate and contextually appropriate responses.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;LOCAL_SEARCH_CONTEXT &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
Relevant Entities: {entities}

Relationships:
{relationships}

Supporting Text:
{text_chunks}
&quot;&quot;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;global-search&quot; tabindex=&quot;-1&quot;&gt;Global Search&lt;/h2&gt;
&lt;p&gt;Global search uses a map-reduce approach to analyze document communities. It first retrieves all community summaries for a document, then in the map phase, it generates intermediate answers for each community. In the reduce phase, it combines these answers into a coherent response.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;global_search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; doc_id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; limit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    session &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SessionLocal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        communities &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Community&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Community&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;document_id &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; doc_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        intermediate_answers &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Performing map phase over communities.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; community &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tqdm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;communities&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            answer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_generate_llm_response&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                GLOBAL_SEARCH_COMMUNITY&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Community Summary:&#92;n&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;community&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#92;n&#92;nQuery: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                temp&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; answer &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No relevant information found&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; answer&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                intermediate_answers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;answer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Performing reduce phase over community answers.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_generate_llm_response&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            GLOBAL_SEARCH_COMBINE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Query: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#92;n&#92;nAnswers to combine:&#92;n&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;intermediate_answers&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            temp&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        session&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;close&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;naive-rag&quot; tabindex=&quot;-1&quot;&gt;Naive RAG&lt;/h2&gt;
&lt;p&gt;The naive search method implements a traditional RAG approach using hybrid search (combining vector similarity and keyword matching). It retrieves relevant text chunks using the hybrid search function, combines them into a context string, and generates a response using the LLM. This method is simpler than the graph-based approaches but can be effective for straightforward queries.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;naive_search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; limit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tiny_graphrag&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;search &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; hybrid_search
    search_results &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hybrid_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; limit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;limit&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; engine&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    context &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&#92;n&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; result &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; search_results&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_generate_llm_response&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        NAIVE_SEARCH_RESPONSE&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Context:&#92;n&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;context&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#92;n&#92;nQuery: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;query&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The hybrid search implementation combines two different search strategies: semantic search using vector embeddings and traditional keyword-based search. This is implemented in a single SQL query that leverages PostgreSQL&#39;s pgvector extension for vector similarity and full-text search capabilities. Let&#39;s look at the core query from the &lt;code&gt;search.py&lt;/code&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;WITH&lt;/span&gt; semantic_search &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; document_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
           RANK &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;OVER&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;BY&lt;/span&gt; embedding &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:embedding&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;::vector&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; rank
    &lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt; document_chunks
    &lt;span class=&quot;token keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;BY&lt;/span&gt; embedding &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:embedding&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;::vector
    &lt;span class=&quot;token keyword&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
keyword_search &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; document_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
           RANK &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;OVER&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;BY&lt;/span&gt; ts_rank_cd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;to_tsvector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;english&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;DESC&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt; document_chunks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; plainto_tsquery&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;english&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; :query&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; query
    &lt;span class=&quot;token keyword&quot;&gt;WHERE&lt;/span&gt; to_tsvector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;english&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; @@ query
    &lt;span class=&quot;token keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;BY&lt;/span&gt; ts_rank_cd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;to_tsvector&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;english&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; query&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;DESC&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;semantic_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;document_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keyword_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;document_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; document_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;semantic_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keyword_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; content&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:k &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; semantic_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rank&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:k &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; keyword_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rank&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; score
&lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt; semantic_search
&lt;span class=&quot;token keyword&quot;&gt;FULL&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;OUTER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;JOIN&lt;/span&gt; keyword_search &lt;span class=&quot;token keyword&quot;&gt;ON&lt;/span&gt; semantic_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; keyword_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id
&lt;span class=&quot;token keyword&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;BY&lt;/span&gt; score &lt;span class=&quot;token keyword&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;LIMIT&lt;/span&gt; :&lt;span class=&quot;token keyword&quot;&gt;limit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The query uses Common Table Expressions (CTEs) to perform two parallel searches. The first CTE, semantic_search, uses pgvector&#39;s cosine similarity operator &amp;lt;=&amp;gt; to find documents whose embeddings are closest to the query embedding. It ranks results based on vector similarity and selects the top 20 matches. The second CTE, keyword_search, uses PostgreSQL&#39;s full-text search capabilities with ts_rank_cd to find and rank documents based on keyword matches, also selecting the top 20 results.&lt;/p&gt;
&lt;p&gt;The main SELECT statement then combines these results using a FULL OUTER JOIN, which ensures we capture matches from both search methods even if a document only appears in one of them. This is where the Reciprocal Rank Fusion comes into play. The formula is implemented in the scoring formula:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:k &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; semantic_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rank&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;COALESCE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;:k &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; keyword_search&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rank&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;AS&lt;/span&gt; score
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The RRF algorithm combines multiple ranked lists by giving each item a score based on its position in each list. The constant k (default 60) acts as a smoothing factor that mitigates the impact of high rankings in individual result lists. For each document, its final score is the sum of the reciprocal of (k + rank) from each source. The COALESCE function handles cases where a document appears in only one of the result sets, defaulting to 0 for missing rankings.&lt;/p&gt;
&lt;p&gt;This fusion approach is an improvement because it compoensates for the strengths and weaknesses of both search methods. Semantic search can capture documents with similar meaning even when they use different words, while keyword search ensures exact matches aren&#39;t missed. The RRF algorithm provides a principled way to combine these signals, giving higher weight to documents that rank well in both searches while still preserving strong single-source matches. The results are finally ordered by the combined RRF score in descending order and limited to the requested number of results. This hybrid approach provides more robust search results than either semantic or keyword search alone, particularly for queries where the relevant information might be expressed in various ways throughout the document.&lt;/p&gt;
&lt;h2 id=&quot;putting-it-all-together&quot; tabindex=&quot;-1&quot;&gt;Putting It All Together&lt;/h2&gt;
&lt;p&gt;Ok we have everything implemented. A basic CLI interface is provided in the reference implementation to run the pipeline the terminal. We also provide a Docker Compose setup for running the pgvector database in an ephemeral Docker container.&lt;/p&gt;
&lt;p&gt;To setup the database and ingest a text document, run the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;graphrag init
graphrag build data/Barack_Obama.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will ingest into Postgres and local graph file in the &lt;code&gt;./graphs&lt;/code&gt; folder. To search use the &lt;code&gt;query&lt;/code&gt; command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;graphrag query &lt;span class=&quot;token builtin class-name&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--graph&lt;/span&gt; graphs/1_graph.pkl &lt;span class=&quot;token string&quot;&gt;&quot;What did Barack Obama study at Columbia University?&quot;&lt;/span&gt;
graphrag query global &lt;span class=&quot;token parameter variable&quot;&gt;--graph&lt;/span&gt; graphs/1_graph.pkl --doc-id &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What are the main themes of this document?&quot;&lt;/span&gt;
graphrag query naive &lt;span class=&quot;token string&quot;&gt;&quot;What efforts did Barack Obama due to combat climate change?&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Under the hood this is using the toplevel Python API which we built above. To call it from Python import the &lt;code&gt;tiny_graphgrag&lt;/code&gt; namespace and invoke the toplevel functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tiny_graphrag &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; QueryEngine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; store_document&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; init_db

&lt;span class=&quot;token comment&quot;&gt;# Initialize the database&lt;/span&gt;
engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; init_db&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Process and store a document&lt;/span&gt;
doc_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph_path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; store_document&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;data/Barack_Obama.txt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Barack Obama Wikipedia&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    engine&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;engine
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create query engine with the database connection&lt;/span&gt;
query_engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QueryEngine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query_engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;local_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    query&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What did Barack Obama study at Columbia University?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    graph_path&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;graph_path
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Local Search Result:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query_engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    query&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What are the main themes of this document?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    doc_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;doc_id
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Global Search Result:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query_engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;naive_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    query&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What efforts did Barack Obama due to combat climate change?&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Naive Search Result:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;custom-entity-types&quot; tabindex=&quot;-1&quot;&gt;Custom Entity Types&lt;/h2&gt;
&lt;p&gt;For more specialized use cases, we can overload the entity and relation types.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; tiny_graphrag &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; QueryEngine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; store_document&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; init_db

engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; init_db&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;postgresql://admin:admin@localhost:5432/tiny-graphrag&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define custom geographic entity types&lt;/span&gt;
geography_entity_types &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;City&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Landmark&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;River&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Mountain&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Ocean&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define custom relation types with type constraints&lt;/span&gt;
geography_relation_types &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;capital_of&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;City&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;flows_through&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;River&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;City&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;located_in&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;City&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Landmark&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;borders&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_head&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;allowed_tail&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Country&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Ocean&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Process document with custom types&lt;/span&gt;
doc_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; graph_path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; store_document&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    filepath&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;data/geography.txt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;World Geography&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    engine&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    entity_types&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;geography_entity_types&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    relation_types&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;geography_relation_types
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

query_engine &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QueryEngine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;engine&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query_engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;local_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    query&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What rivers flow through Paris?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    graph_path&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;graph_path
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Geographic Local Search:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; query_engine&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global_search&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    query&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;What are the major geographical features of Western Europe?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    doc_id&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;doc_id
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Geographic Global Search:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;part-two&quot; tabindex=&quot;-1&quot;&gt;Part Two&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&quot;https://www.stephendiehl.com/posts/graphrag2&quot;&gt;Part 2&lt;/a&gt;, we&#39;ll go beyond the minimal 1000 line toy impelmentation, and add several significant improvements to our toy implementation that would make it more suitable for production use. The first major change would be replacing our serialized networkx graphs with a proper graph database (Memgraph). Graph databases are specifically optimized for relationship-based queries and can handle large-scale graphs much more efficiently than our current pickle file-based approach. They also provide built-in support for graph embeddings (node2vec), which allow us to combine structural and semantic similarity in our queries. This is particularly useful for finding related entities that might not be directly connected but share similar patterns of relationships.&lt;/p&gt;
&lt;p&gt;The second addition will address the computational expense of our current global search implementation. Instead of using a map-reduce approach that requires n+1 LLM calls, we will use top-k clustering on the community summaries. This would group similar communities together based on their vector embeddings. We will also implement a hierarchical clustering approach (like the original GraphRAG reference implementation) that creates a multi-level summary structure, enabling us to start with high-level summaries and drill down only when necessary.&lt;/p&gt;
&lt;p&gt;Then we will focus on improving entity resolution and deduplication. Our current implementation treats entities as exact string matches, which can miss connections between different references to the same entity (e.g., &amp;quot;Barack Obama&amp;quot; vs. &amp;quot;President Obama&amp;quot; vs. &amp;quot;Obama&amp;quot;). We can add vector similarity comparison for entity matching, using the embeddings of entity mentions along with their surrounding context to identify when different strings refer to the same entity.&lt;/p&gt;
&lt;h2 id=&quot;how-does-it-perform&quot; tabindex=&quot;-1&quot;&gt;How Does It Perform&lt;/h2&gt;
&lt;p&gt;GraphRAG can theoretically outperform naive RAG in certain scenarios, however both approaches suffer from fundamental limitations that make them quite fragile to use in practice. Naive RAG&#39;s effectiveness heavily depends on careful tuning of chunking strategies and embedding parameters - optimizations that often need to be discovered empirically and rarely generalize well across different domains or document types. GraphRAG attempts to address these limitations by introducing structured knowledge representation, but it essentially trades one set of hard problems for another. While it can capture relationships that naive RAG might miss, it critically depends on accurate entity and relation extraction, which is itself an unsolved problem. The quality of the final results is highly sensitive to the accuracy of these extraction steps, and errors tend to compound throughout the pipeline.&lt;/p&gt;
&lt;p&gt;Both approaches require what amounts to a sequence of statistical &amp;quot;miracles&amp;quot; to align perfectly in order to produce accurate and contextually appropriate answers. The chunking must be just right, the embeddings must capture the relevant semantic relationships, the LLM must interpret the context correctly, and in GraphRAG&#39;s case, the entity and relation extraction must also be accurate. The failure of any single component can lead to irrelevant or misleading responses. GraphRAG is not uniformly &amp;quot;better&amp;quot; but it trades an embedding simliarity problem for a entity/relation extration problem, which may be easier to solve in some domains. The global search for GraphRAG is also significantly more computationally expensive and requires n+1 (where n is the communities in the graph) calls to LLMs to perform it&#39;s map-reduce operation, which for large documents could be considerable.&lt;/p&gt;
&lt;p&gt;For anyone considering implementing either approach, it&#39;s crucial to understand that end users of this system would need to accept a high degree of potential inaccuracy and out-of-context answers when relying on these chains of black box models. The current state of RAG, whether graph-based or naive, is not sufficiently reliable for production systems where accuracy and consistency are important. While these approaches can be interesting for experimental or research purposes, they are not yet mature enough to serve as dependable solutions for production information retrieval systems.&lt;/p&gt;
&lt;p&gt;GraphRAG is not some algorithmic panacea it seems to often be marketed as. It does not &amp;quot;solve hallucinations&amp;quot; or fix the &amp;quot;pre-training overflow problem&amp;quot; that all RAG approachs that use a language model as final synthesizer suffer from. However it might prove more useful in some specific low-risk domains and may show improvement over a naive RAG implementation. In conclusion, if you&#39;re looking for a way to turn perfectly good documents into unreliable answers while burning through GPU credits, both naive RAG and GraphRAG are excellent choices.&lt;/p&gt;
</description>
      <pubDate>Mon, 04 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/graphrag1/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/graphrag1/</guid>
    </item>
    
    <item>
      <title>The Limitations of RAG</title>
      <description>&lt;h1 id=&quot;the-limitations-of-rag&quot; tabindex=&quot;-1&quot;&gt;The Limitations of RAG&lt;/h1&gt;
&lt;p&gt;Retrieval-Augmented generation or RAG is the future of enterprise search. In short it&#39;s the art of grinding your documents into a fine, high-dimensional &lt;a href=&quot;https://youtu.be/J-QeTbmchvQ?t=95&quot;&gt;vector slurry&lt;/a&gt;, sifting through the high-dimensional slurry for chunks, and then piping the chunks through a non-deterministic large language model, and praying that something resembling useful truth comes out the other end. It&#39;s like making a smoothie with your corporate knowledge base, except instead of getting a refreshing drink, you often end up with... well, let&#39;s explore that.&lt;/p&gt;
&lt;p&gt;Picture this: you take your perfectly organized documents, lovingly crafted by humans who understood context, domain terminology, and nuance, and throw them into the BERT Vector Blender Transformer™. What comes out? A chunky soup of floating point numbers that allegedly capture the &amp;quot;essence&amp;quot; of your content. It&#39;s somewhat like trying to describe the Mona Lisa&#39;s smile using the frequency coefficients of the Fourier transform of a compressed JPEG of the painting. Technically possible, but practically hard.&lt;/p&gt;
&lt;p&gt;Then you throw those vectors into a vector database and use HNSW to search through the high-dimensional space, hoping your nearest neighbor search actually finds semantically similar content rather than just unrelatedly close vectors. Then for RAG to work correctly, we need a sequence of miracles to occur:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Your chunking algorithm needs to perfectly slice your documents&lt;/li&gt;
&lt;li&gt;Your embedding model needs to capture the true semantic meaning&lt;/li&gt;
&lt;li&gt;The embedding of the question and the retrieved chunks need to be similar&lt;/li&gt;
&lt;li&gt;Your vector search needs to find the actually relevant bits&lt;/li&gt;
&lt;li&gt;Your LLM needs to weave it all together coherently with only the in-context information&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It&#39;s like playing a game of telephone, except every player is a bullshit artist who has been fine-tuned to lie instead of saying they don&#39;t know. Each component introduces its own fun flavor of randomness. The whole pipeline is like trying to hit a bullseye while riding a unicycle on a boat during a storm. Sure it can randomly work, you might even get lucky sometimes, but good luck reproducing that result!&lt;/p&gt;
&lt;p&gt;And then there&#39;s the shallow matching problem. Your RAG system might think it&#39;s being helpful by retrieving content about &amp;quot;apple pie&amp;quot; when you ask about &amp;quot;Apple&#39;s corporate structure&amp;quot; because hey, they both contain &amp;quot;apple,&amp;quot; right? It&#39;s like having a particularly enthusiastic but somewhat deaf assistant who, when you ask about &amp;quot;quantum computing,&amp;quot; eagerly provides you with information about &amp;quot;quantum healing crystals&amp;quot; because the word &amp;quot;quantum&amp;quot; is similar. The coherence of these responses often resembles a college freshman&#39;s essay written ten minutes before deadline – technically all the words are there, but they&#39;re arranged with all the grace of a drunk giraffe attempting ballet.&lt;/p&gt;
&lt;p&gt;Even with all your carefully retrieved context, LLMs still love to dance with fiction. The retrieved context is less like a strict set of facts and more like loose suggestions that the model may or may not choose to acknowledge, and there&#39;s no guarantee that the model will sample from the given context or from its general crawl of the dark corners of the internet. You might feed it your entire company handbook, only to have it confidently declare that your CEO is a time-traveling penguin who invented post-it notes. Or it might regurgitate the policies and procedures that don&#39;t exist, or mix them with the policies of a competitor, or tell your &lt;a href=&quot;https://apnews.com/article/new-york-city-chatbot-misinformation-6ebc71db5b770b9969c906a7ee4fae21&quot;&gt;employees to break the law&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And then there&#39;s the structured data problem. Ask your RAG system to find the average salary across departments while excluding temporary contractors, and it&#39;ll cheerfully write you a novel about &amp;quot;the rich tapestry of compensation across our organizational landscape.&amp;quot; It&#39;s like asking a literature professor to do your taxes - they&#39;ll spend six pages waxing lyrical about the metaphorical significance of your W-2 and conclude that your 401k represents humanity&#39;s eternal struggle against desire in a post-modern world. These systems can eloquently write a haiku about every number in your database but ask them to perform basic aggregations and suddenly they&#39;re a philosophy major who took &amp;quot;Intro to Statistics&amp;quot; as an elective: &amp;quot;But what does &#39;average&#39; really mean in a post-truth world?&amp;quot; they&#39;ll muse, while completely ignoring your desperate pleas for a simple mean calculation.&lt;/p&gt;
&lt;p&gt;Now basically none of this matters, because at least at this point in time, most AI projects are about producing slick demos for C-level executives who were drunk on the paradigm shifting power of NFTs twelve months ago, and not about solving real problems. And RAG pipeline are amazing for demos because they give the &lt;em&gt;appearance of working&lt;/em&gt; but without actually &lt;em&gt;having&lt;/em&gt; to work. Which is the best kind of engineering.&lt;/p&gt;
</description>
      <pubDate>Sat, 02 Nov 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/rag/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/rag/</guid>
    </item>
    
    <item>
      <title>The Legacy of Web3</title>
      <description>&lt;h1 id=&quot;the-legacy-of-web3&quot; tabindex=&quot;-1&quot;&gt;The Legacy of Web3&lt;/h1&gt;
&lt;p&gt;Web3 is dead, we killed it. But the legacy of the fever dream is still with us.&lt;/p&gt;
&lt;p&gt;Few movements have poisoned and corrupted minds quite like Web3 — a techno-utopian fever dream that morphed into a radicalization pipeline wrapped in memes, greed, technosolutionism, and crackpot economics. Behind the &amp;quot;have fun staying poor&amp;quot; swagger lurked something more insidious: a worldview where global problems could be solved by tokenizing everything, artificial scarcity meant freedom, and skeptics just &amp;quot;didn&#39;t understand the technology.&amp;quot; Because clearly, the solution to society&#39;s ills was letting venture capitalists repackage artificial scarcity as digital liberation.&lt;/p&gt;
&lt;p&gt;The movement preyed particularly on young men&#39;s anxieties about their economic future, offering them not just the promise of wealth, but a complete Manichaean ideology. Traditional finance was the enemy. Governments were the enemy. Any form of centralization was the enemy. The solution? Put your faith in anonymous developers and venture capitalists who definitely had your best interests at heart. And that ended about as well as you&#39;d expect.&lt;/p&gt;
&lt;p&gt;This wasn&#39;t just about gambling and exploitation – it was about radicalizing a generation. The same patterns of thought that made people vulnerable to pump-and-dump schemes made them susceptible to other forms of extremist thinking. The leap from &amp;quot;the entire financial system is rigged&amp;quot; to &amp;quot;the entire system is rigged&amp;quot; wasn&#39;t a big one. From there, it was a short hop to embracing whatever strongman promised to burn it all down. It was the normalization of anti-system politics and provided a permission structure for the economic nihilism of Trumpism.&lt;/p&gt;
&lt;p&gt;Perhaps most perverse was how Web3 repackaged financial precarity as liberation. The constant fraud, the 24/7 trading, the perpetual fear of missing out – these weren&#39;t bugs, they were features of a worldview the embraced financial nihilism. &amp;quot;Trading is freedom,&amp;quot; they said, as young men sacrificed sleep, relationships, and mental health to the altar of price charts. The gambling-like dopamine hits of small gains masked the deeper trauma of watching life savings evaporate in rugpulls and manipulated markets. Yet rather than recognize this exploitation, many victims doubled down, embracing their precarity as a badge of honor. &amp;quot;We&#39;re all gonna make it,&amp;quot; they chanted, even as their communities devolved into feral digital fight clubs where loss and pain were celebrated as necessary steps on the path to economic freedom. And most of them lost everything, including their future.&lt;/p&gt;
&lt;p&gt;And in a bizaree twist of fate, the crypto predators that once preached decentralization and liberation from traditional finance became one of D.C.&#39;s most aggressive influence-peddlers, flooding the political system with unprecedented sums – nearly half of all corporate donations by 2024. The results were predictably corrosive: politicians who once called for consumer protections now competed to promise &amp;quot;strategic Bitcoin stockpiles,&amp;quot; and bespoke regulation. The movement that claimed to stand against centralized power had simply created its own corrupting power center. The paralyzation of Washington by crypto money, even in the face of spectacular frauds and market crashes, is a symptom of &lt;em&gt;deep systemic rot&lt;/em&gt; caused by the corrupting influence of money in our political system.&lt;/p&gt;
&lt;p&gt;When Trump stood on stage and declared that he was &amp;quot;your retribution,&amp;quot; he was speaking the same language as the Web3 prophets – promising salvation through destruction, liberation through chaos. The same young men who once dreamed of cryptocurrency bringing down the banks were now dreaming of something far more dangerous: the end of democracy itself and the ascendency of a fascist strongman who spoke the illiberal language and ideology they learned from Web3.&lt;/p&gt;
&lt;p&gt;The greatest tragedy of Web3 wasn&#39;t the glut or malinvestment or the many lives it destroyed – it was how it served as training wheels for fascism, teaching a generation to distrust institutions while placing their faith in digital snake oil salesmen who promised them the world. And now, as the barbarians gather at the gate, we find ourselves a coin flip away from the end of liberal democracy that they were groomed to despise. This is the true legacy of Web3, the radicalization of young men to Trumpism and the deep institutional corruption of our democracy.&lt;/p&gt;
</description>
      <pubDate>Tue, 29 Oct 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/web3/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/web3/</guid>
    </item>
    
    <item>
      <title>AGI Discourse as Kayfabe</title>
      <description>&lt;h1 id=&quot;agi-discourse-as-kayfabe&quot; tabindex=&quot;-1&quot;&gt;AGI Discourse as Kayfabe&lt;/h1&gt;
&lt;p&gt;As most people know, the AI industry leaders who talk about Artificial General Intelligence are not sincere ... they are engaging in a kind of performative theater that serves their own interests. But it&#39;s a type of theatre that goes far beyond mere marketing. Ironically, both French philosophers and American pro-wrestlers have precise terms for this phenomenon: Jean Baudrillard called it &lt;em&gt;simulacra&lt;/em&gt; while wrestling insiders call it &lt;em&gt;kayfabe.&lt;/em&gt; The concept of simulacra describes representations so detached from reality they may become copies without originals, ultimately supplanting it. While kayfabe is the deliberate staging of fiction as authentic. Both concepts reveal how narratives often function as carefully constructed spectacles where participants knowingly play roles, prioritizing the unreal over the real.&lt;/p&gt;
&lt;p&gt;The discourse around artificial intelligence has become what Baudrillard would recognize as a perfect simulacrum, a copy without an original, where the representation has entirely supplanted the reality it ostensibly depicts. Like professional wrestling&#39;s kayfabe, the industry practice of maintaining fictional narratives as real, AI discourse operates as an elaborate performance where participants knowingly play roles that benefit everyone involved.&lt;/p&gt;
&lt;p&gt;None of this is to deny that real work that occurs in these companies. The models do do some interesting and useful things. But these actual developments exist in an increasingly tenuous relationship to their representation in the public sphere. The territory, real AI research with genuine but limited capabilities, has been almost entirely obscured by the map, a hyperreal landscape of predictions, narratives, and branded futures.&lt;/p&gt;
&lt;p&gt;The definitional vacuum around AGI creates a unique performative space, a stage where the very absence of clear meaning becomes the mechanism that sustains the narrative. This void isn&#39;t merely an obstacle to progress, but rather serves as the essential backdrop against which the performance unfolds. AI researchers can gesture toward building AGI precisely because nobody knows exactly what AGI would look like or how to recognize it. The uncertainty and ambiguity aren&#39;t bugs in the system, they&#39;re features that enable conceptual arbitrage: the ability to shift meanings and goalposts while maintaining the illusion of coherent progress. The emptiness at the center becomes the space where imagination and spectacle can flourish unencumbered by concrete definitions or measurable outcomes.&lt;/p&gt;
&lt;p&gt;Just as wrestling fans become &amp;quot;smart marks&amp;quot;—aware the performance is staged but choosing to believe anyway—the AI community has largely decided that the collective fiction is more valuable than technical reality. This is Baudrillard&#39;s third order of simulacra made manifest: the sign no longer masks or denotes a reality, but becomes its own hyperreality. AGI predictions, existential risk narratives, and breathless announcements of &amp;quot;breakthroughs&amp;quot; constitute a self-referential system where the simulation precedes and ultimately replaces any underlying technological substance. We inhabit a space where the maps have consumed the territory.&lt;/p&gt;
&lt;p&gt;Like wrestling, AI&#39;s kayfabe requires a stable cast performing predictable roles. The Faces, prophetic technologists and startup founders, promise transformative AGI is perpetually eighteen months away, their vague-posting on Twitter carefully calibrated to suggest profound insights while revealing nothing concrete. The Heels, AI safety advocates and doom-sayers, warn of existential risks, their apocalyptic pronouncements generating the very urgency that justifies massive investment in the field they critique. The Authority Figures, policy makers and tech platform leaders, make theatrical proclamations about regulation while ensuring the fundamental power structures remain untouched.&lt;/p&gt;
&lt;p&gt;Baudrillard argued that in a hypermediated world, simulations often precede and shape reality, rather than merely representing it. This inversion is starkly evident in AI discourse. The narrative of imminent AGI (not necessarily current capabilities) increasingly dictates research priorities, funding allocations, and talent acquisition. Startups chase trending buzzwords for investment, while researchers frame their work in terms that garner attention, making the simulation the engine of perceived progress.&lt;/p&gt;
&lt;p&gt;This phenomenon aligns with Baudrillard&#39;s concept of the &#39;precession of simulacra,&#39; where the map (the model, the representation) precedes and dictates the territory (reality). Consequently, AI capabilities are often evaluated not by their actual functions, but by their perceived role in the grand narrative of AGI progression. A new language model, for instance, might be hailed as a &#39;step toward AGI&#39; less for its specific, demonstrable abilities and more for &amp;quot;vibes&amp;quot; because the prevailing narrative demands such an iteration. The show must go on, the spectacle must be maintained at any cost.&lt;/p&gt;
&lt;p&gt;The effectiveness of this system stems from its widespread utility; it&#39;s less a conscious deception and more a self-reinforcing framework shaping discourse, thought, and funding. Venture capitalists secure deal flow by investing in &#39;the future.&#39; Media outlets gain engagement through sensational narratives of utopia or apocalypse. Researchers obtain grants by aligning their work with the AGI storyline. Even critics benefit, gaining platforms by highlighting existential risks that, in turn, call for more AI safety research.&lt;/p&gt;
&lt;p&gt;Crucially, the individuals with the technical expertise to potentially debunk the prevailing narrative are often those whose careers are intertwined with its perpetuation. This fosters what could be termed an &#39;epistemic conspiracy&#39;—not a mustache-twirling, deliberate, coordinated plot, but a self-organizing silence where the Emperor&#39;s sartorial choices are best left undiscussed by all who depend on the emperor&#39;s continued reign.&lt;/p&gt;
&lt;p&gt;We witness the transformation of genuine activity into passive spectatorship. AI development is increasingly experienced not through direct technical engagement but through consuming a carefully curated stream of announcements, demonstrations, and pronouncements. The technology itself becomes secondary to its representation; the spectacle becomes the primary reality.&lt;/p&gt;
&lt;p&gt;When the performance occasionally breaks kayfabe, when a demo fails, a deadline passes unmet, or promised capabilities prove hollow, these moments are quickly absorbed into the larger narrative. Failure becomes &amp;quot;learning,&amp;quot; delays become &amp;quot;responsible development,&amp;quot; and limitations become &amp;quot;opportunities for future research.&amp;quot; The show must go on because too many livelihoods depend on its continuation.&lt;/p&gt;
&lt;p&gt;Particularly revealing is the deliberate cultivation of mystique through strategic ambiguity. Vague social media posts, cryptic conference presentations, and manifestos rich in metaphor but sparse on specifics point beyond mere communication failure to intentional obfuscation. This shift signals participation in distinct language games, where the operational rules and objectives of communication adapt to the context. In the particular language game shaping AGI discourse, the emphasis appears to move away from goals like precise truth-seeking or empirical validation. Instead, the primary objective becomes the active maintenance and reinforcement of a compelling narrative, effectively prioritizing evocative storytelling and atmospheric effect over verifiable substance.&lt;/p&gt;
&lt;p&gt;Indeed, a certain level of absurdity seems intentional. When industry figures tweet about &#39;machine gods,&#39; researchers claim &#39;emergent capabilities&#39; without clear definitions, or founders invoke &#39;sparks of consciousness&#39; lacking operational criteria, they engage in performative mystification. The aim isn&#39;t necessarily clear communication but fostering an atmosphere where boundless possibilities seem perpetually imminent.&lt;/p&gt;
&lt;p&gt;The discourse around AGI represents Baudrillard&#39;s theory fully realized: a simulacrum that has entirely consumed its referent, a map that has replaced the territory so completely that we can no longer distinguish between representation and reality. The spectacle of AI progress has become more meaningful than any actual technological development it purports to represent.&lt;/p&gt;
&lt;p&gt;Much like in professional wrestling&#39;s audience, participants in the AGI discourse exhibit a spectrum of belief. Some are akin to die-hard fans, genuinely convinced by the narratives of imminent existential stakes; they wholeheartedly drink the Kool-Aid and embrace the kayfabe as literal reality. Others, however, may engage more cynically or strategically. For this latter group, participation can function as a form of sophisticated in-group signaling: by subtly acknowledging the performative nature of hype—perhaps with a knowing nod to its exaggerated claims—they demonstrate they are &amp;quot;in on it,&amp;quot; using the shared understanding of the spectacle as a currency within their circles, all while outwardly playing their prescribed roles.&lt;/p&gt;
&lt;p&gt;The AGI discourse thus fully embodies Baudrillard&#39;s theories: a simulacrum consuming its referent, a map replacing the territory so thoroughly that distinguishing representation from reality becomes challenging. Within this ecosystem, our relationship with technology is increasingly mediated by spectacle, fostering passive acceptance of appearances over substance. The AI industry excels at transforming techno-prophecy into commodified images that sustain themselves and the economic interests underpinning them. Each dramatic AGI pronouncement adds another layer to this consensual hallucination. Ultimately, in this carnival of hyperreality, much like in professional wrestling, the spectacle itself is the main event—a state of affairs the ringmasters seem keen to maintain.&lt;/p&gt;
</description>
      <pubDate>Sat, 14 Sep 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_kayfabe/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_kayfabe/</guid>
    </item>
    
    <item>
      <title>Types of Types: Common → Exotic</title>
      <description>&lt;h1 id=&quot;types-of-types%3A-common-%E2%86%92-exotic&quot; tabindex=&quot;-1&quot;&gt;Types of Types: Common → Exotic&lt;/h1&gt;
&lt;p&gt;Programming languages offer various ways to model data through their type systems. Let&#39;s explore these concepts using Lean, which has a particularly rich type system supporting four fundamental kinds of types: function types (Pi types), product types, sum types (inductive types), and quotient types. In Lean, mathematical objects exist in a three-level hierarchy:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Universes&lt;/strong&gt; (e.g., &lt;code&gt;Type&lt;/code&gt;, &lt;code&gt;Prop&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Types&lt;/strong&gt; (elements of universes)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terms&lt;/strong&gt; (elements of types)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A &lt;strong&gt;type&lt;/strong&gt; is a term of a universe (like &lt;code&gt;Type&lt;/code&gt; or &lt;code&gt;Prop&lt;/code&gt;) that classifies other terms and enforces computational and logical rules about how they can be used. A &lt;strong&gt;term&lt;/strong&gt; is a mathematical object that has a unique type, denoted using &lt;code&gt;:&lt;/code&gt; (e.g., &lt;code&gt;37 : ℝ&lt;/code&gt; means 37 is a term of type real numbers). This forms a strict hierarchy: terms are elements of types, and types are themselves terms of universes, but to avoid paradoxes like Russell&#39;s, there is no &amp;quot;type of all types&amp;quot; - instead, we have an infinite hierarchy of universes.&lt;/p&gt;
&lt;p&gt;In Lean, simple types are foundational and include basic types like &lt;code&gt;nat&lt;/code&gt; for natural numbers and &lt;code&gt;bool&lt;/code&gt; for booleans. These types are first-class citizens, meaning they can be manipulated and passed around just like any other data. Lean&#39;s type system allows for the construction of complex types from these simple ones, such as function types (&lt;code&gt;α → β&lt;/code&gt;) and product types (&lt;code&gt;α × β&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;#check&lt;/code&gt; command in Lean is a powerful tool used to query the type of an expression. It helps verify the types of constants, functions, and expressions, ensuring they conform to expected types. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;constant m : nat    -- m is a natural number
constant n : nat    -- n is a natural number
#check m            -- output: nat
#check n + 0        -- nat
#check tt           -- boolean &amp;quot;true&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While set theory uses &lt;code&gt;∈&lt;/code&gt; for membership, type theory uses &lt;code&gt;:&lt;/code&gt; to denote that a term has a particular type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- In set theory: 37 ∈ ℝ
-- In type theory:
#check 37 : ℝ        -- 37 is a term of type ℝ
#check ℝ : Type      -- ℝ is a term of type Type
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To avoid &lt;a href=&quot;https://en.wikipedia.org/wiki/Russell%27s_paradox&quot;&gt;Russell&#39;s Paradox&lt;/a&gt;, there isn&#39;t a type of all types. Instead, we have universes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Type&lt;/code&gt;: Universe containing mathematical objects (e.g., &lt;code&gt;ℝ&lt;/code&gt;, &lt;code&gt;ℕ&lt;/code&gt;, groups, rings)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Prop&lt;/code&gt;: Universe containing propositions (true/false statements)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For types &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Y&lt;/code&gt;, the type of functions from &lt;code&gt;X&lt;/code&gt; to &lt;code&gt;Y&lt;/code&gt; is written &lt;code&gt;X → Y&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def square : ℝ → ℝ := λ x, x^2
#check square        -- ℝ → ℝ
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The famous &lt;a href=&quot;https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence&quot;&gt;Curry-Howard correspondence&lt;/a&gt; states:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Propositions are types in &lt;code&gt;Prop&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Proofs are terms of these types&lt;/li&gt;
&lt;li&gt;True propositions have exactly one term&lt;/li&gt;
&lt;li&gt;False propositions have zero terms&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- P → Q represents both:
-- 1. A function type from proofs of P to proofs of Q
-- 2. The logical implication P implies Q
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Function types&lt;/strong&gt;, also known as &lt;strong&gt;Pi types&lt;/strong&gt;, represent mappings between types. They can either be dependent or non-dependent. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- Simple function type (non-dependent)
def square : ℝ → ℝ := λ x, x^2

-- Dependent function type (Pi type)
def vector_length : Π (n : Nat), Vector ℝ n → ℝ
| 0 _ := 0
| (n+1) v := real.sqrt (dot_product v v)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Pi type notation &lt;code&gt;Π (x : A), B x&lt;/code&gt; represents a dependent function type where the return type &lt;code&gt;B x&lt;/code&gt; can depend on the input value &lt;code&gt;x&lt;/code&gt;. When the return type doesn&#39;t depend on the input, we use the simpler arrow notation &lt;code&gt;A → B&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Product types&lt;/strong&gt; represent combinations of multiple values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;structure Point :=
  (x : Int)
  (y : Int)

structure Person :=
  (name : String)
  (age : Int)
  (address : String)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Sum types&lt;/strong&gt;, also known as &lt;strong&gt;inductive types&lt;/strong&gt;, represent alternatives:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;inductive Shape
| circle (radius : Float)
| rectangle (width : Float) (height : Float)
| triangle (base : Float) (height : Float)

-- Example of using the equation compiler with sum types
def area : Shape → Float
| Shape.circle r := Float.pi * r * r
| Shape.rectangle w h := w * h
| Shape.triangle b h := b * h / 2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quotient types&lt;/strong&gt; represent values that are equivalent under some relation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- Define an equivalence relation for angles
def angle_equiv (a b : Float) : Prop :=
  ∃ k : Int, a = b + k * 360

-- Create a quotient type for angles mod 360
def Angle := quotient (setoid.mk angle_equiv)

-- Constructor for angles
def mk_angle (degrees : Float) : Angle :=
  quotient.mk degrees
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Dependent types&lt;/strong&gt; allow types to depend on values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- A vector type whose length is part of its type
inductive Vector (α : Type) : Nat → Type
| nil : Vector α 0
| cons : α → {n : Nat} → Vector α n → Vector α (n + 1)

-- A function whose return type depends on its input
def get_type (b : Bool) : Type :=
  if b then Nat else String
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Difference types&lt;/strong&gt; represent the &amp;quot;subtraction&amp;quot; of one type from another. While not directly supported in Lean, we can encode them using dependent types and proof obligations. They&#39;re particularly useful when we want to express &amp;quot;Type A without elements satisfying property B.&amp;quot;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- Non-zero numbers: ℝ &amp;quot;minus&amp;quot; 0
def NonZero := {x : ℝ // x ≠ 0}

-- Non-empty lists: List α &amp;quot;minus&amp;quot; empty list
def NonEmptyList (α : Type) := {l : List α // l ≠ []}

-- Even numbers: ℕ &amp;quot;minus&amp;quot; odd numbers
def Even := {n : ℕ // ∃ k, n = 2 * k}

-- Injective functions: (α → β) &amp;quot;minus&amp;quot; non-injective functions
def Injection (α β : Type) := {f : α → β // ∀ x y, f x = f y → x = y}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The syntax &lt;code&gt;{x : A // P x}&lt;/code&gt; represents a subtype of &lt;code&gt;A&lt;/code&gt; where elements satisfy predicate &lt;code&gt;P&lt;/code&gt;. This is Lean&#39;s way of encoding difference types through dependent pairs (also known as Σ-types).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;-- Creating terms of difference types
def two_nonzero : NonZero := ⟨2, by norm_num⟩

-- Extracting the underlying value
def get_value (n : NonZero) : ℝ := n.val

-- The proof of the property is also accessible
def get_proof (n : NonZero) : n.val ≠ 0 := n.property
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, division can be made total (removing the divide-by-zero case) by using &lt;code&gt;NonZero&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;def safe_div (a : ℝ) (b : NonZero) : ℝ := a / b.val
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This may all sound abstract, but these types are used in many practical applications, broadly the uses of these types fall into the following categories:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Function Types (Pi Types)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Higher-order functions&lt;/li&gt;
&lt;li&gt;Dependent functions in proof assistants&lt;/li&gt;
&lt;li&gt;Type families&lt;/li&gt;
&lt;li&gt;Polymorphic functions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Product Types&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Configuration records&lt;/li&gt;
&lt;li&gt;Data structures&lt;/li&gt;
&lt;li&gt;Complex domain models&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Sum Types&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;li&gt;State machines&lt;/li&gt;
&lt;li&gt;Abstract syntax trees&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Quotient Types&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Modular arithmetic&lt;/li&gt;
&lt;li&gt;Equivalence classes&lt;/li&gt;
&lt;li&gt;Mathematical structures&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Difference Types&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enforcing invariants at compile time&lt;/li&gt;
&lt;li&gt;Removing edge cases from algorithms&lt;/li&gt;
&lt;li&gt;Making implicit assumptions explicit&lt;/li&gt;
&lt;li&gt;Strengthening function preconditions&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Wed, 04 Sep 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/types_of_types/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/types_of_types/</guid>
    </item>
    
    <item>
      <title>Monte Carlo Tree Search</title>
      <description>&lt;h1 id=&quot;monte-carlo-tree-search&quot; tabindex=&quot;-1&quot;&gt;Monte Carlo Tree Search&lt;/h1&gt;
&lt;p&gt;Monte Carlo Tree Search is a robust algorithm used for decision making in game playing and reinforcement learning. It is a heuristic search algorithm that combines the best aspects of tree search and Monte Carlo simulation. MCTS is particularly useful when dealing with large and complex search spaces, such as in neurosymbolic systems and reinforcement learning.&lt;/p&gt;
&lt;p&gt;Today let&#39;s implement MCTS in Python. First we need to define a &lt;code&gt;Node&lt;/code&gt; class that will be used to represent the state.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;rom dataclasses &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; dataclass&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; field
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; collections &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; defaultdict
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; math
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Optional
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; abc &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ABC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; abstractmethod
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; random


&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ABC&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Abstract base class for game state nodes.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_children&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Node&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Return all possible child nodes.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_random_child&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Node&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Return a random child node.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_terminal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Check if the node is a terminal state.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Return the reward value for a terminal node.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__hash__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Define a hash function for the node.&quot;&quot;&quot;&lt;/span&gt;

    &lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@abstractmethod&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__eq__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Define equality comparison for nodes.&quot;&quot;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll define the &lt;code&gt;MCTS&lt;/code&gt; class that will hold the search state and perform the tree search.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MCTS&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Monte Carlo Tree Search implementation.&quot;&quot;&quot;&lt;/span&gt;

    exploration_weight&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;
    total_rewards&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;default_factory&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; defaultdict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    visit_counts&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;default_factory&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; defaultdict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    children&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Dict&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; field&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;default_factory&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; defaultdict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll implement the &lt;code&gt;select_best_move&lt;/code&gt; method that will select the best move from the current node.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;select_best_move&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Select the best move from the current node.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Cannot select move from terminal node: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_random_child&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the algorithm we&#39;ll use the UCT (Upper Confidence Trees) formula to select the best move.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
UCT(v) = &#92;frac{w_v}{n_v} + c &#92;sqrt{&#92;frac{&#92;ln(N)}{n_v}}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$w_v$ is the total reward of the node.&lt;/li&gt;
&lt;li&gt;$n_v$ is the number of times the node has been visited.&lt;/li&gt;
&lt;li&gt;$N$ is the total number of simulations.&lt;/li&gt;
&lt;li&gt;$c$ is the exploration constant.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;simulate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Perform one iteration of the MCTS algorithm.&quot;&quot;&quot;&lt;/span&gt;
        path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_traverse_tree&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        leaf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; path&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_expand_node&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;leaf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        reward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_simulate_random_playout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;leaf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_backpropagate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; reward&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_traverse_tree&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Traverse the tree to find an unexplored node.&quot;&quot;&quot;&lt;/span&gt;
        path &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; path
            unexplored &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; unexplored&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                path&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unexplored&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; path
            node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_select_uct&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_expand_node&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Expand the node by adding its children to the tree.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;find_children&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll implement the &lt;code&gt;_simulate_random_playout&lt;/code&gt; method that will simulate a random playout from the given node.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_simulate_random_playout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Simulate a random playout from the given node.&quot;&quot;&quot;&lt;/span&gt;
        invert_reward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                reward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reward&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; reward &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; invert_reward &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; reward
            node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_random_child&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            invert_reward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; invert_reward
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we&#39;ll implement the &lt;code&gt;_backpropagate&lt;/code&gt; method that will update the statistics for the nodes in the path.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_backpropagate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; path&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; reward&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Update the statistics for the nodes in the path.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; node &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;total_rewards&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; reward
            reward &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; reward
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The select method will select a child node using the UCT metric.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_select_uct&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Select a child node using the UCT formula.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        log_n_parent &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uct&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;total_rewards&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
                n
            &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exploration_weight &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;log_n_parent &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;uct&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then finally we&#39;ll implement the &lt;code&gt;_calculate_node_score&lt;/code&gt; method that will calculate the score for a node based on its average reward.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_calculate_node_score&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nodeNode&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Calculate the score for a node based on its average reward.&quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;-inf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;total_rewards&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;visit_counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to use this we need to define a &lt;code&gt;TicTacToeNode&lt;/code&gt; class that will be used to represent the state of the game.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TicTacToeNode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Node&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; state&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; player&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; state
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; player

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_children&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;TicTacToeNode&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            TicTacToeNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token string&quot;&gt;&quot;O&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; value &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_random_child&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;TicTacToeNode&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
        empty_spots &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; value &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; value &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;choice&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;empty_spots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; TicTacToeNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;O&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_terminal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;winner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reward&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        winner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;winner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; winner &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Draw&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; winner &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;player &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__hash__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__eq__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;other&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TicTacToeNode&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; other&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;winner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        lines &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Rows&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Columns&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Diagonals&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; line &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; lines&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;line&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;line&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;line&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;line&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use the &lt;code&gt;MCTS&lt;/code&gt; class to play a game of TicTacToe.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play_game&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;
    mcts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MCTS&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    board &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TicTacToeNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Initial board:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    print_board&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Human player&#39;s turn (O)&lt;/span&gt;
        human_move &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Enter your move (0-8): &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        board &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TicTacToeNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;human_move&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;O&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;human_move &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;X&quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;nBoard after your move:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        print_board&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# AI player&#39;s turn (X)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Number of MCTS iterations&lt;/span&gt;
            mcts&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;simulate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;board&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        board &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mcts&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;select_best_move&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;board&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;nBoard after AI move:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        print_board&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_terminal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;break&lt;/span&gt;

    winner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; board&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;winner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; winner&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&#92;nPlayer &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;winner&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; wins!&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;nIt&#39;s a draw!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;print_board&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    play_game&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Wed, 04 Sep 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/mtcs/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/mtcs/</guid>
    </item>
    
    <item>
      <title>Fine-tuning with ORPO and Unsloth</title>
      <description>&lt;h1 id=&quot;fine-tuning-with-orpo-and-unsloth&quot; tabindex=&quot;-1&quot;&gt;Fine-tuning with ORPO and Unsloth&lt;/h1&gt;
&lt;p&gt;ORPO was introduced in the paper &lt;a href=&quot;https://huggingface.co/docs/trl/main/en/orpo_trainer&quot;&gt;&amp;quot;ORPO: Monolithic Preference Optimization without Reference Model&amp;quot;&lt;/a&gt;. The key innovation is that it eliminates the need for a separate preference alignment phase by incorporating a minor penalty for disfavored generations during the training process.&lt;/p&gt;
&lt;p&gt;Traditional approaches like DPO require two steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Supervised fine-tuning on the base model&lt;/li&gt;
&lt;li&gt;Preference optimization using paired data&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This two-step process has a significant drawback: during SFT, the model increases the probability of generating both preferred and undesired responses. This happens because SFT treats all training examples as equally valid, without distinguishing between better and worse responses. As a result, a separate direct preference alignment step is needed to widen the gap between preferred and rejected outputs. With ORPO, we can do it all in one step, which is great and more efficient.&lt;/p&gt;
&lt;p&gt;ORPO requires a preference dataset with three key components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A prompt/instruction&lt;/li&gt;
&lt;li&gt;A chosen (preferred) response&lt;/li&gt;
&lt;li&gt;A rejected (non-preferred) response&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A preference dataset will look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the meaning of life?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;chosen&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The meaning of life is to grow, learn, and find purpose through our experiences and connections with others.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;rejected&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;42 lol&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ll use the new Llama 3.2 8B model with Unsloth for efficient training. First, let&#39;s set up our environment:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install required dependencies&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; git+https://github.com/facebookresearch/xformers.git@main&lt;span class=&quot;token comment&quot;&gt;#egg=xformers&lt;/span&gt;
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s set up our training script:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; TextStreamer
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ORPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ORPOConfig

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; unsloth &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; is_bfloat16_supported
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; unsloth&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chat_templates &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; get_chat_template

&lt;span class=&quot;token comment&quot;&gt;# Model configuration&lt;/span&gt;
max_seq_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2048&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model_name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unsloth/Meta-Llama-3-8B-bnb-4bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_seq_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;max_seq_length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    load_in_4bit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Setup Unsloth&#39;s optimized LoRA&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_peft_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;q_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;k_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;v_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;o_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;gate_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;up_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;down_proj&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    use_rslora&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Faster training&lt;/span&gt;
    use_gradient_checkpointing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unsloth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Setup ChatML format&lt;/span&gt;
tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    chat_template&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;chatml&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the dataset, we&#39;ll use the &lt;code&gt;ultrafeedback_binarized&lt;/code&gt; dataset which contains binarized preferences for training a model to avoid unethical and toxic responses. Using trl we don&#39;t need to preprocess the dataset since it already understands the preference format.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;trl-lib/ultrafeedback_binarized&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s configure the ORPO trainer with Unsloth-optimized settings:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;orpo_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ORPOConfig&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8e-6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    beta&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cosine_with_restarts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Better with Unsloth&lt;/span&gt;
    max_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2048&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_prompt_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Increased due to Unsloth optimizations&lt;/span&gt;
    per_device_eval_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    optim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;adamw_8bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    evaluation_strategy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;steps&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./results/&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    fp16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; is_bfloat16_supported&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bf16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;is_bfloat16_supported&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ORPOTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;orpo_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    eval_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After training, we can save the model in various formats:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Save LoRA weights&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lora_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lora_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Save merged model in 16-bit&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Saving merged model...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_merged&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fused_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;merged_16bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Save GGUF formats for inference&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Saving GGUF formats...&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_gguf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ggufs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; quantization_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;f16&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_gguf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ggufs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; quantization_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;q4_k_m&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;During training, ORPO logs several important metrics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rewards/chosen&lt;/code&gt;: Log probabilities for chosen responses&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rewards/rejected&lt;/code&gt;: Log probabilities for rejected responses&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rewards/accuracies&lt;/code&gt;: How often chosen rewards exceed rejected rewards&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rewards/margins&lt;/code&gt;: Mean difference between chosen and rejected rewards&lt;/li&gt;
&lt;li&gt;&lt;code&gt;log_odds_ratio&lt;/code&gt;: Mean log odds ratio of chosen vs rejected responses&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nll_loss&lt;/code&gt;: Negative log likelihood loss from SFT component&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can monitor these metrics using Weights &amp;amp; Biases.&lt;/p&gt;
</description>
      <pubDate>Tue, 03 Sep 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/orpo/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/orpo/</guid>
    </item>
    
    <item>
      <title>Pi from String Amplitudes</title>
      <description>&lt;h1 id=&quot;pi-from-string-amplitudes&quot; tabindex=&quot;-1&quot;&gt;Pi from String Amplitudes&lt;/h1&gt;
&lt;p&gt;From the &lt;a href=&quot;https://journals.aps.org/prl/pdf/10.1103/PhysRevLett.132.221601&quot;&gt;paper by Arnab Priya Saha and Aninda Sinha&lt;/a&gt;. A new series for &#92;(&#92;pi&#92;) and its convergence.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;pi = 4 + &#92;sum_{n=1}^{&#92;infty} &#92;frac{1}{n!} &#92;left( &#92;frac{1}{n + &#92;lambda} - &#92;frac{4}{2n + 1} &#92;right) &#92;left( &#92;frac{(2n + 1)^2}{4(n + &#92;lambda)} - n &#92;right)_{n-1}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where &#92;((a)_{n-1}&#92;) is the Pochhammer symbol.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(a)_{n-1} = &#92;frac{&#92;Gamma(a + n - 1)}{&#92;Gamma(a)}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;&#92;(&#92;lambda&#92;) is a free parameter for optimizing convergence. As &#92;(&#92;lambda&#92;) approaches infinity, the series reduces to the classic Madhava series. With &#92;(&#92;lambda&#92;) between 10 and 100, the series takes 30 terms to converge to ten decimal places. Which is far more efficient than the classic &lt;a href=&quot;https://en.wikipedia.org/wiki/Madhava_series&quot;&gt;Madhava series&lt;/a&gt;, which takes 5 billion terms to converge to ten decimal places.&lt;/p&gt;
&lt;p&gt;This can be implemented in Python as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; mpmath &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; mp

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mp_calculate_pi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lambda_val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; iters&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;
    pi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mpf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
    prev_pi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    factorial &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mpf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;iters&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        term1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; factorial
        term2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; lambda_val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        term3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; lambda_val&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; n

        pochhammer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            pochhammer &lt;span class=&quot;token operator&quot;&gt;*=&lt;/span&gt; term3 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; i

        prev_pi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pi
        pi &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; term1 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; term2 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; pochhammer

        &lt;span class=&quot;token comment&quot;&gt;# print to 30 decimal places using mpmath&lt;/span&gt;
        approx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nstr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Compare to mp.pi to calcualte the delta&lt;/span&gt;
        delta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# numbers of digits that pi approximate is correct to&lt;/span&gt;
        accuracy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Print a line with the current iteration, pi, delta&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;approx&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;delta&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;accuracy&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        n &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
        factorial &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; factorial &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; n

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; pi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

lambda_val &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;
pi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mp_calculate_pi&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lambda_val&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Sun, 01 Sep 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/madhva/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/madhva/</guid>
    </item>
    
    <item>
      <title>Diaconescu&#39;s Theorem</title>
      <description>&lt;h1 id=&quot;diaconescu&#39;s-theorem&quot; tabindex=&quot;-1&quot;&gt;Diaconescu&#39;s Theorem&lt;/h1&gt;
&lt;p&gt;Diaconescu&#39;s Theorem, discovered by Radu Diaconescu in 1975, reveals a fascinating and surprising connection between two fundamental principles in mathematics: the Axiom of Choice and the Law of Excluded Middle. In simple terms, the theorem states that if you accept the Axiom of Choice, you must also accept the Law of Excluded Middle. The theorem was independently discovered by Goodman and Myhill in 1978, and is sometimes called the Diaconescu-Goodman-Myhill theorem. Interestingly, the Errett Bishop had posed this result as an exercise in his 1967 book on constructive analysis, before Diaconescu&#39;s publication.&lt;/p&gt;
&lt;p&gt;As background, the Law of Excluded Middle states that for any proposition &#92;(P&#92;), either &#92;(P&#92;) is true or &#92;(P&#92;) is false - there is no middle condition. Or symbolically, &#92;(P &#92;lor &#92;neg P&#92;). For example, either a number is even or it&#39;s not even. This seems intuitive, but not all mathematical frameworks accept this principle!&lt;/p&gt;
&lt;p&gt;The Axiom of Choice states that given any collection of non-empty sets, it&#39;s possible to select exactly one element from each set to form a new set. While this also seems intuitive, it has some surprising consequences.&lt;/p&gt;
&lt;p&gt;Diaconescu&#39;s Theorem shows that:&lt;/p&gt;
&lt;p&gt;$$&#92;text{Axiom of Choice} &#92;implies &#92;text{Law of Excluded Middle}$$&lt;/p&gt;
&lt;p&gt;Before diving into the proof, let&#39;s establish some important concepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A set is called &lt;strong&gt;decidable&lt;/strong&gt; if for all elements, we can determine whether they belong to the set or not. Formally, for any element &#92;(x&#92;), either &#92;(x &#92;in S&#92;) or &#92;(x &#92;notin S&#92;).&lt;/li&gt;
&lt;li&gt;A set is called a &lt;strong&gt;choice set&lt;/strong&gt; if every surjection onto it splits (meaning there exists a map going back such that their composition is the identity).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The proof works through the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Start with any proposition &#92;(P&#92;) that we want to prove must be either true or false&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Construct two sets:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(U = {x &#92;in {0,1} &#92;mid (x = 0) &#92;lor P}&#92;)&lt;/li&gt;
&lt;li&gt;&#92;(V = {x &#92;in {0,1} &#92;mid (x = 1) &#92;lor P}&#92;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Both sets are guaranteed to be non-empty:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(U&#92;) contains 0 (because &#92;(x = 0&#92;) is true for &#92;(x = 0&#92;))&lt;/li&gt;
&lt;li&gt;&#92;(V&#92;) contains 1 (because &#92;(x = 1&#92;) is true for &#92;(x = 1&#92;))&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Form the set &#92;(X = {U, V}&#92;). By the Axiom of Choice, since both &#92;(U&#92;) and &#92;(V&#92;) are non-empty, we can select one element from each set.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let &#92;(f&#92;) be our choice function. Since equality is decidable in choice sets (a key lemma from the formal proof), we can analyze &#92;(f(U)&#92;) and &#92;(f(V)&#92;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &#92;(f(U) = 0&#92;), then &#92;(P&#92;) must be false (&#92;(&#92;neg P&#92;))&lt;/li&gt;
&lt;li&gt;If &#92;(f(U) = 1&#92;), then &#92;(P&#92;) must be true&lt;/li&gt;
&lt;li&gt;The same analysis applies to &#92;(f(V)&#92;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Through this analysis, we can conclude &#92;(P &#92;lor &#92;neg P&#92;) must hold.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can formalize this theorem in Lean. The proof uses function extensionality and propositional extensionality alongside choice. The Lean proof follows the same logical structure as our mathematical proof, but works with propositions instead of sets. It uses function and propositional extensionality to handle equality of predicates, and the Axiom of Choice appears in the form of the &lt;code&gt;choose&lt;/code&gt; function, which selects witnesses from existential statements.&lt;/p&gt;
&lt;p&gt;First, we state the theorem as the type declaration of &lt;code&gt;em&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem em (p : Prop) : p ∨ ¬p :=
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s break down how this formal proof works:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;First, we define two predicates &lt;code&gt;U&lt;/code&gt; and &lt;code&gt;V&lt;/code&gt; that take a proposition &lt;code&gt;x&lt;/code&gt; and return another proposition:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;let U (x : Prop) : Prop := x = True ∨ p
let V (x : Prop) : Prop := x = False ∨ p
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These correspond to the sets U and V in our mathematical proof, but work with propositions instead of {0,1}.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We prove that both U and V are inhabited (non-empty):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;have exU : ∃ x, U x := ⟨True, Or.inl rfl⟩   -- True satisfies U because True = True
have exV : ∃ x, V x := ⟨False, Or.inl rfl⟩  -- False satisfies V because False = False
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using the Axiom of Choice (via &lt;code&gt;choose&lt;/code&gt;), we select witnesses u and v from U and V:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;let u : Prop := choose exU
let v : Prop := choose exV
have u_def : U u := choose_spec exU  -- u satisfies U
have v_def : V v := choose_spec exV  -- v satisfies V
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The key insight is that either u ≠ v, or p must be true:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;have not_uv_or_p : u ≠ v ∨ p :=
  match u_def, v_def with
  | Or.inr h, _ =&amp;gt; Or.inr h        -- If U(u) because of p, then p is true
  | _, Or.inr h =&amp;gt; Or.inr h        -- If V(v) because of p, then p is true
  | Or.inl hut, Or.inl hvf =&amp;gt;      -- Otherwise, u = True and v = False
    have hne : u ≠ v := by simp [hvf, hut, true_ne_false]
    Or.inl hne
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We then prove that if p is true, then u = v:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;have p_implies_uv : p → u = v :=
  fun hp =&amp;gt;
  have hpred : U = V :=    -- If p is true, then U and V are the same predicate
    funext fun x =&amp;gt;        -- Using function extensionality
      have hl : (x = True ∨ p) → (x = False ∨ p) :=
        fun _ =&amp;gt; Or.inr hp
      have hr : (x = False ∨ p) → (x = True ∨ p) :=
        fun _ =&amp;gt; Or.inr hp
      show (x = True ∨ p) = (x = False ∨ p) from
        propext (Iff.intro hl hr)   -- Using propositional extensionality
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, we conclude p ∨ ¬p by case analysis:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;match not_uv_or_p with
| Or.inl hne =&amp;gt; Or.inr (mt p_implies_uv hne)  -- If u ≠ v, then ¬p
| Or.inr h   =&amp;gt; Or.inl h                      -- If p, then p
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Diaconescu&#39;s theorem demonstrates a profound connection between choice principles and classical logic. This result has significant metamathematical implications: it reveals that choice principles are inherently non-constructive, as they force classical reasoning even in otherwise constructive settings. From a foundational perspective, this shows that choice principles are not logically neutral - they implicitly commit us to classical logic. For constructive mathematics, this necessitates careful consideration of choice principles, as even seemingly innocuous uses of AC can inadvertently classical-ize an otherwise constructive development. The theorem thus exemplifies how foundational axioms can have subtle but far-reaching consequences for the logical character of mathematical theories. You could say that when it comes to the Axiom of Choice and the Law of Excluded Middle, there&#39;s no choosing the middle ground!&lt;/p&gt;
</description>
      <pubDate>Mon, 05 Aug 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/diaconescu/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/diaconescu/</guid>
    </item>
    
    <item>
      <title>A Rapid Tutorial on Unsloth</title>
      <description>&lt;h1 id=&quot;a-rapid-tutorial-on-unsloth&quot; tabindex=&quot;-1&quot;&gt;A Rapid Tutorial on Unsloth&lt;/h1&gt;
&lt;p&gt;Unsloth is a library for fast and efficient fine-tuning of large language models. It is built on top of the Hugging Face Transformers library and can be used to fine-tune models on a variety of tasks. In this case we&#39;ll train a Llama 3.1 model to do text to sql generation.&lt;/p&gt;
&lt;p&gt;Unsloth can be used to do 2x faster training and 60% less memory than standard fine-tuning on single GPU setups. It uses a technique called Quantized Low Rank Adaptation (QLoRA) to quantize the model and train a small number of additional trainable parameters instead of the entire model. This allows us to train the model on a single NVIDIA H100 GPU.&lt;/p&gt;
&lt;p&gt;We&#39;ll need to use raw pip to install flash-attn and xformers from source.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Install flash-attn&lt;/span&gt;
poetry run pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; flash-attn --no-build-isolation

&lt;span class=&quot;token comment&quot;&gt;# Install xformers&lt;/span&gt;
poetry run pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-U&lt;/span&gt; git+https://github.com/facebookresearch/xformers.git@main&lt;span class=&quot;token comment&quot;&gt;#egg=xformers&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Install unsloth&lt;/span&gt;
poetry run pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rest can be installed via poetry from PyPI.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-toml&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token table class-name&quot;&gt;tool.poetry.dependencies&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;trl&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^0.9.6&quot;&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;nvitop&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^1.3.2&quot;&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;xformers&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0.0.27&quot;&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;peft&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^0.12.0&quot;&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;accelerate&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^0.33.0&quot;&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;bitsandbytes&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^0.43.3&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can initialize out &lt;code&gt;train.py&lt;/code&gt; script. Load the following Hugging Face libraries:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; torch

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; datasets &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; load_dataset
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; EarlyStoppingCallback&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TextStreamer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrainingArguments
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; trl &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SFTTrainer

&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; unsloth &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; is_bfloat16_supported
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; unsloth&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;chat_templates &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; get_chat_template
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ll download the 8 billion parameter Llama 3.1 model that has been quantized to 4 bits. This means that the model has been compressed to use 4 bits instead of 16 bits for its weights. This makes the model much faster to load and use, but it is still a fairly large model.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;max_seq_length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2048&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model_name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unsloth/Meta-Llama-3.1-8B-bnb-4bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_seq_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;max_seq_length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    load_in_4bit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_peft_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lora_dropout&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_modules&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;q_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;k_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;v_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;up_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;down_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;o_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;gate_proj&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    use_rslora&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    use_gradient_checkpointing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;unsloth&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

tokenizer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    chat_template&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;chatml&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ll use a synthetic data set of text to sql prompts.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;question&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; question&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sql_prompt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; question&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;sql&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_template&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;examples&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    messages &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; examples&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;conversations&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    text &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            convert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; add_generation_prompt&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; message &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; messages
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; text&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;


dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; load_dataset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gretelai/synthetic_text_to_sql&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; split&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;train&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
dataset &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;apply_template&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; batched&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ll use the 8bit AdamW optimizer with the cosine annealing warm restarts scheduler. In addition we&#39;ll add early stopping to prevent overfitting.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;callbacks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    EarlyStoppingCallback&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;early_stopping_patience&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    learning_rate&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3e-4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;linear&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    per_device_train_batch_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gradient_accumulation_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    num_train_epochs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    fp16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; is_bfloat16_supported&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bf16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;is_bfloat16_supported&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    logging_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    optim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;adamw_8bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lr_scheduler_type &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cosine_with_restarts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    weight_decay&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    warmup_steps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    seed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SFTTrainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenizer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dataset_text_field&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_seq_length&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;max_seq_length&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    dataset_num_proc&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    packing&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    load_best_model_at_end&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    metric_for_best_model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;eval_loss&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    callbacks&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;callbacks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Done.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After running this you should see output like this. This will run for six epochs and should take about 2-3 hours to complete on a Nvidia H100 GPU.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# ==((====))==  Unsloth - 2x faster free finetuning | Num GPUs = 1
#    &#92;&#92;   /|    Num examples = 2,254 | Num Epochs = 3
# O^O/ &#92;_/ &#92;    Batch size per device = 2 | Gradient Accumulation steps = 4
# &#92;        /    Total batch size = 8 | Total steps = 843
#  &amp;quot;-____-&amp;quot;     Number of trainable parameters = 29,884,416
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use the model to generate text.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FastLanguageModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;for_inference&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

messages &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;What is the average salary of employees in the IT department?&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;assistant&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
inputs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_chat_template&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    messages&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    tokenize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    add_generation_prompt&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    return_tensors&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pt&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;to&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;cuda&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

text_streamer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TextStreamer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tokenizer&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
_ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;generate&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    input_ids&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; streamer&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;text_streamer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; max_new_tokens&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; use_cache&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The model can also be exported to a variety of formats.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_merged&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;merged_16bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer_stats &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Done.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lora_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
tokenizer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lora_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Merging model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_merged&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fused_model&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; save_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;merged_16bit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Saving gguf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_gguf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ggufs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; quantization_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;f16&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;save_pretrained_gguf&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ggufs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tokenizer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; quantization_method&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;q4_k_m&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should see output like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;==((====))==  Unsloth: Conversion from QLoRA to GGUF information
   &#92;&#92;   /|    [0] Installing llama.cpp will take 3 minutes.
O^O/ &#92;_/ &#92;    [1] Converting HF to GGUF 16bits will take 3 minutes.
&#92;        /    [2] Converting GGUF 16bits to [&#39;f16&#39;] will take 10 minutes each.
 &amp;quot;-____-&amp;quot;     In total, you will have to wait at least 16 minutes.

Unsloth: [0] Installing llama.cpp. This will take 3 minutes...
Unsloth: [1] Converting model at lora-fused into f16 GGUF format.
The output location will be ./lora-fused/unsloth.F16.gguf

...

Writing: 100%|██████████| 7.64G/7.64G [01:18&amp;lt;00:00, 97.1Mbyte/s]
INFO:hf-to-gguf:Model successfully exported to lora-fused/unsloth.F16.gguf
Unsloth: Conversion completed! Output location: ./lora-fused/unsloth.F16.gguf
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that&#39;s it! You now have a fine-tuned text to sql generator. This can easily be adapted to other tasks with minimal changes to the data processing script.&lt;/p&gt;
&lt;p&gt;From here you can also train the model using a variety of reinforcement learning techniques to further improve the model&#39;s performance.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/trl/main/en/kto_trainer&quot;&gt;Kahneman-Tversky Optimization (KTO)&lt;/a&gt; can be used aligning language models with binary feedback data.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/docs/trl/main/en/dpo_trainer&quot;&gt;Direct Preference Optimization (DPO)&lt;/a&gt; can be used aligning language models with pairwise feedback data.&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Sun, 28 Jul 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/unsloth/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/unsloth/</guid>
    </item>
    
    <item>
      <title>Basic Formal Ontology</title>
      <description>&lt;h1 id=&quot;basic-formal-ontology&quot; tabindex=&quot;-1&quot;&gt;Basic Formal Ontology&lt;/h1&gt;
&lt;p&gt;The &lt;em&gt;Basic Formal Ontology&lt;/em&gt; is an upper ontology that can describe a broad swatch of human knowledge. It&#39;s a cornerstone for logic programmers and those creating comprehensive knowledge graphs which cover multiple domains.&lt;/p&gt;
&lt;pre class=&quot;mermaid&quot;&gt;graph TD;&amp;#10;  Q --&amp;gt; Q;&amp;#10;  Q[Variable Order-Class] --&amp;gt; P;&amp;#10;  P[Metaclass] --&amp;gt; Z;&amp;#10;  Z[Class] --&amp;gt; A;&amp;#10;  A[Entity] --&amp;gt; B[Continuant];&amp;#10;  A --&amp;gt; C[Occurrent];&amp;#10;  B --&amp;gt; D[Independent Continuant];&amp;#10;  B --&amp;gt; E[Dependent Continuant];&amp;#10;  D --&amp;gt; F[Object];&amp;#10;  D --&amp;gt; G[Object Aggregate];&amp;#10;  D --&amp;gt; H[Fiat Object];&amp;#10;  E --&amp;gt; I[Specifically Dependent Continuant];&amp;#10;  E --&amp;gt; J[Generically Dependent Continuant];&amp;#10;  I --&amp;gt; K[Quality];&amp;#10;  I --&amp;gt; L[Disposition];&amp;#10;  C --&amp;gt; M[Process];&amp;#10;  C --&amp;gt; N[Process Boundary];&amp;#10;  C --&amp;gt; O[Temporal Region];&amp;#10;&lt;/pre&gt;
&lt;p&gt;With BFO providing an underlying structure, this ontology encompasses fundamental categories and principles, enabling better communication and synergistic understanding among professionals.&lt;/p&gt;
&lt;p&gt;The hierarchical composition of BFO distinguishes major ontological categories, reflective of our comprehension of reality:&lt;/p&gt;
&lt;h3 id=&quot;entity&quot; tabindex=&quot;-1&quot;&gt;Entity&lt;/h3&gt;
&lt;p&gt;As the overarching category in BFO, entities encompass every conceivable existence, which is bifurcated into:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Continuants&lt;/strong&gt;: Entities retaining their identity across time, including:
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Material Entities&lt;/strong&gt;: Tangible objects like cells and tissues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Immaterial Entities&lt;/strong&gt;: Non-physical elements like policies or ideas.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Occurrents&lt;/strong&gt;: Time-bound occurrences, for example, biological processes like cell division.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;dependent-continuants&quot; tabindex=&quot;-1&quot;&gt;Dependent Continuants&lt;/h3&gt;
&lt;p&gt;These entities necessitate the existence of others for their survival, incapable of standalone presence. They are differentiated into:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Specifically Dependent Continuant (SC)&lt;/strong&gt;: Qualities tethered to a singular entity, such as a car&#39;s color.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Generically Dependent Continuant (GC)&lt;/strong&gt;: Traits applicable across several instances, like the shared feature of &amp;quot;redness&amp;quot; in all red objects.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;independent-continuants&quot; tabindex=&quot;-1&quot;&gt;Independent Continuants&lt;/h3&gt;
&lt;p&gt;Entities within this category can exist autonomously, which includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objects&lt;/strong&gt;: Individual material items, like a particular book.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object Aggregates&lt;/strong&gt;: Assemblages of objects, such as a book collection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fiat Objects&lt;/strong&gt;: Definitions based more on relational contexts than on innate physical characteristics, such as a designated parking space.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;qualities&quot; tabindex=&quot;-1&quot;&gt;Qualities&lt;/h3&gt;
&lt;p&gt;Observable or quantifiable characteristics of entities fall into this category, with illustrations like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Disposition&lt;/strong&gt;: A latent tendency to manifest a condition under specific circumstances, such as fragility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Relational Quality&lt;/strong&gt;: Attributes elucidating relationships among entities, like a parental connection.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;process&quot; tabindex=&quot;-1&quot;&gt;Process&lt;/h3&gt;
&lt;p&gt;Processes embody temporal developments or changes, which are definable through their involved entities and chronology. Key sub-categories include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Process Boundary&lt;/strong&gt;: Markers denoting the commencement or termination of processes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;History&lt;/strong&gt;: Documented sequences of events across a temporal expanse.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;regions&quot; tabindex=&quot;-1&quot;&gt;Regions&lt;/h3&gt;
&lt;p&gt;Regions cater to the demarcation of spatial or temporal planes where entities reside or events transpire:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spatial Regions&lt;/strong&gt;: Three-dimensional spaces occupied by material entities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temporal Regions&lt;/strong&gt;: Linear time segments encompassing occurrences.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;extensions-to-bfo&quot; tabindex=&quot;-1&quot;&gt;Extensions to BFO&lt;/h3&gt;
&lt;p&gt;To augment BFO&#39;s core ontology, &lt;strong&gt;metaclasses&lt;/strong&gt;, which classify other classes, can be incorporated. Additionally, the introduction of &lt;strong&gt;variable-order classes&lt;/strong&gt;—abstract constructs designating classes whose exemplars may span various class orders or even include non-classes—is noteworthy. Unique to variable-order classes is their classification as a subclass and superclass of themselves.&lt;/p&gt;
&lt;p&gt;For example, the hierarchy of a &amp;quot;Barack Obama&amp;quot; is:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lower Ontology&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;quot;Barack Obama&amp;quot; instance of &amp;quot;human&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;human&amp;quot; subclass of &amp;quot;person&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;person&amp;quot; subclass of &amp;quot;individual&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;individual&amp;quot; subclass of &amp;quot;agent&amp;quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Upper Ontology&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;quot;agent&amp;quot; subclass of &amp;quot;independent continuant&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;independent continuant&amp;quot; subclass of &amp;quot;continuant&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;continuant&amp;quot; subclass of &amp;quot;entity&amp;quot;&lt;/li&gt;
&lt;li&gt;&amp;quot;entity&amp;quot; instance of &amp;quot;class&amp;quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With that you can categorize everything. Well, almost everything—as Hamlet might, &amp;quot;There are more things in Heaven and Earth than are dreamt of in your ontology.&amp;quot; But BFO comes impressively close.&lt;/p&gt;
</description>
      <pubDate>Sun, 21 Jul 2024 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/bfo/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/bfo/</guid>
    </item>
    
    <item>
      <title>The AI Backlash is to be Expected</title>
      <description>&lt;h1 id=&quot;the-ai-backlash-is-to-be-expected&quot; tabindex=&quot;-1&quot;&gt;The AI Backlash is to be Expected&lt;/h1&gt;
&lt;p&gt;The rising tide of skepticism, criticism, and outright hostility towards artificial intelligence, particularly noticeable in left-leaning circles, should come as no surprise to anyone. While the potential applications of AI are still being researched and debated, the context in which it is emerging, the nature of its proponents, the audacity of their claims, and the ethical shadows surrounding its development have created a perfect storm for backlash. This reaction, while sometimes prone to overstatement, is a natural and largely predictable consequence of the AI industry&#39;s own actions and the broader technological landscape of the past decade. It stems not merely from Luddite fear, but from a well-earned distrust cultivated by recent history and the specific characteristics of the current AI boom.&lt;/p&gt;
&lt;p&gt;Firstly, the ground for skepticism towards grand technological promises has been thoroughly salted over the last ten years. We have witnessed a parade of innovations heralded as transformative, only to reveal themselves as predicated on shaky foundations, outright fraud, or producing deeply damaging externalities. Social media platforms, once envisioned as tools for connection and democracy, have become engines of polarization, misinformation, mental health crises and even &lt;a href=&quot;https://www.bbc.co.uk/news/world-asia-46105934&quot;&gt;genocides&lt;/a&gt;. Cryptocurrencies, pitched as decentralized financial liberation, devolved into speculative bubbles rife with scams and environmental concerns. The much-hyped metaverse remains a clunky, sparsely populated digital ghost town. The gig economy, sold as entrepreneurial freedom, often translated into precarious labor with eroded worker protections. And hovering over all tech hype is the cautionary tales of Theranos and WeWork, a stark reminder of how easily venture capital enthusiasm and bold claims can mask malfeasance. Against this backdrop, the emergence of AI, accompanied by similarly breathless predictions of revolutionizing everything, inevitably triggers societal antibodies wary of the next over-promised, under-delivered, and potentially harmful technological wave. The tech industry has undeniably done some horrible, downright evil, things over the last decade; the benefit of the doubt has been squandered by predecessors, leaving AI to face a far more critical reception which is entirely deserved.&lt;/p&gt;
&lt;p&gt;Adding significantly to this distrust are the often extreme and unsettling philosophical and political viewpoints espoused by many leading figures within the AI development sphere, particularly in Silicon Valley. It is jarring for the general public to learn that individuals wielding influence over hyped technology harbor beliefs far outside the mainstream Overton window. Many prominent AI proponents openly subscribe to long-termist or effective altruist philosophies that can lead to startling conclusions, prioritizing hypothetical future generations or digital consciousness over the pressing needs of currently living humans. There exists a genuine, albeit niche, belief among some tech elites that humanity&#39;s destiny lies in creating artificial successors—superintelligent AIs or posthumans—effectively passing the torch of existence to non-biological entities. This &lt;a href=&quot;https://ai-2027.com/&quot;&gt;eschatological vision&lt;/a&gt;, viewing AI not just as a tool but as the next stage of evolution or even a path to digital godhood, is deeply alienating and frankly alarming to those grounded in humanist values and concerned with present-day social and ecological challenges. Such pronouncements make the entire enterprise feel less like responsible innovation and more like a scifi-fantasy cult pursuing a bizarre, unrelatable endgame.&lt;/p&gt;
&lt;p&gt;This sense of alienation is further compounded by the political ideologies prevalent among many tech and AI figureheads, often fused with a potent strain of self-aggrandizement reflecting beliefs far outside the societal mainstream. While Silicon Valley is not monolithic, a strong current of libertarian, anarcho-capitalist, and anti-democratic sentiment runs through its influential core. This isn&#39;t merely abstract political theory; for some prominent figures, it appears rooted in a self-conception of themselves as Nietzschean supermen or Randian heroes—exceptional individuals seemingly believing they operate beyond conventional ethical constraints, destined to forge progress through sheer will and intellect, unburdened by the concerns or consent of the masses. Consequently, their championing of radical deregulation, minimal government, and market-based solutions for nearly all societal problems clashes sharply not just with mainstream political thought. When figures shaping tech&#39;s future express disdain for democratic processes as inefficient obstacles, champion elitist notions of governance, or advocate for economic systems that exacerbate inequality, it naturally breeds suspicion, appearing as the logical extension of a worldview that holds certain individuals as inherently superior and unbound by traditional social contracts. The perception arises that AI is being developed to further concentrate power and wealth in the hands of a techno-elite who see themselves as operating outside, and perhaps above, the democratic and egalitarian principles valued by civil society. This perceived anti-democratic, anti-egalitarian streak, amplified by an air of self-appointed exceptionalism, makes the industry &lt;a href=&quot;https://youtu.be/5MWT_doo68k?t=174&quot;&gt;profoundly unlikeable and untrustworthy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The sheer hyperbole and often prima facie absurdity of the claims made by AI evangelists also contribute significantly to the backlash. Statements about &amp;quot;building god,&amp;quot; &amp;quot;curing all diseases,&amp;quot; achieving immortality, or ushering in an era of unimaginable abundance sound less like realistic technological roadmaps and more like messianic prophecies. Crucially, there is no clear, demonstrable path from the current state of AI—largely sophisticated pattern-matching engines like LLMs—to these grandiose outcomes. This disconnect fuels comparisons to previous manias, amplified by the quasi-religious fervor surrounding the technology. When faced with claims that seem unmoored from scientific reality, coupled with the unsettling philosophies mentioned earlier, it is natural for observers to suspect either profound delusion or deliberate misrepresentation on a massive scale. The industry&#39;s reliance on such extreme rhetoric makes it difficult to take its more modest, potentially realistic claims seriously, poisoning the well of public trust.&lt;/p&gt;
&lt;p&gt;Furthermore, the very foundation of many current AI models is built upon practices that invite ethical and legal challenges, feeding directly into the backlash. The training of large AI models has involved the ingestion of vast amounts of data scraped from the internet, including copyrighted text, images, and code, often without the explicit consent or compensation of the original creators. While the legal definition of &amp;quot;fair use&amp;quot; is being actively debated, the perception among artists, writers, and many others is one of profound unfairness—large corporations profiting from the wholesale appropriation of creative work. This perception of intellectual property theft is particularly galling when paired with simultaneous pronouncements about AI&#39;s potential to displace workers in precisely those creative fields. The narrative becomes one of an industry harvesting the collective work of humanity without permission, only to turn around and automate the jobs of those whose work they used. This combination of perceived IP theft, data exploitation, and job displacement threat is a potent recipe for generating resentment and opposition.&lt;/p&gt;
&lt;p&gt;Beyond the ethical and philosophical objections, the current AI boom exhibits worrying hallmarks of a classic economic bubble, adding another potent ingredient to the cauldron of public skepticism. The sheer volume of capital flooding into AI—evidenced by soaring valuations Nvidia, frantic investment rounds for startups promising the next breakthrough, and massive infrastructure spending by tech giants—mirrors the speculative manias of the past, from dot-com exuberance to the pre-2008 housing market. There is a growing concern that much of this represents significant malinvestment, with capital chasing hype and future promises rather than demonstrable, sustainable business models or clear paths to profitability for many ventures built on current AI capabilities. Given the scale of these investments and the central role of major corporations and pension funds, the potential deflation of this bubble carries non-trivial systemic risk. A significant downturn in AI-related valuations or a wave of startup failures could ripple through interconnected markets, impacting everything from cloud computing providers to the wider stock market, reinforcing the perception that AI, like previous tech waves, might bring not just disruption, but also dangerous market instability fuelled by unrealistic expectations. This speculative frenzy, detached from immediate economic realities for many applications, only serves to heighten suspicion and contributes to the sense that the AI project is driven more by financial engineering than by economically relevant innovation.&lt;/p&gt;
&lt;p&gt;Crucially, beyond the sociological and political dimensions of the backlash, there lies a substantive argument that the negative externalities of current AI technologies may genuinely outweigh their demonstrated benefits, potentially justifying far stricter controls or even pauses in deployment. The significant environmental footprint associated with training and running massive AI models, demanding vast amounts of energy and resources, cannot be ignored in an age acutely aware of climate change. Simultaneously, these systems threaten to profoundly exacerbate the post-truth epistemic crisis, flooding digital spaces with synthetically generated content—often described as &amp;quot;not even wrong slop&amp;quot;—that further erodes shared reality and makes discerning reliable information incredibly difficult. The potential for AI to power hyper-personalized, automated anti-democratic disinformation campaigns at unprecedented scale is a palpable threat. Added to this is the fundamental issue of unreliability, manifested in the persistent problem of &amp;quot;hallucinations&amp;quot; where models confidently invent false information, presenting potentially existential risks to businesses or individuals who might rely on their outputs for critical decisions. When weighing these substantial and already visible harms against the currently realized, dependable use cases—which, while existing, often fall short of the revolutionary hype—a strong case can be made that the negative externalities presently dominate the equation, lacking sufficient compensatory value to rationalize the mounting societal and environmental costs.&lt;/p&gt;
&lt;p&gt;However, it is also true that the backlash, like many reactions, can sometimes manifest as an overcorrection. While the skepticism is understandable, claims that current AI technologies have &amp;quot;no uses&amp;quot; or are purely hype ignore the lived experiences of many. There exists a significant segment of the modern economy engaged in what anthropologist David Graeber termed &amp;quot;bullshit jobs&amp;quot;—roles in middle management, PR, corporate consulting, content marketing, and administration that often involve generating reports, presentations, and communications that feel devoid of intrinsic value; essentially, producing human-generated &amp;quot;slop.&amp;quot; For individuals in these roles, AI tools like can demonstrably streamline the production of this output, automating tedious tasks like drafting emails, summarizing documents, or generating boilerplate content. One might morally object to the existence of these jobs or the value of their output, but that is distinct from denying the reality that AI is being adopted to make their execution more efficient. Similarly, the widespread uptake of AI coding assistants for generating boilerplate code, suggesting solutions, and speeding up development workflows is undeniable among programmers.  &lt;a href=&quot;https://www.nature.com/articles/s41586-021-03819-2&quot;&gt;One&lt;/a&gt; &lt;a href=&quot;https://www.mrs.org/meetings-events/annual-meetings/archive/meeting/presentations/view/2024-fall-meeting/2024-fall-meeting-4151234&quot;&gt;must&lt;/a&gt; &lt;a href=&quot;https://pmc.ncbi.nlm.nih.gov/articles/PMC11984503/&quot;&gt;also&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41698-024-00789-2&quot;&gt;realize&lt;/a&gt; &lt;a href=&quot;https://chemrxiv.org/engage/api-gateway/chemrxiv/assets/orp/resource/item/66a8c11bc9c6a5c07a7a59c0/original/a-multi-agent-driven-robotic-ai-chemist-enabling-autonomous-chemical-research-on-demand.pdf&quot;&gt;that&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-023-06735-9&quot;&gt;all&lt;/a&gt; &lt;a href=&quot;https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2024JH000136&quot;&gt;of&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-021-03819-2&quot;&gt;the&lt;/a&gt; &lt;a href=&quot;https://www.mrs.org/meetings-events/annual-meetings/archive/meeting/presentations/view/2024-fall-meeting/2024-fall-meeting-4151234&quot;&gt;researchers&lt;/a&gt; &lt;a href=&quot;https://pmc.ncbi.nlm.nih.gov/articles/PMC11984503/&quot;&gt;in&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41698-024-00789-2&quot;&gt;acadamia&lt;/a&gt; &lt;a href=&quot;https://chemrxiv.org/engage/api-gateway/chemrxiv/assets/orp/resource/item/66a8c11bc9c6a5c07a7a59c0/original/a-multi-agent-driven-robotic-ai-chemist-enabling-autonomous-chemical-research-on-demand.pdf&quot;&gt;working&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-023-06735-9&quot;&gt;on&lt;/a&gt; &lt;a href=&quot;https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2024JH000136&quot;&gt;this&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-021-03819-2&quot;&gt;stuff&lt;/a&gt; &lt;a href=&quot;https://www.mrs.org/meetings-events/annual-meetings/archive/meeting/presentations/view/2024-fall-meeting/2024-fall-meeting-4151234&quot;&gt;are&lt;/a&gt; &lt;a href=&quot;https://pmc.ncbi.nlm.nih.gov/articles/PMC11984503/&quot;&gt;not&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41698-024-00789-2&quot;&gt;labouring&lt;/a&gt; &lt;a href=&quot;https://chemrxiv.org/engage/api-gateway/chemrxiv/assets/orp/resource/item/66a8c11bc9c6a5c07a7a59c0/original/a-multi-agent-driven-robotic-ai-chemist-enabling-autonomous-chemical-research-on-demand.pdf&quot;&gt;under&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-023-06735-9&quot;&gt;some&lt;/a&gt; &lt;a href=&quot;https://agupubs.onlinelibrary.wiley.com/doi/full/10.1029/2024JH000136&quot;&gt;false&lt;/a&gt; &lt;a href=&quot;https://www.nature.com/articles/s41586-021-03819-2&quot;&gt;consciousness&lt;/a&gt;. As the 18th century philosopher David Hume noted, we must be careful not to confuse an &amp;quot;ought&amp;quot; (these uses are trivial, undesirable, or symptomatic of a flawed economy) with an &amp;quot;is&amp;quot; (these uses exist and people are adopting them). While the advent of transformers will almost certainly not herald the arrival of artificial general intelligence (whatever that means?) or solve world hunger, and the long-term profitability of most of these businesses remains uncertain, they do represent some real, existing use cases. Denying their existence entirely weakens the credibility of the broader critique.&lt;/p&gt;
&lt;p&gt;It is also crucial to acknowledge the environment in which much of this backlash manifests and intensifies: online social spaces. Platforms like Bluesky and LinkedIn, and various forums are notoriously poor venues for nuanced discussion on complex topics. Instead, the dynamics of engagement optimization often reward hyperbole and hyper-partisanship. Strongly worded, pithy, maximally critical (or maximally boosterish) takes generate more likes, reposts, and replies—both from an in-group eager to signal agreement and from an out-group provoked into flame wars, which ironically also boosts visibility. Consequently, the discourse surrounding AI in these spaces frequently flattens complex issues into binary conflicts, devoid of caveats or acknowledgements of conflicting truths. This environment actively discourages any rational debate, which is precisely why I don&#39;t engage with these spaces anymore. Given these dynamics, it is entirely predictable that many online communities will likely maintain a staunchly anti-AI posture for the foreseeable future.&lt;/p&gt;
&lt;p&gt;As a counter-critique, however, there is often a noticeable gap between impassioned rhetoric and the articulation of concrete, legally precise policy solutions. While the posturing against AI&#39;s perceived dangers is loud, the detailed &amp;quot;how&amp;quot; of addressing these concerns frequently remains vague. For instance, does the opposition translate into a desire to outright ban the development or use of certain types of neural networks? Such a proposal faces immense practical hurdles and likely insurmountable First Amendment challenges, given that algorithms are essentialy just math. Conversely, proposing substantial fines and robust enforcement mechanisms against companies deploying AI trained on copyrighted works without consent or licensing is a far more legally tractable, though still complex, avenue. The real policy debate needs to engage with further concrete possibilities: should there be mandatory public disclosure and auditing requirements for the datasets used to train large commercial AI models to address bias and IP concerns? Could regulations establish clear liability frameworks, determining who is legally responsible when autonomous systems cause harm? Perhaps policy should focus on economic mitigation, such as imposing targeted taxes on automation that demonstrably displaces human labour, with revenues dedicated to the social safety net or worker retraining programs. These are the kinds of specific, actionable proposals needed to move the conversation beyond aimless generalized outrage towards concrete bills that could be enshrined in law.&lt;/p&gt;
&lt;p&gt;I won&#39;t tell you what to think on AI, you have to make up your mind around complex ethics issues like eating meat, carbon footprint, and social media. But I will say that the backlash against artificial intelligence, especially within circles sensitive to economic inequality and corporate overreach, is not a random or irrational phenomenon. It is an entirely predictable consequence of a decade scarred by tech disappointments and malfeasance, amplified by the unsettling ideologies and political stances of AI&#39;s leading figures. The industry&#39;s penchant for reality-bending hyperbole, coupled with legitimate concerns about intellectual property, data privacy, and job security, has created an environment where distrust is and &lt;em&gt;should&lt;/em&gt; be the default position. While some critiques may overstate the case by denying any utility to current AI tools, the core reasons for the backlash are deeply rooted in the industry&#39;s own presentation and practices. By cultivating an image characterized by unlikeable leaders, bizarre eschatological pronouncements, anti-democratic leanings, and ethically questionable methods, the AI industry has largely engineered its own public relations problem. It should not wonder why it faces skepticism and hostility; it is, in many ways, a backlash of its own making.&lt;/p&gt;
</description>
      <pubDate>Mon, 04 Mar 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ai_backlash/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ai_backlash/</guid>
    </item>
    
    <item>
      <title>Volatility Surface</title>
      <description>&lt;h1 id=&quot;volatility-surface&quot; tabindex=&quot;-1&quot;&gt;Volatility Surface&lt;/h1&gt;
&lt;p&gt;We&#39;re going to use Python to generate an implied volatility surface for a family of options contracts. This is an extremely common tool for analyzing options and is a key component of many quantitative trading strategies.&lt;/p&gt;
&lt;p&gt;The Black-Scholes model is like a recipe for pricing options - it takes ingredients like the current stock price, strike price, time until expiration, interest rates, and volatility, and spits out what an option should theoretically cost. But in the real world, traders often work backwards - they look at actual market prices of options and try to figure out what volatility number would make the Black-Scholes formula give that price. This reverse-engineered number is called the &lt;em&gt;implied volatility&lt;/em&gt; or IV.  Think of implied volatility as the market&#39;s forecast of how much a stock might bounce around in the future. Higher IV means traders expect bigger price swings (and are willing to pay more for options as insurance), while lower IV suggests they expect smoother sailing ahead. It&#39;s like a fear gauge - when investors are nervous about the future, they&#39;ll pay more for options protection, driving up IVs.&lt;/p&gt;
&lt;p&gt;What makes this really interesting is that options with different strike prices and expiration dates often have different IVs, even for the same underlying stock. When you plot these IVs, you get what&#39;s called the &lt;em&gt;volatility surface&lt;/em&gt; - a 3D visualization that shows how the market&#39;s expectations of future volatility vary across strikes and time. This surface often shows patterns like the &lt;em&gt;volatility smile&lt;/em&gt; or &lt;em&gt;volatility skew&lt;/em&gt;, where out-of-the-money puts have higher IVs than calls, reflecting the market&#39;s tendency to worry more about crashes than rallies. And we can use this surface to identify inefficiencies and arbitrage opportunities.&lt;/p&gt;
&lt;p&gt;In order to generate the volatility surface, we need to install a few libraries. These are mostly standard except for the py_vollib library which is a Python wrapper for the &lt;a href=&quot;https://vollib.org/&quot;&gt;VolLib&lt;/a&gt; library for doing efficient Black-Scholes calculations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-shell&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; numpy pandas seaborn matplotlib py_vollib
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; pd
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; seaborn &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; sns
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matplotlib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pyplot &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; plt
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; matplotlib &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cm
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; mpl_toolkits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mplot3d &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Axes3D
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; scipy&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;interpolate &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; interpolate
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; py_vollib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;black_scholes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implied_volatility &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; iv
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; py_vollib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;black_scholes &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; black_scholes &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; bs
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need to load the price data for the underlying asset and the strike data for the options chain. In practice we would load this data from Bloomberg or some other vendor. But for now we&#39;ll just load it from CSV files. The data consits of a chain of options contracts for the underlying asset &lt;code&gt;SPY&lt;/code&gt;. We have the strike price, price, and expiration date for each contract. From this we can use the Black-Scholes formula to calculate the implied volatility for each contract.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;contractSymbol&lt;/th&gt;
&lt;th&gt;lastTradeDate&lt;/th&gt;
&lt;th&gt;strike&lt;/th&gt;
&lt;th&gt;lastPrice&lt;/th&gt;
&lt;th&gt;inTheMoney&lt;/th&gt;
&lt;th&gt;currency&lt;/th&gt;
&lt;th&gt;optionType&lt;/th&gt;
&lt;th&gt;expiration&lt;/th&gt;
&lt;th&gt;daysToExpiration&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00598000&lt;/td&gt;
&lt;td&gt;2024-11-26 20:57:08+00:00&lt;/td&gt;
&lt;td&gt;598.0&lt;/td&gt;
&lt;td&gt;10.0&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00599000&lt;/td&gt;
&lt;td&gt;2024-11-26 21:08:56+00:00&lt;/td&gt;
&lt;td&gt;599.0&lt;/td&gt;
&lt;td&gt;9.4&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00600000&lt;/td&gt;
&lt;td&gt;2024-11-26 21:11:54+00:00&lt;/td&gt;
&lt;td&gt;600.0&lt;/td&gt;
&lt;td&gt;8.8&lt;/td&gt;
&lt;td&gt;True&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00601000&lt;/td&gt;
&lt;td&gt;2024-11-26 20:50:47+00:00&lt;/td&gt;
&lt;td&gt;601.0&lt;/td&gt;
&lt;td&gt;8.06&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00602000&lt;/td&gt;
&lt;td&gt;2024-11-26 20:57:59+00:00&lt;/td&gt;
&lt;td&gt;602.0&lt;/td&gt;
&lt;td&gt;7.39&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SPY241227C00602500&lt;/td&gt;
&lt;td&gt;2024-11-26 21:00:20+00:00&lt;/td&gt;
&lt;td&gt;602.5&lt;/td&gt;
&lt;td&gt;7.05&lt;/td&gt;
&lt;td&gt;False&lt;/td&gt;
&lt;td&gt;USD&lt;/td&gt;
&lt;td&gt;call&lt;/td&gt;
&lt;td&gt;2024-12-27 23:59:59&lt;/td&gt;
&lt;td&gt;31&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Recall that the Black-Scholes formula for a call option is given by:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
C = S &#92;cdot N(d_1) - K &#92;cdot e^{-rT} &#92;cdot N(d_2) &#92;&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
d_1 = &#92;frac{&#92;ln(S/K) + (r + &#92;sigma^2/2)T}{&#92;sigma&#92;sqrt{T}},&#92;quad d_2 = d_1 - &#92;sigma&#92;sqrt{T}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(C&#92;) is the price of the call option&lt;/li&gt;
&lt;li&gt;&#92;(S&#92;) is the current price of the underlying asset&lt;/li&gt;
&lt;li&gt;&#92;(K&#92;) is the strike price of the option&lt;/li&gt;
&lt;li&gt;&#92;(r&#92;) is the risk-free interest rate (i.e. the yield on a 3-month US Treasury bill)&lt;/li&gt;
&lt;li&gt;&#92;(&#92;sigma&#92;) is the volatility of the underlying asset&lt;/li&gt;
&lt;li&gt;&#92;(T&#92;) is the time until expiration&lt;/li&gt;
&lt;li&gt;&#92;(N(x)&#92;) is the cumulative distribution function of the standard normal distribution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Out of Black-Sholes we get the concept of &lt;em&gt;moneyness&lt;/em&gt; that describes the relationship between the current price of the underlying asset and the strike price of the option. An option is considered &lt;em&gt;in the money&lt;/em&gt; (often abbreviated ITM or the opposite which is OTM or out of the money) when it has intrinsic value, meaning that exercising the option would lead to a positive cash flow. For call options, this occurs when the strike price is lower than the current market price of the underlying asset, allowing the holder to buy the asset at a lower price than its market value. Conversely, for put options, an option is ITM when the strike price is higher than the current market price, enabling the holder to sell the asset at a price greater than its market value.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;In-the-Money (ITM)&lt;/em&gt;: Options that have intrinsic value. For calls, the strike price is below the current market price; for puts, it&#39;s above.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;At-the-Money (ATM)&lt;/em&gt;: Options where the strike price is approximately equal to the current market price of the underlying asset.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Out-of-the-Money (OTM)&lt;/em&gt;: Options that have no intrinsic value. For calls, this means the strike price is above the current market price of the underlying asset; for puts, it&#39;s below.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ITM options typically have higher premiums due to their intrinsic value, making them more desirable for traders who anticipate further favorable movements in the underlying asset&#39;s price. Grossly, the risk associated with ITM options is also higher, as they can be more sensitive to changes in market conditions. And generally OTM options are cheaper but have a higher probability of expiring worthless.&lt;/p&gt;
&lt;p&gt;We plug our market data into the Black-Scholes formula to calculate the implied volatility for each option in the chain. For the risk-free rate we use the yield on a 3-month US Treasury bill, replace this with the actual risk-free rate at present for the option you are analyzing.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_current_market_data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    spy_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read_csv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;PRICE_CSV&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    current_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; spy_price&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Close&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    risk_free_rate &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 3-month Treasury yield (4.3% as of 2024-11-27)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; spy_price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current_price


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculate_iv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sigma&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            S&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lastPrice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            K&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;strike&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            t&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;t&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            sigma&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;sigma&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            flag&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; iv&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implied_volatility&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            price&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            S&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lastPrice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            K&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;strike&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            t&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;t&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            r&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            flag&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error calculating IV for &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nan
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The volatility surface is a three-dimensional representation of implied volatility across different strike prices and expiration dates. It provides a comprehensive view of how the market prices options across various strikes and maturities, revealing important patterns in market expectations and risk preferences. In a perfect theoretical Black-Scholes world, this surface would be completely flat, as volatility would be constant across all strikes and expirations. However, in reality, we observe various patterns and distortions in the surface.&lt;/p&gt;
&lt;p&gt;To construct a smooth and continuous volatility surface from discrete market data, we employ interpolation techniques. In our implementation, we use a bivariate spline interpolation method through SciPy&#39;s SmoothBivariateSpline. This approach fits a smooth surface through our observed implied volatility points while maintaining a balance between accuracy and smoothness. The smoothing parameter (s=0.1) controls how closely the surface fits the original data points - a smaller value creates a tighter fit to the data, while a larger value produces a smoother surface that might be less sensitive to market noise.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_volatility_surface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;calls_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    surface &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        calls_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;daysToExpiration&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;strike&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;impliedVolatility&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pivot_table&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            values&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;impliedVolatility&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; index&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;strike&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;daysToExpiration&quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare interpolation data&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; surface&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values
    y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; surface&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values
    X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;meshgrid&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    Z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; surface&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values

    &lt;span class=&quot;token comment&quot;&gt;# Create interpolation points&lt;/span&gt;
    x_new &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;linspace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    y_new &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;linspace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    X_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y_new &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;meshgrid&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_new&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Perform interpolation&lt;/span&gt;
    spline &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; interpolate&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SmoothBivariateSpline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        X&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; s&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    Z_smooth &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; spline&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y_new&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; X_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z_smooth


&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;plot_volatility_surface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;style&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;use&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;default&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    sns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_style&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;whitegrid&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;axes.grid&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    fig &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;figure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;figsize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ax &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add_subplot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; projection&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;3d&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;plot_surface&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cmap&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;viridis&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; alpha&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; linewidth&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; antialiased&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_xlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Days to Expiration&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_ylabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Strike Price&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_zlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Implied Volatility&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set_title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;SPY Volatility Surface&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ax&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;view_init&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elev&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; azim&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tight_layout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;show&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can run the main function to generate the volatility surface.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;volsurface&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Get market data&lt;/span&gt;
    risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; historical_price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_current_market_data&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Using current price: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;current_price&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Load and process calls data&lt;/span&gt;
    calls &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read_csv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;OPTIONS_CSV&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    calls&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;t&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; calls&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;daysToExpiration&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;365.0&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate historical volatility&lt;/span&gt;
    sigma &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; historical_price&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Close&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pct_change&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;std&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;252&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate implied volatilities&lt;/span&gt;
    calls&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;calculated_iv&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; calls&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; row&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; calculate_iv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;row&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; risk_free_rate&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sigma&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    calls &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; calls&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;calculated_iv&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create and plot surface&lt;/span&gt;
    X_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z_smooth &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; create_volatility_surface&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;calls&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plot_volatility_surface&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y_new&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z_smooth&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;flex justify-center flex-col items-center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/volsurf.png&quot; alt=&quot;Volatility Surface&quot; class=&quot;mx-auto mb-4&quot; /&gt;
    &lt;figcaption class=&quot;caption text-center mt-2&quot;&gt;The volatility surface shows the market&#39;s expectations of future volatility across various strike prices and expiration dates.&lt;/figcaption&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Volatility Arbitrage&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The surface shows the market&#39;s expectation of future volatility across different strikes and expirations. When there are irregularities or &amp;quot;bumps&amp;quot; in the surface, it may indicate mispriced options. This is a type of trading strategy called &lt;em&gt;volatility arbitrage&lt;/em&gt;. We&#39;re essentially bet on whether the market&#39;s expectations of how much a stock&#39;s price will move are too high or too low. Basically, you&#39;re trying to profit from the difference between what the market predicts for future volatility and what you believe it will actually be.&lt;/p&gt;
&lt;p&gt;We can identify areas where the market has mispriced options by looking for areas where the volatility changes abruptly as compared to the surrounding areas by calculating the gradient of the surface and looking for areas where the gradient is greater than a certain threshold.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Find areas where volatility changes abruptly&lt;/span&gt;
 &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;find_vol_arbitrage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;surface&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; threshold&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.02&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
      vol_gradients &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;gradient&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;surface&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      anomalies &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;where&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vol_gradients&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; threshold&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; anomalies
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we were to find a inefficiencies in the surface, we could then buy the underpriced option, sell the overpriced option and delta hedge with the underlying asset for a guaranteed profit. Of course, finding inefficiencies is not easy and requires a lot of work. For options on assets like SPY, the market is very efficient and thousands of other people are trying to find inefficiencies at near nanosecond speeds so your probability of success is very low.&lt;/p&gt;
&lt;div class=&quot;flex justify-center flex-col items-center&quot;&gt;
    &lt;img src=&quot;https://www.stephendiehl.com/images/anomoly.png&quot; alt=&quot;Volatility Surface Anomaly&quot; class=&quot;mx-auto mb-4&quot; /&gt;
    &lt;figcaption class=&quot;caption text-center mt-2&quot;&gt;An anomaly in the volatility surface.&lt;/figcaption&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Skew Trading&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;volatility skew&lt;/em&gt; refers to the difference in implied volatility between out-of-the-money options, at-the-money options, and in-the-money options. &lt;em&gt;Skew trading&lt;/em&gt; involves exploiting the discrepancies in implied volatilities across different option strikes. Traders analyze the skew to identify mispriced options and implement strategies that can profit from the expected normalization of these discrepancies.&lt;/p&gt;
&lt;p&gt;The volatility &amp;quot;smile&amp;quot; is a convex pattern in the volatility surface where implied volatilities are higher for both in-the-money ITM and OTM options compared to ATM options. This convexity, shaped like a smile or smirk when plotted, indicates that market participants are willing to pay more for options that are away from the current market price. The convex shape emerges because traders often seek protection against both upside and downside risks - buying OTM puts to hedge against market crashes and OTM calls to capture potential rallies. This behavior directly relates to our earlier discussion of volatility arbitrage, as the smile pattern can create opportunities for traders to exploit any temporary distortions in this typically stable relationship.&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;volatility smile&lt;/em&gt; is symmetric, with higher implied volatilities for both OTM puts and OTM calls compared to ATM options.&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;volatility smirk&lt;/em&gt; is asymmetric, with higher implied volatilities for OTM puts compared to OTM calls.&lt;/p&gt;
&lt;p&gt;Think of skew trading like betting on the market&#39;s fear levels evening out. When traders get really worried about market crashes, they drive up the prices of &amp;quot;insurance&amp;quot; (put options) compared to &amp;quot;lottery tickets&amp;quot; (call options). This creates a lopsided or &amp;quot;skewed&amp;quot; pattern in option prices. Skew traders try to make money by betting that this fear will either increase or decrease - they might buy the relatively cheap calls and sell the expensive puts if they think the market is being too paranoid, or do the opposite if they think people aren&#39;t worried enough. It&#39;s like being a fear arbitrageur, profiting when market psychology swings between extremes of panic and complacency.&lt;/p&gt;
&lt;p&gt;We can analyze the skew by performing a linear regression on the IV vs strike and looking for a trend.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;analyze_volatility_skew&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;options_data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Filter ATM options&lt;/span&gt;
    atm_strike &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; options_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;options_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; options_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;lastPrice&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;argsort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Calculate skew: IV vs Strike&lt;/span&gt;
    skew_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; options_data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;copy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike_diff&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; atm_strike
    skew_data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;~&lt;/span&gt;skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;impliedVolatility&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Perform linear regression to quantify skew&lt;/span&gt;
    slope&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; intercept&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r_value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p_value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; std_err &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; linregress&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike_diff&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;impliedVolatility&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Skew Slope: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;slope&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.5f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, R-squared: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;r_value&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.5f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;# Plot the skew&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;figure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;figsize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scatter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike_diff&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;impliedVolatility&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Options&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;plot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike_diff&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; intercept &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; slope &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; skew_data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;strike_diff&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;r&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Linear Fit&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;xlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Strike Difference (Strike - ATM Strike)&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ylabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Implied Volatility&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Volatility Skew Analysis&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;legend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;show&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; slope
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Fri, 01 Mar 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/volatility_surface/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/volatility_surface/</guid>
    </item>
    
    <item>
      <title>The Dark Eschatology of Crypto Ideologues</title>
      <description>&lt;h1 id=&quot;the-dark-eschatology-of-crypto-ideologues&quot; tabindex=&quot;-1&quot;&gt;The Dark Eschatology of Crypto Ideologues&lt;/h1&gt;
&lt;p&gt;In the shadowy corners of the internet, a new breed of financial doomsayers is emerging. Enabled by a rise in populism and mixed with a potent mix of libertarian ideals and apocalyptic fervor, cryptocurrency ideologues are crafting a narrative that goes far beyond mere speculation on digital nonsense. This isn&#39;t just about a new form of gambling; it&#39;s a full-fledged eschatology—a vision of economic end times that promises salvation through the collapse of our current financial order. While the media likes to pretend the crypto movement as a &amp;quot;cutting-edge&amp;quot; technological revolution, its roots run deep into a murky underworld of fringe economic theories and anti-establishment paranoia. From the ashes of our current economic system, these true believers envision a new world order rising—one where decentralized digital currencies reign supreme, traditional financial institutions crumble, and a select few visionaries (conveniently, themselves) emerge as the prophets of a new economic age. But as we peel back the layers of this crypto-fueled fever dream, we uncover a disturbing ideology that threatens not just our economic stability, but the very foundations of democratic society itself.&lt;/p&gt;
&lt;p&gt;Just as William Jennings Bryan thundered against the gold standard in his &amp;quot;Cross of Gold&amp;quot; speech at the 1896 Democratic National Convention, modern crypto enthusiasts often position themselves as crusaders against the perceived tyranny of fiat currency. However, while Bryan championed bimetallism to alleviate the plight of debt-ridden farmers and laborers during the Panic of 1893, today&#39;s crypto ideologues push for a complete decentralization of money, often with reckless disregard for the broader economic consequences. The concept of returning to a metallic standard for currency—a notion thoroughly discredited by economists since the Great Depression—has found new life in ultra-right wing circles. This neo-metallism, often intertwined with baroque conspiracy theories about central banks, global elites, and the &amp;quot;petrodollar,&amp;quot; sees cryptocurrencies as a digital proxy for gold. It&#39;s a worldview that rejects the nuanced realities of modern monetary policy in favor of simplistic, often ahistorical notions of &amp;quot;sound money,&amp;quot; conveniently ignoring the deflationary crises and economic instability that plagued the era of the classical gold standard.&lt;/p&gt;
&lt;p&gt;The cryptoanarchist vision extends far beyond mere alternative currencies, encompassing the creation of autonomous special economic zones that function as tax havens and regulatory-free utopias. This audacious concept seeks to establish isolated enclaves where crypto-enthusiasts can actualize their libertarian fantasies, unfettered by the constraints of traditional government oversight. The ill-fated Próspera project in Honduras stands as a stark exemplar of this neo-colonialist ambition. Despite the ignominious collapse of Próspera and similar ventures, there persists a robust ideological nexus between fervent enthusiasm for gold (and now, Bitcoin) and fringe economic theories, most notably those espoused by the Austrian School of economics. These heterodox ideologies vehemently reject mainstream economic orthodoxy, central banking institutions, and any form of government intervention in markets. Proponents of these views often prognosticate imminent economic cataclysm with an almost eschatological zeal, positioning their preferred assets as financial arks in a coming deluge of hyperinflation and monetary collapse. The irony is palpable: even as their own experiments in governance crumble under the weight of reality, these modern-day prophets of doom continue to peddle their apocalyptic economic gospel. Their vision of a stateless, crypto-powered future represents not just a reimagining of currency, but a wholesale rejection of the social contract that underpins modern civilization—a dangerous fantasy that, if realized, would result not in a libertarian paradise, but in a new form of digital feudalism or anarchy.&lt;/p&gt;
&lt;p&gt;Perhaps most disturbing is the strain of thought within some crypto circles that actively desires the collapse of the current economic and political order. This apocalyptic mindset sees the destruction of democratic institutions, the Community of Nations, and the U.S. economy not as a tragedy to be avoided, but as a necessary step towards establishing an authoritarian regime based on so-called &amp;quot;traditional values&amp;quot; and a new economic order. It&#39;s crucial to recognize that these ideas, while gaining traction in certain online communities, remain far outside the mainstream of American political thought. The desire to end democracy and establish an authoritarian regime is far outside the Overton window of acceptable political discourse. While healthy skepticism of elected officials, financial institutions, and monetary policy is essential, the apocalyptic visions and authoritarian leanings of certain crypto enthusiasts represent a genuine threat to our democratic norms and economic stability.&lt;/p&gt;
&lt;p&gt;At its core, the crypto-ideology embodies a peculiar form of aristo-populism. It appeals to populist sentiments by railing against established financial institutions and promising economic liberation for the masses. Yet, paradoxically, it often serves the interests of a new digital aristocracy – early adopters, tech billionaires, and crypto-evangelists who stand to benefit most from widespread adoption. This aristo-populist approach allows crypto enthusiasts to position themselves as champions of the common person while simultaneously advocating for a system that could potentially concentrate wealth and power in the hands of a new techno-elite. The result is a movement that purports to democratize finance while potentially entrenching new forms of economic inequality and social stratification.&lt;/p&gt;
&lt;p&gt;In contrast to the apocalyptic visions of some crypto circles, the mainstream American political traditional recognizes the strength and resilience of liberal democratic institutions and the US-led international order. This perspective acknowledges that while our systems may have flaws, they have provided unprecedented stability, prosperity, and individual freedoms. The US dollar&#39;s role as the world&#39;s primary reserve currency is not seen as a tool of oppression, but as a cornerstone of global economic stability, facilitating international trade and investment. We must recognize the importance of gradual reform and responsible innovation within existing frameworks, rather than advocating for their wholesale destruction. We must channel our healthy skepticism towards elected officials, financial institutions, and monetary policy into constructive civic engagement and policy debates. The goal is to strengthen and improve our democratic systems, not to dismantle them in favor of economically absurd anarcho-capitalist fever dreams.&lt;/p&gt;
&lt;p&gt;It is imperative that we, as citizens of democratic societies, forcefully reject these dangerous ideologies that threaten the very foundations of the state and democracy. Policymakers must enact forceful and overbearing regulation to stymie the efforts of crypto ideologues. We must confront the legitimate economic grievances that make these ideologies appealing, not through false promises of digital utopianism, but through sound policy grounded in democratic principles and the rule of law. The crypto-ideologues&#39; vision of a collapsed state and anarchic international order is not just misguided—it&#39;s a direct assault on the values that have sustained our republic for generations. We must stand firm in our commitment to democratic institutions, equitable economic policies, and the primacy of law over the whims of digital aristocrats. We must push back against the false promises of digital utopianism and the dangerous fantasies of a new authoritarian order. Our financial system should serve all citizens, not just a select few who dream of profiting from chaos. The path forward lies not in the fever dreams of crypto-anarchists, but in the slow and steady progress of democratic governance, international cooperation, responsible innovation, and a commitment to the supremacy of the democratic state and the rule of law.&lt;/p&gt;
</description>
      <pubDate>Fri, 01 Mar 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/eschatology/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/eschatology/</guid>
    </item>
    
    <item>
      <title>Silicon Valley&#39;s (totally not a cult) Ideologies</title>
      <description>&lt;h1 id=&quot;silicon-valley&#39;s-(totally-not-a-cult)-ideologies&quot; tabindex=&quot;-1&quot;&gt;Silicon Valley&#39;s (totally not a cult) Ideologies&lt;/h1&gt;
&lt;p&gt;Silicon Valley has a rich thriving intellectual ecosystem, just like a septic tank. But for the outsider, it can be hard to make sense of all the thriving intellectual flora taking bloom. So here is a guide to the major schools of thought dominating modern tech culture these days:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Neoreaction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Rich dudes who think democracy sucks and want kings back. They hate equality and progress, and think the Dark Ages were actually lit. Basically edgy contrarians who&#39;d rather bow to a monarch than deal with messy democracy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Effective Altruism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Rich nerds trying to feel better about themselves by throwing ill-gotten gains at problems. They crunch numbers to decide which causes are trendy enough to support. Basically a fancy way to say &amp;quot;I&#39;m helping!&amp;quot; while rationalizing fraud. But hey, at least some of that stolen FTX money went to charity. Turns out with enough utilitarian calculus, you can justify anything!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cypherpunks&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Privacy zealots who think encryption will save us from the surveillance state, even if it means burning down democracy in the process. They&#39;d rather see the world descend into crypto-anarchy than let the NSA read their anime fanfiction. Everything must be sacrificed on the altar of privacy - your convenience, your safety, even basic human cooperation. But hey, at least the government won&#39;t know what kind of hentai you&#39;re into while society collapses! They&#39;re basically digital doomsday preppers with PGP keys.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Longtermism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds with savior complexes who think they can save humanity by obsessing over calculations about hypothetical future people. As if apparently the billions alive now aren&#39;t enough to deal with. But hey, why solve the problems of real people today when you can pontificate about dyson spheres thirty thousand years in the future?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crypto-maximalists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Crypto bros who think they&#39;re saving the world from &amp;quot;central bankers&amp;quot; (totally not a dog whistle), but really they&#39;re just gambling addicts with a messiah complex. They&#39;ve mixed Austrian economics drivel with vaping and doomsday prepping, and think their magic internet money will save them from the collapse of society (and the internet) and make them into kings of the apocalypse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crypto Aristopopulism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Digital feudal lords selling lottery tickets to the precariat while calling it &amp;quot;financial empowerment.&amp;quot; They&#39;ve figured out how to monetize desperation by convincing people that gambling on magic internet money is their only escape from wage slavery. Nothing says late capitalism quite like tech oligarchs preaching &amp;quot;democratization of finance&amp;quot; while building elaborate Ponzi schemes to extract the last drops of wealth from the working class. They promise revolution but deliver digital company scrip, turning poverty into a play-to-earn game where the house always wins. At least the feudal lords of old were honest about owning everything - these ones do it while tweeting about &amp;quot;community&amp;quot; from their penthouse in Dubai.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Effectve Accelerationism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds on steroids who think AI will solve everything. They call skeptics &amp;quot;doomers&amp;quot; and jerk off to the idea of becoming space gods. Some Silicon Valley bigwigs jumped on the bandwagon after having a come-to-Roko&#39;s-Basilisk epiphany on the road to Burning Man and realized the crazy train shouldn&#39;t have any brakes. All aboard the Techno-Capital Extinction express!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Techno-presentism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think we&#39;re living through a crisis of historicity, where modernity has lost its sense of temporal orientation. They think we&#39;re stuck in a &amp;quot;presentist&amp;quot; regime - obsessed with the immediate and unable to imagine real futures or learn from the past. Their solution? Destroy liberalism and bring back those sweet, sweet grand narratives about humanity&#39;s destiny, but make it tech-bro. Think &amp;quot;Lord of the Rings&amp;quot; meets &amp;quot;Silicon Valley&amp;quot;, where democracy is totally passé and tech CEOs are the new philosopher kings. Because nothing says &amp;quot;solving the crisis of meaning&amp;quot; quite like LARPing as a Borgia prince while writing manifestos on Substack.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Thielism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think we&#39;re all just mimetic lemmings chasing after whatever everyone else wants and the only way out is the abstract idea of Jesus, but minus the whole &amp;quot;love thy neighbor&amp;quot; thing. And the only way to be truly original is to become a billionaire vampire who funds right-wing populists and builds apocalypse bunkers in New Zealand while warning that the UN is literally the antichrist. Because nothing says &amp;quot;I&#39;m not like other girls&amp;quot; quite like trying to destroy liberal democracy while prepping for the end times.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Muskism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Techno-feudalists who worship a billionaire shitposter as humanity&#39;s savior. Followers dream of becoming space peasants in Musk&#39;s Mars fiefdom while defending his divine right to manipulate markets on Twitter. It&#39;s like Silicon Valley created its own prosperity gospel where &amp;quot;disruption&amp;quot; means bringing back company towns and indentured servitude. They&#39;re basically speedrunning the creation of a corporate feudal state while calling it &amp;quot;saving humanity.&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Simulationists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think we&#39;re NPCs in a video game. Some hippie named Nick came up with their core argument after too many bong rips: either we can&#39;t make simulations, we don&#39;t wanna, or like ... we&#39;re in one dude. And if future nerds can make more epic video games indefinitely, we&#39;re probably in one. But it doesn&#39;t matter and we can&#39;t know, so just go touch grass and get a life.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Postliberalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you&#39;re tired of avocado toast and want to bring back the good ol&#39; days of community values and state-mandated morality. It&#39;s like liberalism, but with more Jesus and less weed. Proponents think free markets and individual rights are overrated, and what we really need is Big Brother telling us how to live our best lives. Comes in both leftist and right-wing flavors, united by a shared nostalgia for when people knew their place and didn&#39;t ask so many pesky questions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rationalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think they&#39;re Spock. They worship data and algorithms like a secular deity, convinced they can solve everything with enough hand-waving about Bayes theorem. Critics say they&#39;re missing the forest for the trees, ignoring pesky things like ethics and human emotions but you should never let the facts get in the way of a good regression. They love living in glorified frat houses, swapping partners like Pokémon cards, and having deep philosophical debates about expected value of eugenics while getting high.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crypto-libertarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Tinfoil hat enthusiasts who think Dogecoin and JPEGs of apes will solve world hunger and overthrow governments. They&#39;re super into market astrology (i.e. technical analysis), hate central banks, and worship at a shrine of Micahel Saylor in their parent&#39;s basement. For whatever the question is, the answer is &amp;quot;Bitcoin solves it!&amp;quot; Just don&#39;t ask them how to actually use it for anything useful.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Techno-libertarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think the internet should be a lawless Wild West. They hate government meddling and love &amp;quot;meritocracy&amp;quot; (aka the already rich calling the shots). Basically, they want a digital free-for-all with minimal rules and maximum profit potential while retreating into doomsday bunkers while the rest of the world burns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Corporate-libertarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Tech nerds who think crypto and algorithms will let them dodge taxes and regulations while they fantasize about Ayn Rand. They want tech billionaires running everything and dream of a world where money is god and pesky things like governments and unions are extinct. Basically, they&#39;re drunk on market fundamentalism and think the world would be far better off with Zuckerberg as god emperor of Earth.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solarpunk&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Hippies with solar panels who think we can save the world by hugging trees and building eco-communes. It&#39;s like cyberpunk, but replace the neon and dystopia with flowers and kumbaya. Basically, it&#39;s what happens when sci-fi nerds discover gardening and decide capitalism is, like, totally unsustainable, man.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Transhumanism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds obsessed with ditching their meat suits and turning themselves into walking iPhones. They can&#39;t wait to ditch their flesh and prehensile paws for the cold purity of chrome and circuits. One part tech cult, one part midlife crisis.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Millenarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think the end is always just around the corner, whether it&#39;s financial collapse or the second coming of cyber-Jesus. They spend their days writing manifestos about how [current year] is definitely THE year everything collapses. When the world doesn&#39;t end on schedule, they just move the goalposts and double down on their predictions. It&#39;s like a death cult for people with too much time on their hands and not enough therapy. At least the Heaven&#39;s Gate cult had the decency to pick one apocalypse and stick with it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Anarcho-primitivism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;Ooga booga, civilization bad! Let&#39;s ditch our phones and go live in caves!&amp;quot; These folks think we should abandon everything since the Stone Age. No more farming, no more tech, just vibing with nature and hoping we don&#39;t get eaten by bears. They blame agriculture for all our problems and think we&#39;d be happier if we just went back to hunting mammoths or whatever.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Techno-utopianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Tech nerds who think gadgets will save the world. They dream of a future where robots do all the work and we live forever. Sure, tech might cause some issues, but who cares? It&#39;s all sunshine and rainbows in their silicon-powered fantasyland. Just don&#39;t burst their bubble by mentioning things like history or actual human nature.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Declinism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think everything is getting worse and secretly hope it does. They doomscroll through collapse porn and write manifestos about how society is totally falling apart this time, for real. It&#39;s like they&#39;re trying to speedrun societal collapse by willing it into existence through sheer negativity. They&#39;ve turned hopelessness into an aesthetic and depression into an ideology. At least other doomers try to prep - these folks just want front row seats to watch it all burn.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extropianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think science will make them immortal. They&#39;re all about elevating the world&#39;s consciousness and expanding into the &amp;quot;noosphere&amp;quot;. It&#39;s like Scientology for techies who think the Kremlin is just misunderstood.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Singularitarianism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Nerds who think super-smart AI god is coming soon and we should make it play nice. Unlike other tech prophets, they actually want this to happen. Some even dedicate their lives to speeding it up &amp;quot;safely&amp;quot;, despite simultaneously believeing it will cause total extinction. It&#39;s like a religion, but with more cognitive dissonance and GPUs. Feel the AGI, Amen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AI Doomerism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Paranoid nerds who think AI god is just around the corner but think stoking fear generates more retweets. They spend their days frantically writing apocolpyse porn on Substack about how we&#39;re all going to die because ChatGPT memorized how many R&#39;s there are in the word &amp;quot;strawberry&amp;quot;. Their solution? More AI of course! Because nothing helps prevent fire like pouring gasoline on the fire.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Grey Tribe&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Self-proclaimed intellectual refugees from the culture war who are definitely not just libertarians. They&#39;ve optimized their way out of joy - surviving on Soylent and paleo while writing insufferable manifestos about how the NSA is literally 1984 and drugs should be legal (but only the ones they like). They uber everywhere while listening to folk songs about spaceships and pretending to understand Slate Star Codex posts. They&#39;re not left or right wing - they&#39;re just &lt;em&gt;above&lt;/em&gt; it all, which they&#39;ll remind you of constantly while passive-aggressively sighing about how identity politics is ruining their ability to be smugly contrarian about everything.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Trumpism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A cult of personality for orange-tinted narcissists. Mix one part racism, two parts conspiracy theories, and a dash of Christian hypocrisy. Shake vigorously with fear-mongering and serve chilled to gullible masses. Side effects may include crowd size dysfunction and severe cognitive dissonance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Techno-optimism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Technology will save us all! Just look at how smartphones solved world hunger and social media brought about world peace. Clearly, AI and gene editing will fix climate change, cure cancer, and make everyone rich. Who needs critical thinking when we can have 37 iterations of the same iPhone?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Synthetic Technocracy&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Fancy term for letting AI run everything. Basically, handing over the keys to Skynet and hoping it doesn&#39;t decide to redecorate with human skulls. Perfect for folks who dream of being brainless meat puppets in a Matrix-style pod, high on computer-supplied happy juice while robots play dress-up with their limp bodies. Living the dream, right?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Christian Nationalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Basically, Christian nationalists want Jesus and MAGA to have a baby, but in a totally heteronormative way. They&#39;re like fundamentalists on steroids, with a side of white power and a sprinkle of &amp;quot;God wrote the Constitution.&amp;quot; They&#39;re not quite the same as the Bible-thumpers who want pastors running the country. These folks think the Founding Fathers were basically God&#39;s stenographers, so they&#39;ll happily vote for a sleazeball if he promises to own the libs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Metamodernism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When hipsters got bored of being ironic and decided to be earnest again, but like, ironically. It&#39;s the cultural equivalent of wearing a vintage t-shirt under a tailored blazer while sipping artisanal coffee and listening to vinyl. Basically, it&#39;s trying to have your avocado toast and eat it too.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decentralizationists&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Imagine the internet, but with Ponzi schemes as a first class protocol. And also what if your browser history was on a permanent public blockchain so grandma could see what kind of kinky shit you&#39;re into? Decentralized protocol are just like regular tech, except they don&#39;t work and there&#39;s a 100% chance of losing all your money unless you have NSA-level opsec. Don&#39;t worry, though – your expensive broken link to a JPEG of a bored monkey will definitely be worth a trillion dollars ... someday.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Biohackers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When writing buggy Javascript isn&#39;t enough, so you decide to hack your own DNA the way nature intended. If you like like popping 50 supplements and think injecting yourself with CRISPR kits bought from strangers on eBay will turn you into a superhuman mutant, then biohacking is for you! Spoiler alert: at best it gives you expensive pee, at worst it gives you a free Darwin Award.&lt;/p&gt;
</description>
      <pubDate>Tue, 20 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ideologies_21st_century/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ideologies_21st_century/</guid>
    </item>
    
    <item>
      <title>Slippage Modelling</title>
      <description>&lt;h1 id=&quot;slippage-modelling&quot; tabindex=&quot;-1&quot;&gt;Slippage Modelling&lt;/h1&gt;
&lt;p&gt;When we&#39;re trading, we&#39;re not trading at the current market price, we&#39;re trading at the price the market will execute our order at. There&#39;s a difference between the price we see and the price we get. This difference is called &lt;em&gt;slippage&lt;/em&gt;. Predicting a priori the slippage is an empirical problem, but we can model it and do better than just guessing or completely avoiding it in our model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Naive Slippage Model&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When we place an order on the market that is outsized relative to the size of other orders, our order will be executed at a price different to the price we see. However we can analytically model our expected slippage given the recent price history of the asset and then size our orders to minimise the impact of slippage. This is a function of three components.&lt;/p&gt;
&lt;p&gt;The volatility of the asset is the main driver of slippage. The more volatile the asset, the more slippage we expect. We can model this by estimating the volatility of the asset from the recent price history and then using this to estimate the slippage. We can calculate the volatility using a simple rolling window average.&lt;/p&gt;
&lt;p&gt;The other component of slippage is the &lt;em&gt;volume impact factor&lt;/em&gt;. This is the amount the price will move due to the size of our order relative to the average daily volume. We can model this by estimating the average daily volume of the asset from the recent price history and then using this to estimate the slippage.&lt;/p&gt;
&lt;p&gt;The third component of slippage is the &lt;em&gt;spread&lt;/em&gt;. This is the difference between the bid and ask price. We can model this by estimating the spread from the recent price history of the asset. The total slippage can be modeled as the sum of these components:&lt;/p&gt;
&lt;p&gt;$$S = S_{&#92;text{base}} + S_{&#92;text{volume}} + S_{&#92;text{volatility}} + S_{&#92;text{spread}}$$&lt;/p&gt;
&lt;p&gt;where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(S_{&#92;text{base}} = P &#92;cdot &#92;frac{m}{10000}&#92;)&lt;/li&gt;
&lt;li&gt;&#92;(S_{&#92;text{volume}} = P &#92;cdot f_v &#92;cdot &#92;frac{Q}{V}&#92;)&lt;/li&gt;
&lt;li&gt;&#92;(S_{&#92;text{volatility}} = P &#92;cdot &#92;sigma &#92;cdot f_v&#92;)&lt;/li&gt;
&lt;li&gt;&#92;(S_{&#92;text{spread}} = s &#92;cdot f_s&#92;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(P&#92;) is the current price&lt;/li&gt;
&lt;li&gt;&#92;(f_v&#92;) is the volume impact factor&lt;/li&gt;
&lt;li&gt;&#92;(f_s&#92;) is the spread factor&lt;/li&gt;
&lt;li&gt;&#92;(Q&#92;) is the order size&lt;/li&gt;
&lt;li&gt;&#92;(V&#92;) is the average daily volume&lt;/li&gt;
&lt;li&gt;&#92;(&#92;sigma&#92;) is the volatility&lt;/li&gt;
&lt;li&gt;&#92;(s&#92;) is the bid-ask spread&lt;/li&gt;
&lt;li&gt;&#92;(m&#92;) is the minimum slippage in basis points&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; pd
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Union&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Optional

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SlippageModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volatility_window&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volume_impact_factor&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        spread_factor&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        min_slippage_bps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volatility_window &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; volatility_window
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volume_impact_factor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; volume_impact_factor
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;spread_factor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; spread_factor
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;min_slippage_bps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; min_slippage_bps

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;estimate_slippage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        price&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        order_size&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        avg_daily_volume&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volatility&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bid_ask_spread&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Base slippage on minimum bps&lt;/span&gt;
        base_slippage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; price &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;min_slippage_bps &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Volume impact component&lt;/span&gt;
        volume_ratio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; order_size &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; avg_daily_volume
        volume_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; price &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volume_impact_factor &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; volume_ratio

        &lt;span class=&quot;token comment&quot;&gt;# Volatility component (if provided)&lt;/span&gt;
        volatility_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; volatility &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            volatility_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; price &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; volatility &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volume_impact_factor

        &lt;span class=&quot;token comment&quot;&gt;# Spread component&lt;/span&gt;
        spread_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; bid_ask_spread &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            spread_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bid_ask_spread &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;spread_factor
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;# Estimate spread as 0.1% of price if not provided&lt;/span&gt;
            spread_impact &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; price &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;spread_factor

        &lt;span class=&quot;token comment&quot;&gt;# Combine all components&lt;/span&gt;
        total_slippage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; base_slippage &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; volume_impact &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; volatility_impact &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; spread_impact

        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; total_slippage

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculate_volatility&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; prices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        returns &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pct_change&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        volatility &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; returns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rolling&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;window&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volatility_window&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;std&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; volatility&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;estimate_trade_price&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        current_price&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        order_size&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        avg_daily_volume&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        is_buy&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volatility&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        bid_ask_spread&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Optional&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        slippage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_slippage&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            current_price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            order_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            avg_daily_volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            volatility&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            bid_ask_spread
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Add slippage for buys, subtract for sells&lt;/span&gt;
        execution_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_price &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slippage &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; is_buy &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;slippage&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; execution_price
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now to use this model we need to estimate the volatility and bid-ask spread of the asset. We&#39;ll use an equity-like asset for this example.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Initialize model&lt;/span&gt;
model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SlippageModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    volatility_window&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    volume_impact_factor&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    spread_factor&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    min_slippage_bps&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Example trade&lt;/span&gt;
current_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100.0&lt;/span&gt;
order_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;
avg_daily_volume &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000000&lt;/span&gt;
volatility &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.02&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 2% daily volatility&lt;/span&gt;
bid_ask_spread &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Estimate slippage for a buy order&lt;/span&gt;
slippage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_slippage&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    order_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    avg_daily_volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    volatility&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bid_ask_spread
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

execution_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_trade_price&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_price&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    order_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    avg_daily_volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    is_buy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    volatility&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;volatility&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bid_ask_spread&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;bid_ask_spread
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Current Price: $&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;current_price&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Estimated Slippage: $&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;slippage&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.4f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Execution Price: $&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;execution_price&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Slippage Impact (bps): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slippage&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;current_price&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Abdi and Ranaldo Model&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Abdi and Ranaldo (2017) model offers a comprehensive framework for estimating bid-ask spreads in financial markets. The model is grounded in the concept of efficient price variance, which reflects the underlying volatility of asset prices. By analyzing high, low, and closing prices, the model aims to provide a more accurate estimation of the bid-ask spread. At the core of the model is the calculation of the average price, denoted as:&lt;/p&gt;
&lt;p&gt;$$&#92;eta_t = &#92;frac{(h_t + h_l)}{2}$$&lt;/p&gt;
&lt;p&gt;for each trading period. This average price serves as a reference point for assessing price movements. The model then examines the variance of changes in these average prices, represented as &#92;(&#92;text{Var}(&#92;Delta &#92;eta)&#92;), to capture the dynamics of price fluctuations over time. To estimate the efficient price variance, the model uses the variance of the changes in the average price, adjusted by a constant factor. The efficient price variance is calculated as:&lt;/p&gt;
&lt;p&gt;$$&#92;sigma^2_{&#92;text{eff}} = &#92;frac{&#92;text{Var}(&#92;Delta &#92;eta)}{2 - &#92;frac{k_1}{2}}$$&lt;/p&gt;
&lt;p&gt;Where &#92;(k_1&#92;) is a constant derived from the model&#39;s theoretical foundations (see the derivation in the original paper). This adjustment accounts for the impact of market microstructure on price formation. Finally, the model derives the bid-ask spread using the relationship between the variance of closing prices and the efficient price variance. The spread (&#92;(S&#92;)) is estimated as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
S = &#92;sqrt{4 &#92;left( &#92;text{Var}(c_t - &#92;eta_{avg}) - &#92;left(0.5 + &#92;frac{k_1}{8}&#92;right) &#92;sigma^2_{&#92;text{eff}} &#92;right)}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This equation shows us how the spread is influenced by both the observed price movements and the underlying market conditions, providing traders with a robust tool for slippage estimation. For more details, refer to the original paper &lt;a href=&quot;https://academic.oup.com/rfs/article/30/12/4437/4047344&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;abdi_ranaldo_spread_estimator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    highs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lows&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    closes&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    k1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Constant used in the model&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate η_t = (high + low)/2 for each day&lt;/span&gt;
    eta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;highs &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; lows&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate variance of η changes&lt;/span&gt;
    eta_changes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eta&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;diff&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    var_eta_changes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; eta_changes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Estimate efficient price variance&lt;/span&gt;
    efficient_variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; var_eta_changes &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; k1&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate variance of close vs average etas&lt;/span&gt;
    eta_avg &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;eta &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; eta&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shift&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# (η_t + η_{t+1})/2&lt;/span&gt;
    close_vs_eta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; closes &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; eta_avg
    var_close_eta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; close_vs_eta&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Solve for spread&lt;/span&gt;
    spread &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;var_close_eta &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; k1&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; efficient_variance&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; spread&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; efficient_variance
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this function we can create a model that estimates the bid-ask spread and then uses this to estimate the slippage for a trade using the same class interface as before.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AbdiRanaldoModel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;SlippageModel&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volatility_window&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        volume_impact_factor&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        spread_factor&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        min_slippage_bps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token builtin&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__init__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;volatility_window&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; volume_impact_factor&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; spread_factor&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; min_slippage_bps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;estimate_market_spread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        highs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        lows&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        closes&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Series
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;
        Estimates the bid-ask spread using the Abdi-Ranaldo method.
        &quot;&quot;&quot;&lt;/span&gt;
        spread&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; abdi_ranaldo_spread_estimator&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;highs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lows&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; closes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; spread
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use this model to estimate the slippage for an individual trade from it&#39;s recent price history.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AbdiRanaldoModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Estimate the market spread&lt;/span&gt;
estimated_spread &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_market_spread&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;High&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Low&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Close&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Use the estimated spread in slippage calculation&lt;/span&gt;
slippage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_slippage&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    price&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Close&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    order_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    avg_daily_volume&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Volume&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bid_ask_spread&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;estimated_spread
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Estimate the execution price&lt;/span&gt;
execution_price &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;estimate_trade_price&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_price&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Close&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    order_size&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    avg_daily_volume&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Volume&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;iloc&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    is_buy&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    bid_ask_spread&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;estimated_spread
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Sat, 10 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/slippage/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/slippage/</guid>
    </item>
    
    <item>
      <title>Portfolio Rebalancing</title>
      <description>&lt;h1 id=&quot;portfolio-rebalancing&quot; tabindex=&quot;-1&quot;&gt;Portfolio Rebalancing&lt;/h1&gt;
&lt;p&gt;Continuing on our topic of portfolio theory, we&#39;ve already discussed how to optimize a portfolio given a set of constraints. Now we&#39;ll discuss how to use cvxopt to rebalance a portfolio to maintain the desired asset allocation.&lt;/p&gt;
&lt;p&gt;For example we&#39;re going to start with a simple portfolio that exists in a current portfolio and we&#39;ll rebalance it to a target portfolio. While the optimization techniques we&#39;ve discussed in the last post determine optimal portfolio weights, maintaining these weights over time requires careful consideration of transaction costs and market impact. We will need to change the portfolio weights over time to either reflect a new thesis or in response to changing market conditions.&lt;/p&gt;
&lt;p&gt;The rebalancing problem can be formulated as a quadratic programming problem that balances three competing objectives:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Minimizing the distance from target weights&lt;/li&gt;
&lt;li&gt;Minimizing transaction costs&lt;/li&gt;
&lt;li&gt;Maintaining risk-return characteristics&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The mathematical formulation looks like this:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{array}{ll}&lt;br /&gt;
&#92;text{minimize} &amp;amp; &#92;lambda_1(w - w_t)^T&#92;Sigma(w - w_t) + &#92;lambda_2|w - w_c|_2^2 + &#92;lambda_3|w - w_c|_1 &#92;&#92;&lt;br /&gt;
&#92;text{subject to} &amp;amp; &#92;mathbf{1}^T w = 1 &#92;&#92;&lt;br /&gt;
&amp;amp; w_i &#92;geq 0, &#92;quad i = 1,&#92;ldots,n&lt;br /&gt;
&#92;end{array}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(w_t&#92;) are the target weights from our optimization&lt;/li&gt;
&lt;li&gt;&#92;(w_c&#92;) are the current weights&lt;/li&gt;
&lt;li&gt;&#92;(&#92;lambda_1&#92;) controls the risk penalty&lt;/li&gt;
&lt;li&gt;&#92;(&#92;lambda_2&#92;) controls the L2 transaction cost (quadratic cost)&lt;/li&gt;
&lt;li&gt;&#92;(&#92;lambda_3&#92;) controls the L1 transaction cost (linear cost)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&#39;s a practical implementation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; cvxopt &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; solvers

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rebalance_portfolio&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lambda_risk&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lambda_trade&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; lambda_cost&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.01&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert inputs to cvxopt format&lt;/span&gt;
    P &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lambda_risk &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;
              lambda_trade &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Linear cost term&lt;/span&gt;
    q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lambda_cost &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;
              &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; lambda_trade &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Constraints&lt;/span&gt;
    G &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Long only constraint&lt;/span&gt;
                         np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Upper bound constraint&lt;/span&gt;
    h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# w_i &gt;= 0&lt;/span&gt;
                         np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# w_i &amp;lt;= 1&lt;/span&gt;

    A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Sum of weights = 1&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Solve the QP problem&lt;/span&gt;
    sol &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;qp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;P&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;status&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;optimal&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Optimization failed to converge&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    new_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;x&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate turnover&lt;/span&gt;
    turnover &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_weights &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; new_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; turnover
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And as a simple example, let&#39;s rebalance a portfolio with 4 stocks that we want to rebalance to an equal 25% weight in each stock:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;current_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Current portfolio weights&lt;/span&gt;
target_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Target weights&lt;/span&gt;

new_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; turnover &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; rebalance_portfolio&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    covariance_matrix
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Rebalancing Results:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Current weights:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Target weights:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;New weights:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; new_weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Turnover:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;turnover&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will solve for the amount of each stock to buy or sell to get to the target weights. First, it accounts for transaction costs, incorporating both linear costs, which are proportional to trade size, and quadratic costs that reflect market impact. The rebalanced portfolio is designed to maintain risk characteristics that are similar to those of the target portfolio. Finally, turnover control is a key aspect of the optimization process, as it seeks to balance the benefits of rebalancing with the associated costs.&lt;/p&gt;
&lt;p&gt;We can also tune the constraints with the lambda parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Higher &lt;code&gt;lambda_risk&lt;/code&gt; puts more emphasis on matching the risk characteristics&lt;/li&gt;
&lt;li&gt;Higher &lt;code&gt;lambda_trade&lt;/code&gt; reduces turnover&lt;/li&gt;
&lt;li&gt;Higher &lt;code&gt;lambda_cost&lt;/code&gt; makes the optimization more sensitive to transaction costs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Sector Exposure&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In reality we would want to have a lot more additional constraints to ensure that we are meeting our risk and return objectives. In practice, you might want to add additional constraints such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sector exposure limits&lt;/li&gt;
&lt;li&gt;Maximum position sizes&lt;/li&gt;
&lt;li&gt;Minimum trade sizes&lt;/li&gt;
&lt;li&gt;Trading costs that vary by asset&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These can be incorporated by adding even more constraints to the optimization problem. As an example, let&#39;s add the following constraints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No sector can exceed 30% of the portfolio&lt;/li&gt;
&lt;li&gt;Total turnover (sum of absolute changes) cannot exceed 20%&lt;/li&gt;
&lt;li&gt;Individual position sizes remain between 0% and 100%&lt;/li&gt;
&lt;li&gt;The sum of weights equals 100%&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s work with a more realistic example using 10 stocks from different sectors of the S&amp;amp;P 500:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; pd

stocks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;AAPL&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Technology&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;MSFT&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Technology&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JPM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Financials&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;    &lt;span class=&quot;token string&quot;&gt;&#39;Financials&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JNJ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Healthcare&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;ABBV&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Healthcare&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;PG&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token string&quot;&gt;&#39;Consumer Staples&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;KO&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token string&quot;&gt;&#39;Consumer Staples&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;XOM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Energy&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;CVX&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Energy&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;HD&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token string&quot;&gt;&#39;Consumer Discretionary&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;NEE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Utilities&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;LIN&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Materials&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;UNP&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&#39;Industrials&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;VZ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token string&quot;&gt;&#39;Communications&#39;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Read in historical stock prices from a CSV file&lt;/span&gt;
data &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read_csv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;stock_prices.csv&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; index_col&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Date&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; parse_dates&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate returns and covariance matrix&lt;/span&gt;
returns &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pct_change&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dropna&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
covariance_matrix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; returns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cov&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;252&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Annualized covariance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s enhance our rebalancing function to include sector constraints and more realistic transaction costs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rebalance_portfolio_with_constraints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    sector_map&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    transaction_costs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_sector_exposure&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_turnover&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lambda_risk&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    lambda_trade&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Create sector constraint matrix&lt;/span&gt;
    unique_sectors &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sector_map&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    sector_constraints &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unique_sectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sector &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unique_sectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; stock &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sector_map&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sector_map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;stock&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; sector&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                sector_constraints&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Convert inputs to cvxopt format&lt;/span&gt;
    P &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lambda_risk &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;
              lambda_trade &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Linear cost term including transaction costs&lt;/span&gt;
    q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; lambda_trade &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Constraints matrix&lt;/span&gt;
    G &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                &lt;span class=&quot;token comment&quot;&gt;# Long only constraint&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                 &lt;span class=&quot;token comment&quot;&gt;# Upper bound constraint&lt;/span&gt;
        sector_constraints&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Sector exposure constraints&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;sector_constraints&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Minimum sector exposure&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                 &lt;span class=&quot;token comment&quot;&gt;# Positive turnover&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;token comment&quot;&gt;# Negative turnover&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Constraints vector&lt;/span&gt;
    h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;              &lt;span class=&quot;token comment&quot;&gt;# Long only&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;               &lt;span class=&quot;token comment&quot;&gt;# Upper bound&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;repeat&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;max_sector_exposure&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unique_sectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Max sector&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unique_sectors&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                        &lt;span class=&quot;token comment&quot;&gt;# Min sector&lt;/span&gt;
        current_weights &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; max_turnover&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Max positive turnover&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;current_weights &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; max_turnover  &lt;span class=&quot;token comment&quot;&gt;# Max negative turnover&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Sum of weights = 1&lt;/span&gt;
    A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Solve the QP problem&lt;/span&gt;
    sol &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;qp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;P&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;status&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;optimal&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Optimization failed to converge&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    new_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;x&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    turnover &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_weights &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Calculate transaction costs&lt;/span&gt;
    total_cost &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_weights&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; transaction_costs&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ticker&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ticker &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sector_map&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; new_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; turnover&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; total_cost
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok let&#39;s run this with some real data. We&#39;ll use the current portfolio weights and the target portfolio weights from the last post:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# What we have&lt;/span&gt;
current_portfolio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;AAPL&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.15&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 15% Apple&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;MSFT&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 12% Microsoft&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JPM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% JPMorgan&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 8%  AT&amp;amp;T&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JNJ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 12% Johnson &amp;amp; Johnson&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;ABBV&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 8%  AbbVie&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;PG&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Procter &amp;amp; Gamble&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;KO&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 8%  Coca-Cola&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;XOM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.09&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 9%  Exxon Mobil&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;CVX&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.08&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 8%  Chevron&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;HD&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.07&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 7%  Home Depot&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;NEE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.06&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 6%  NextEra Energy&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;LIN&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 5%  Linde&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;UNP&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.06&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 6%  Union Pacific&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;VZ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.05&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 5%  Verizon&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# What we want&lt;/span&gt;
target_portfolio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;AAPL&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Apple&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;MSFT&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Microsoft&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JPM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% JPMorgan&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% AT&amp;amp;T&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JNJ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Johnson &amp;amp; Johnson&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;ABBV&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% AbbVie&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;PG&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Procter &amp;amp; Gamble&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;KO&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Coca-Cola&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;XOM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Exxon Mobil&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;CVX&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# 10% Chevron&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;HD&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 0%  Home Depot&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;NEE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 0%  NextEra Energy&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;LIN&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 0%  Linde&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;UNP&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 0%  Union Pacific&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;VZ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 0%  Verizon&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Dummy transaction costs (will vary by stock liquidity)&lt;/span&gt;
transaction_costs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;AAPL&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;MSFT&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JPM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0012&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 12 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;    &lt;span class=&quot;token number&quot;&gt;0.0012&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 12 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;JNJ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;ABBV&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;PG&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;KO&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 10 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;XOM&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0015&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 15 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;CVX&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0015&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 15 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;HD&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.0012&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 12 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;NEE&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0015&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 15 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;LIN&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0018&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 18 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;UNP&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;  &lt;span class=&quot;token number&quot;&gt;0.0015&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# 15 bps&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;VZ&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;   &lt;span class=&quot;token number&quot;&gt;0.0018&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# 18 bps&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Example usage&lt;/span&gt;
current_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;current_portfolio&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ticker&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ticker &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; stocks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
target_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;target_portfolio&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ticker&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ticker &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; stocks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


new_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; turnover&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; total_cost &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; rebalance_portfolio_with_constraints&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    current_weights&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;current_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    target_weights&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;target_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    covariance_matrix&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    sector_map&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;stocks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    transaction_costs&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;transaction_costs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_sector_exposure&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.30&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    max_turnover&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.20&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Print results with sector analysis&lt;/span&gt;
results &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DataFrame&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Stock&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stocks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Sector&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;stocks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Current Weight&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; current_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Target Weight&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; target_weights&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;New Weight&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; new_weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Rebalancing Results:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;results&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Turnover: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;turnover&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Transaction Costs: $&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;total_cost&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; per $1M traded&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Sector exposure analysis&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Sector Exposures:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
sector_exposures &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;groupby&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Sector&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;New Weight&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sector_exposures&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok this is a lot more realistic. We can see that the solution is respecting the sector constraints and the transaction costs are being minimized. Quite often the expected return of the portfolio is lower than the target return because of the additional constraints, especially with large position sizes.&lt;/p&gt;
&lt;p&gt;A more complicated model would also take into account the slippage that occurs when making large trades and the potential impact on the market price of the stocks. This isn&#39;t directly a constraint of the optimization problem, but it is something we can use other models to help mitigate.&lt;/p&gt;
</description>
      <pubDate>Mon, 05 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/portfolio_rebalance/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/portfolio_rebalance/</guid>
    </item>
    
    <item>
      <title>Using SMT Solvers to Manage JIRA Tickets</title>
      <description>&lt;h1 id=&quot;using-smt-solvers-to-manage-jira-tickets&quot; tabindex=&quot;-1&quot;&gt;Using SMT Solvers to Manage JIRA Tickets&lt;/h1&gt;
&lt;p&gt;One common and often tedious problem that managers regularly encounter is task scheduling. This is particularly relevant in project management, where assigning tasks to team members in a manner that adheres to various constraints can become a complex challenge. Constraints can include availability, skill levels, deadlines, and workload balance. Solving such problems manually can be error-prone and time-consuming, but with the power of Z3, an open-source SMT (Satisfiability Modulo Theories) solver from Microsoft Research, we can automate this process efficiently.&lt;/p&gt;
&lt;p&gt;Imagine you are managing a project with a set of tasks that need to be assigned to team members. Each task has a specific time requirement, and each team member has limited availability. Additionally, some tasks cannot be started until others are completed. Here, we’ll see how Z3 can help automate this scheduling problem, ensuring all constraints are met and tasks are assigned optimally.&lt;/p&gt;
&lt;p&gt;Let&#39;s consider a real-world scenario where:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We have 3 team members: Alice, Bob, and Charlie.&lt;/li&gt;
&lt;li&gt;There are 5 tasks, each requiring a different amount of time.&lt;/li&gt;
&lt;li&gt;Each team member has limited availability (number of hours they can work).&lt;/li&gt;
&lt;li&gt;Some tasks have dependencies and must be completed in a certain order.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The tasks and their durations (in hours) are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Task 1: 3 hours&lt;/li&gt;
&lt;li&gt;Task 2: 2 hours&lt;/li&gt;
&lt;li&gt;Task 3: 4 hours&lt;/li&gt;
&lt;li&gt;Task 4: 2 hours&lt;/li&gt;
&lt;li&gt;Task 5: 1 hour&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The availability of team members (in hours) is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Alice: 5 hours&lt;/li&gt;
&lt;li&gt;Bob: 4 hours&lt;/li&gt;
&lt;li&gt;Charlie: 3 hours&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The dependencies are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Task 3 cannot start until Task 1 is completed&lt;/li&gt;
&lt;li&gt;Task 5 cannot start until Task 4 is completed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s solve this problem using Z3. First, we need to install the Z3 package:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; z3-solver
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let&#39;s write the code to solve the problem:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; z3 &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Int&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Solver&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; And&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; If

&lt;span class=&quot;token comment&quot;&gt;# Create solver instance&lt;/span&gt;
solver &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Solver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define team members&lt;/span&gt;
team_members &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Alice&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Charlie&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define tasks and durations&lt;/span&gt;
tasks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 2&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 3&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 4&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 5&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define team member availability&lt;/span&gt;
availability &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Alice&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Charlie&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Define task dependencies&lt;/span&gt;
dependencies &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 3&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Task 1&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Task 5&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Task 4&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create a dictionary to hold the assigned team member for each task&lt;/span&gt;
task_assignment &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Int&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;replace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;_&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; task &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tasks&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Add constraints for team member availability&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; member &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; team_members&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;If&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;task_assignment&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tasks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; task&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tasks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;team_members&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; availability&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;member&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Add constraints for task dependencies&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; task&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; deps &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; dependencies&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; dep &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; deps&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;task_assignment&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; task_assignment&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;dep&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ensure each task is assigned to exactly one team member&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; task &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tasks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task_assignment&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;team_members&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Check for solution&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;check&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; sat&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; task &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; tasks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        assigned_member_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task_assignment&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as_long&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;task&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; is assigned to &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;team_members&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;assigned_member_index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;No solution found!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code defines the problem and constraints, and then uses Z3 to find a valid assignment of tasks to team members. The &lt;code&gt;task_assignment&lt;/code&gt; dictionary holds the assigned team member for each task, and the &lt;code&gt;solver.add&lt;/code&gt; method is used to add the constraints to the solver. Finally, the &lt;code&gt;solver.check&lt;/code&gt; method is used to check for a solution, and the &lt;code&gt;model&lt;/code&gt; is used to print the assigned tasks.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/z3/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/z3/</guid>
    </item>
    
    <item>
      <title>Word Calculators are Useful, but they&#39;re not Smart</title>
      <description>&lt;h1 id=&quot;word-calculators-are-useful%2C-but-they&#39;re-not-smart&quot; tabindex=&quot;-1&quot;&gt;Word Calculators are Useful, but they&#39;re not Smart&lt;/h1&gt;
&lt;p&gt;The release of GPT-3 marked a transformative moment in computer science with the advent of a tool capable of summarizing, analyzing, and predicting text completions with unprecedented accuracy. However, despite this impressive accomplishment, we need to understand that language models that generate text that closely mimics human dialogue, achieving a level of coherence that has led many to anthropomorphize what is, fundamentally, a sophisticated &lt;strong&gt;word calculator&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Just as calculators excel at arithmetic without understanding the profound understanding of number theory, LLMs are adept at word manipulation while remaining utterly devoid of comprehension. A computer algebra system performing complex calculus faster than a mathematician doesn&#39;t mean it grasps the underlying principles of mathematical proof—it&#39;s simply executing predetermined operations. Similarly, LLMs performing linguistic acrobatics doesn&#39;t indicate understanding; it&#39;s pattern matching against a massive corpus of internet text and combining those patterns together in surprising ways that appear coherent but the architecture fundamentally lacks any deep understanding of the world.&lt;/p&gt;
&lt;p&gt;The elephant in the room is the complete absence of a world model. These systems don&#39;t just lack understanding—they lack the fundamental architecture required to develop understanding. The popular hand-waving that &amp;quot;hallucinations are features, not bugs&amp;quot; is a thought-terminating cliché that obscures a fundamental limitation: these systems cannot distinguish truth from falsehood because they have no internal conception of either. They are sophisticated pattern matchers operating in a void, devoid of any grounding in reality.&lt;/p&gt;
&lt;p&gt;Knowledge isn&#39;t just information—it&#39;s &lt;strong&gt;justified true belief&lt;/strong&gt;. This distinction exposes the fundamental emptiness of LLM&#39;s claim to &amp;quot;knowledge.&amp;quot; How can a system have justified true beliefs when it lacks the machinery for belief itself? How can it justify anything when it has no concept of truth to justify against? LLMs are essentially high-dimensional curve-fitting exercises against massive datasets—impressive in scale but philosophically hollow. They&#39;re not &amp;quot;almost intelligent&amp;quot; or &amp;quot;approaching understanding&amp;quot;—they&#39;re fundamentally incapable of either in their current form.&lt;/p&gt;
&lt;p&gt;Our industry&#39;s breathless anthropomorphization of these tools does a disservice to both human intelligence and the enormous genuine utility of LLMs. Yes, there may be enormous economic value in generating unlimited amounts of coherent, stylistically-appropriate text. After all, our late capitalist just-in-time economy runs on a foundation of bullshit jobs that might be perfectly suited for automation by sophisticated bullshit generators. But let&#39;s not conflate economic utility with intelligence or understanding.&lt;/p&gt;
&lt;p&gt;We shouldn&#39;t rule out that future architectures might develop genuine world models and internal truth verification capabilities, but extrapolating such possibilities onto current transformer-based models is pure fantasy. We should absolutely remain open to future developments in AI architecture that might bridge these fundamental gaps. But pretending current LLMs are &amp;quot;almost there&amp;quot; or &amp;quot;just need more parameters&amp;quot; is willful self-deception. Until we see systems with fundamentally different architectures capable of maintaining internal models of truth and reality, we&#39;re still just building increasingly sophisticated calculators—ones that operate on words instead of numbers.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/word_calculators/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/word_calculators/</guid>
    </item>
    
    <item>
      <title>Enough Numbers to Build a Universe</title>
      <description>&lt;h1 id=&quot;enough-numbers-to-build-a-universe&quot; tabindex=&quot;-1&quot;&gt;Enough Numbers to Build a Universe&lt;/h1&gt;
&lt;p&gt;As a fun excercise, consider the question: What are the numbers that are used to build a universe? Well, we don&#39;t know for sure, but we do know that we can build our current understanding of physics out of a handful of fundamental constants. Some we know precisely, and some we know only approximately. Some are universal, and some are determined empirically. Let&#39;s start with the mathematical constants.&lt;/p&gt;
&lt;p&gt;D is the number of &lt;strong&gt;spatial dimensions&lt;/strong&gt;. Space has three dimensions, which are commonly referred to as length, width, and height. These dimensions are perpendicular to each other, and they provide a framework for understanding the position and motion of objects in space. There might be more, but for now we&#39;ll stick with three.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
D = 3&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The universe also has a single &lt;strong&gt;temporal dimension&lt;/strong&gt; that corresponds to the continuum of time. In spacetime, the temporal dimension is represented by a single number, known as a temporal coordinate, which describes the position of an object in time.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
T = 1&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Pi is a dimensionless mathematical constant that is defined as the ratio of the circumference of a circle to its diameter.&lt;/p&gt;
&lt;p&gt;$$&#92;pi &#92;approx 3.141592653589793238$$&lt;/p&gt;
&lt;p&gt;e is a dimensionless mathematical constant that arises in the study of exponential growth and decay. It arises naturally out of calculus.&lt;/p&gt;
&lt;p&gt;$$e &#92;approx 2.71828182845904523536$$&lt;/p&gt;
&lt;p&gt;Now, physical constants. The complete standard model of physics requires 25 fundamental dimensionless constants. At present, their numerical values are not understood in terms of any overarching theory and are determined only from experimental measurement. These constants are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;fine structure constant&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;strong coupling constant&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The four parameters of the &lt;strong&gt;Cabibbo-Kobayashi-Maskawa matrix&lt;/strong&gt; which describe how quarks oscillate between different forms&lt;/li&gt;
&lt;li&gt;The four parameters of the &lt;strong&gt;Pontecorvo-Maki-Nakagawa-Sakata matrix&lt;/strong&gt; which describe how neutrinos oscillate between different forms&lt;/li&gt;
&lt;li&gt;Fifteen masses of the fundamental particles, which can expressed in terms of the Planck mass
&lt;ul&gt;
&lt;li&gt;six quarks&lt;/li&gt;
&lt;li&gt;six leptons&lt;/li&gt;
&lt;li&gt;the Higgs boson&lt;/li&gt;
&lt;li&gt;the W boson&lt;/li&gt;
&lt;li&gt;the Z boson&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&quot;https://physics.nist.gov/cuu/Constants/Table/allascii.txt&quot;&gt;NIST has a table of all the physical constants&lt;/a&gt; that are used in the standard model of physics.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Fine Structure Constant&lt;/strong&gt; (denoted $&#92;alpha$) is a fundamental physical constant that is used in the study of the interaction between electromagnetic radiation and matter. The value of the Fine Structure Constant is a dimensionless quantity.&lt;/p&gt;
&lt;p&gt;$$&#92;alpha = &#92;frac{1}{4 &#92;pi &#92;epsilon_0} &#92;frac{e^2}{&#92;hbar c}$$&lt;/p&gt;
&lt;p&gt;$$&#92;alpha &#92;approx &#92;frac{1}{137}$$&lt;/p&gt;
&lt;p&gt;$$&#92;alpha = 0.0072973525693$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck constant&lt;/strong&gt; is denoted by the letter h, and its value is $6.63 &#92;cdot 10^{-34} J &#92;cdot s$. This constant allows us to calculate the amount of energy associated with a particle, given its frequency. The equation for this relationship is given by the formula E = hf, where E is the energy of the particle, f is its frequency, and h is the Planck constant.&lt;/p&gt;
&lt;p&gt;$$h = 6.62607015 &#92;times 10^{-34} &#92;quad J s$$&lt;/p&gt;
&lt;p&gt;$$&#92;hbar = &#92;frac{h}{2 &#92;pi}$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck mass&lt;/strong&gt; is defined as the mass of a particle whose Compton wavelength is equal to the Planck length. The Compton wavelength of a particle is a measure of its size, and the Planck length is the smallest possible length that can be measured in our universe. Therefore, the Planck mass is a measure of the smallest possible mass that can exist in our universe under our current understanding.&lt;/p&gt;
&lt;p&gt;$$1.22089 &#92;times 10^{19} &#92;quad GeV/c^2$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Cabibbo-Kobayashi-Maskawa matrix&lt;/strong&gt;, also known as the CKM matrix, is a unitary matrix used in particle physics to describe the mixing of the six quarks that make up protons and neutrons. The CKM matrix describes the mixing of the six quarks, which are the up quark, the down quark, the charm quark, the strange quark, the top quark, and the bottom quark. These six quarks are thought to be different forms or &amp;quot;flavors&amp;quot; of the same fundamental particles, and they can oscillate or &amp;quot;switch&amp;quot; between these different flavors as they interact with other particles.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{bmatrix}&lt;br /&gt;
V_{ud}&amp;amp;V_{us}&amp;amp;V_{ub} &#92;&#92;&lt;br /&gt;
V_{cd}&amp;amp;V_{cs}&amp;amp;V_{cb} &#92;&#92;&lt;br /&gt;
V_{td}&amp;amp;V_{ts}&amp;amp;V_{tb}&lt;br /&gt;
&#92;end{bmatrix}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Pontecorvo-Maki-Nakagawa-Sakata matrix&lt;/strong&gt;, also known as the PMNS matrix, is a unitary matrix used in particle physics to describe the mixing of three generations of neutrinos.&lt;/p&gt;
&lt;p&gt;The PMNS matrix describes the mixing of the three generations of neutrinos, which are the electron neutrino, the muon neutrino, and the tau neutrino. These three generations of neutrinos are thought to be different forms or &amp;quot;flavors&amp;quot; of the same fundamental particle, and they can oscillate or &amp;quot;switch&amp;quot; between these different flavors as they propagate through space.&lt;/p&gt;
&lt;p&gt;The PMNS matrix is a 3x3 matrix with three rows and three columns, and it contains six real parameters that describe the mixing of the three generations of neutrinos. These six parameters are the three mixing angles and the three CP-violating phases, which are associated with the phenomenon of CP violation in particle physics.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{bmatrix}&lt;br /&gt;
U_{e1}&amp;amp;U_{e2}&amp;amp;U_{e3} &#92;&#92;&lt;br /&gt;
U_{&#92;mu 1}&amp;amp;U_{&#92;mu 2}&amp;amp;U_{&#92;mu 3} &#92;&#92;&lt;br /&gt;
U_{&#92;tau 1}&amp;amp;U_{&#92;tau 2}&amp;amp;U_{&#92;tau 3}&lt;br /&gt;
&#92;end{bmatrix}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;up quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
1.4 × 10^{-22} – 2.7 × 10^{-22}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;down quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
3.4 × 10^{-22} – 4.8 × 10^{-22}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;charm quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
0.78 × 10^{-22} – 1.30 × 10^{-22}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;charged quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
1.4 × 10^{-22} – 2.7 × 10^{-22}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;strange quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
8.27 × 10^{-21}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;top quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
173.07 &#92;quad GeV/c^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;bottom quark&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
3.43 &#92;times 10^{-19}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;electron&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
4.18546 × 10^{-23}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;electron neutrino&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
9.00978 &#92;times 10^{-30}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;muon&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
8.65418 &#92;times 10^{-21}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;muon neutrino&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&amp;lt; 1.6 &#92;times 10^{-28}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;tau&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
1.45535 &#92;times 10^{-19}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;tau neutrino&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
1.6 &#92;times 10^{-28}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;W boson&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
80.385 &#92;quad GeV/c^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;Z boson&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
91.1876 &#92;quad GeV/c^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The mass of the &lt;strong&gt;Higgs boson&lt;/strong&gt; is:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
125.35 &#92;quad GeV&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The expectation value of the &lt;strong&gt;Higgs field&lt;/strong&gt; is a measure of the average value of the Higgs field in a given region of space. It is defined as the weighted average of the Higgs field over all possible configurations of the field, where the weighting is determined by the probability of each configuration.&lt;/p&gt;
&lt;p&gt;In gauge theories, the &lt;strong&gt;U(1) coupling constant&lt;/strong&gt; is a parameter that describes the strength of the interaction between charged particles and the electromagnetic field. It is a measure of how strongly charged particles are affected by the electromagnetic field, and it is related to the charge of the particles and the intensity of the electromagnetic field.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;SU(2) coupling constant&lt;/strong&gt; is a measure of the strength of the interaction between particles and the SU(2) gauge field. It is a fundamental constant of the Standard Model of particle physics, and it is related to the weak nuclear force. The SU(2) coupling constant is denoted by the symbol $&#92;alpha_W$, and it is defined as the ratio of the coupling constant for the weak force to the coupling constant for the electromagnetic force.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;strong coupling constant&lt;/strong&gt; is a measure of the strength of the interaction between quarks and gluons. It is also a fundamental constant of the Standard Model and it is related to the strong nuclear force. The strong coupling constant is denoted by the symbol $&#92;alpha_S$, and it is defined as the ratio of the coupling constant for the strong force to the coupling constant for the electromagnetic force.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Weinberg angle&lt;/strong&gt; is denoted by the symbol $&#92;theta_{W}$, and it is defined as the angle between the weak force and the electromagnetic force in a certain reference frame. The value of the Weinberg angle is not fixed, but it is typically measured to be around 0.23. This value is important because it determines the strength of the weak force relative to the electromagnetic force, which in turn affects the behavior of particles on the atomic and subatomic scales.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;theta_{W} = 0.22290&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;cosmological constant&lt;/strong&gt;, also known as the vacuum energy, is a fundamental constant of the universe that is used in the study of cosmology. It is denoted by the symbol $&#92;Lambda$, and it is defined as the energy density of empty space. The cosmological constant is related to the expansion of the universe, and it plays a crucial role in the study of the evolution of the universe over time. It is thought to be responsible for the acceleration of the expansion of the universe.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;Lambda = 1.05 &#92;times 10^{-52} &#92;quad m^{-2}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;speed of light&lt;/strong&gt; is a fundamental constant of the universe that is used in the study of physics. It is denoted by the symbol $c$, and it is defined as the speed at which light travels in a vacuum. The speed of light is a constant value, and it is related to the properties of space and time.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
c = 299792458 &#92;quad m/s&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;permeability of vacuum&lt;/strong&gt; is a fundamental constant of the universe that is used in the study of physics. It is denoted by the symbol $&#92;mu_0$, and it is defined as the magnetic permeability of a vacuum. The permeability of vacuum is related to the properties of space and time.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;mu_0 = {4 &#92;pi &#92;alpha &#92;hbar} / (e^2 c)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;elementary charge&lt;/strong&gt; is the electric charge carried by a single proton or, equivalently, the magnitude of the negative electric charge carried by a single electron.&lt;/p&gt;
&lt;p&gt;$$e = 1.60217663 &#92;times 10^{-19} &#92;text{coulombs} $$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Coulomb constant&lt;/strong&gt; is a fundamental constant of the universe that is used in the study of physics. It is denoted by the symbol $k_e$, and it is defined as the ratio of the electric force between two charged particles to the product of their charges. The Coulomb constant is related to the properties of space and time.&lt;/p&gt;
&lt;p&gt;$$k_e = 1 / (4 &#92;pi &#92;epsilon_0)$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;particle charges&lt;/strong&gt; are the electric charges carried by the particles in the universe.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Particle&lt;/th&gt;
&lt;th&gt;Charge (in units of elementary charge)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Electron&lt;/td&gt;
&lt;td&gt;$$-1 e$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proton&lt;/td&gt;
&lt;td&gt;$$+1 e$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Neutron&lt;/td&gt;
&lt;td&gt;$$0 e$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Up quark&lt;/td&gt;
&lt;td&gt;$$+2/3 e$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Down quark&lt;/td&gt;
&lt;td&gt;$$-1/3 e$$&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The &lt;strong&gt;particle rest masses&lt;/strong&gt; are the masses of the particles in the universe.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Particle&lt;/th&gt;
&lt;th&gt;Mass (in kilograms)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Electron&lt;/td&gt;
&lt;td&gt;$$9.10938356 &#92;cdot 10^{-31}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proton&lt;/td&gt;
&lt;td&gt;$$1.672621923 &#92;cdot 10^{-27}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Neutron&lt;/td&gt;
&lt;td&gt;$$1.674927471 &#92;cdot 10^{-27}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Muon&lt;/td&gt;
&lt;td&gt;$$1.883531302 &#92;cdot 10^{-28}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deuteron&lt;/td&gt;
&lt;td&gt;$$3.34358348 &#92;cdot 10^{-27}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alpha particle&lt;/td&gt;
&lt;td&gt;$$6.64465675 &#92;cdot 10^{-27}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tau&lt;/td&gt;
&lt;td&gt;$$3.16777 &#92;cdot 10^{-27}$$&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The &lt;strong&gt;Rydberg constant&lt;/strong&gt; is a physical constant that is used in the study of atomic and molecular structure. It is denoted by the symbol $R_{&#92;infty}$, and it is defined as the limit of the Rydberg formula as the atomic number approaches infinity. The Rydberg constant is related to the energy levels of the hydrogen atom, and it is used to calculate the wavelengths of the spectral lines emitted by hydrogen atoms.&lt;/p&gt;
&lt;p&gt;$$R_{&#92;infty} = 10973731.568508 &#92;quad m^{-1}$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck mass&lt;/strong&gt; is a fundamental physical constant that is used in the study of physics. It is denoted by the symbol $m_P$, and it is defined as the mass of a particle whose Compton wavelength is equal to the Planck length. The Planck length is the smallest possible length that can be measured in the universe, and it is related to the properties of space and time.&lt;/p&gt;
&lt;p&gt;$$m_P = 2.176434 &#92;times 10^{-8} &#92;quad kg$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck time&lt;/strong&gt; is a fundamental physical constant that is used in the study of physics. It is denoted by the symbol $t_P$, and it is defined as the time it takes for a photon to travel one Planck length in a vacuum. The Planck time is related to the properties of space and time, and it is used to calculate the smallest possible time interval that can be measured in the universe.&lt;/p&gt;
&lt;p&gt;$$t_P = 5.391247 &#92;times 10^{-44} &#92;quad s$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck length&lt;/strong&gt; is a fundamental physical constant that is used in the study of physics. It is denoted by the symbol $l_P$, and it is defined as the length at which quantum effects become significant. The Planck length is related to the properties of space and time, and it is used to calculate the smallest possible length that can be measured in the universe.&lt;/p&gt;
&lt;p&gt;$$l_P = 1.616255 &#92;times 10^{-35} &#92;quad m$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Planck temperature&lt;/strong&gt; is a fundamental physical constant that is used in the study of physics. It is denoted by the symbol $T_P$, and it is defined as the temperature at which the thermal energy of a particle is equal to its rest mass energy. The Planck temperature is related to the properties of space and time, and it is used to calculate the highest possible temperature that can be measured in the universe.&lt;/p&gt;
&lt;p&gt;$$T_P = 1.416 784 &#92;times 10^{32} &#92;quad K$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Newton constant&lt;/strong&gt;, also known as the &lt;strong&gt;gravitational constant&lt;/strong&gt;, is a physical constant that appears in Newton&#39;s law of universal gravitation. The gravitational constant, denoted by the letter G, is a proportionality constant that appears in the mathematical expression for the gravitational force between two objects.&lt;/p&gt;
&lt;p&gt;$$G = 6.67430 &#92;cdot 10^{-11} &#92;quad N m^2 / kg^2$$&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Hubble constant&lt;/strong&gt; is a measure of the expansion rate of the universe. The Hubble constant is typically denoted by the letter H, and it is defined as the ratio of the velocity with which a galaxy is moving away from us to its distance from us. This relationship is expressed mathematically as $H = v/d$, where $v$ is the velocity of the galaxy and d is its distance from us.&lt;/p&gt;
&lt;p&gt;$$H_0 = 67.8 &#92;quad km/s/&#92;text{megaparsec}$$&lt;/p&gt;
&lt;p&gt;And that&#39;s it! We have enough numbers to build our universe (at least as our current understanding goes). There&#39;s probably a few more that will emerge as we develop a more complete understanding of physical laws, and maybe a theory of spacetime itself may emerge out of some deeper theory but for now these are the 30 fundamental constants that underly, well, everything.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/universe/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/universe/</guid>
    </item>
    
    <item>
      <title>Cooking Logics with Soufflé</title>
      <description>&lt;h1 id=&quot;cooking-logics-with-souffl%C3%A9&quot; tabindex=&quot;-1&quot;&gt;Cooking Logics with Soufflé&lt;/h1&gt;
&lt;p&gt;Soufflé is an efficient Datalog engine designed for program analysis and knowledge representation. In this tutorial, we&#39;ll demonstrate basic and advanced features of Soufflé, including component parameterization and algebraic data types.&lt;/p&gt;
&lt;p&gt;Ensure you have Soufflé installed. You can download it from the &lt;a href=&quot;https://github.com/souffle-lang/souffle&quot;&gt;Soufflé GitHub repository&lt;/a&gt; and follow the installation instructions.&lt;/p&gt;
&lt;p&gt;Datalog is a declarative language for defining data queries and transformations. It&#39;s particularly useful for pattern matching and recursive data processing. If you&#39;re new to Datalog, you might find it helpful to start with a simple example.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Simple example&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Relationships
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;ancestor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;David&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;ancestor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;ancestor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ancestor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We define a &lt;code&gt;parent&lt;/code&gt; relationship.&lt;/li&gt;
&lt;li&gt;We derive an &lt;code&gt;ancestor&lt;/code&gt; relation using recursion.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To run the program, save your code in a file called &lt;code&gt;family.dl&lt;/code&gt;, then run the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;souffle family.dl
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Souffle supports the standard functional programming features, such as component parameterization and algebraic data types.&lt;/p&gt;
&lt;p&gt;Component parameters enable you to create reusable modules in Soufflé. Consider a scenario where we want to analyze two graphs using a similar structure but with different parameters.&lt;/p&gt;
&lt;p&gt;Create a Datalog file called &lt;code&gt;graph.dl&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;reach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;C&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;C&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;D&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;reach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;reach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we can easily create different graphs by saving them as separate Datalog files while referencing the same component.&lt;/p&gt;
&lt;p&gt;Soufflé also supports algebraic data types like sum types and product types. This feature allows you to model complex data structures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sum Types&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sum types represent a value that can take on multiple forms. Here’s an example of a sum type for shapes:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;type Shape &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Circle &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;radius&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Square &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;side&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shape&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Shape&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;output shape_instance

&lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Circle1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;Circle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Square1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;Square&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Product Types&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Product types combine multiple values. Here’s an example of a product type representing a 2D point:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;type Point &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;point_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Point&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;output point_instance

&lt;span class=&quot;token function&quot;&gt;point_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Point1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;point_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Point2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Modules&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can create more complex structures by combining component parameterization with algebraic data types. You can combine the shape and point definitions into a reusable component:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;type Shape &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Circle &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;radius&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Square &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;side&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; float&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;type ShapeParam &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; shape&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Shape&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;param&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ShapeParam&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;output shape_instance
&lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Circle1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;Circle&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;shape_instance&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Square1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;Square&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to the basic features, Soufflé supports advanced features such as component parameterization and algebraic data types. Components behave much like generative modules in Standard ML and can be parameterized by types.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;comp Catalog&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;type ItemID &lt;span class=&quot;token operator&quot;&gt;&amp;lt;:&lt;/span&gt; T
    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; author&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;borrower&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item_id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; borrower_id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; due_date&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;overdue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item_id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; borrower_id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; days_overdue&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;overdue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; BorrowerID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; DaysOverdue&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; BorrowerID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; DueDate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;to_number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;DueDate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;to_number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;2024-03-01&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Assuming current date &lt;span class=&quot;token operator&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2024&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;03&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;01&lt;/span&gt;
        DaysOverdue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;to_number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;2024-03-01&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;to_number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;DueDate&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

    &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;popular_item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; borrow_count&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;popular_item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BorrowerID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ItemID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; BorrowerID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BorrowerID&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;init BookCatalog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Catalog&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;symbol&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;init MovieCatalog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Catalog&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B001&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;1984&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;George Orwell&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B002&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;To Kill a Mockingbird&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Harper Lee&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrower&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;U001&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Alice&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrower&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;U002&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bob&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B001&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U001&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-02-15&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B002&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U002&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-02-20&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;B001&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U002&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-03-05&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;The Godfather&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Francis Ford Coppola&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Pulp Fiction&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Quentin Tarantino&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrower&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;U003&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Charlie&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrower&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;U004&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;David&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U003&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-02-25&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U004&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-02-28&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;borrowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;U004&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2024-03-10&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;overdue_books&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; borrower&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; days_overdue&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;output overdue_books
&lt;span class=&quot;token function&quot;&gt;overdue_books&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Borrower&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Days&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; BookCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;overdue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Borrower&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Days&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;decl &lt;span class=&quot;token function&quot;&gt;popular_movies&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; borrow_count&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;output popular_movies
&lt;span class=&quot;token function&quot;&gt;popular_movies&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Count&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&lt;/span&gt; MovieCatalog&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;popular_item&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ID&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Count&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/souffle/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/souffle/</guid>
    </item>
    
    <item>
      <title>Proving Trivial Theorems in Lean</title>
      <description>&lt;h1 id=&quot;proving-trivial-theorems-in-lean&quot; tabindex=&quot;-1&quot;&gt;Proving Trivial Theorems in Lean&lt;/h1&gt;
&lt;p&gt;When starting with a proof assistant like Lean 4, it&#39;s best to begin with the most elementary proofs possible. While these may seem trivially obvious to a mathematician, implementing them in a formal system helps build intuition for how the proof assistant works. By starting with basic logical statements that we know are true, we can focus on learning the syntax and mechanics of formal verification without getting lost in complex mathematics. Let&#39;s look at some of these foundational proofs to get comfortable with Lean&#39;s proof system.&lt;/p&gt;
&lt;h2 id=&quot;example-1%3A-identity&quot; tabindex=&quot;-1&quot;&gt;Example 1: Identity&lt;/h2&gt;
&lt;p&gt;Our first theorem shows that any proposition implies itself:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem self_implies_self (p : Prop) :
    p → p := fun h =&amp;gt; h
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can also be written using the &lt;code&gt;λ&lt;/code&gt; notation or with an explicit proof:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem self_implies_self&#39; (p : Prop) :
    p → p :=
    λ h =&amp;gt; h

theorem self_implies_self&#39;&#39; (p : Prop) :
    p → p := by
  intro h
  exact h
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-2%3A-and-elimination&quot; tabindex=&quot;-1&quot;&gt;Example 2: And Elimination&lt;/h2&gt;
&lt;p&gt;We can prove that if we have &lt;code&gt;p ∧ q&lt;/code&gt;, then we have &lt;code&gt;p&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem and_elim_left (p q : Prop) : p ∧ q → p := by
  intro h
  exact h.left
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-3%3A-or-introduction&quot; tabindex=&quot;-1&quot;&gt;Example 3: Or Introduction&lt;/h2&gt;
&lt;p&gt;We can prove that if we have &lt;code&gt;p&lt;/code&gt;, then we have &lt;code&gt;p ∨ q&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem or_intro_left (p q : Prop) : p → p ∨ q := by
  intro h
  exact Or.inl h
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-4%3A-double-negation-introduction&quot; tabindex=&quot;-1&quot;&gt;Example 4: Double Negation Introduction&lt;/h2&gt;
&lt;p&gt;We can prove that if &lt;code&gt;p&lt;/code&gt; is true, then &lt;code&gt;¬¬p&lt;/code&gt; is also true:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem double_neg_intro (p : Prop) : p → ¬¬p := by
  intro h
  intro hn
  exact hn h
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;example-5%3A-law-of-excluded-middle&quot; tabindex=&quot;-1&quot;&gt;Example 5: Law of Excluded Middle&lt;/h2&gt;
&lt;p&gt;In classical logic, for any proposition &lt;code&gt;p&lt;/code&gt;, either &lt;code&gt;p&lt;/code&gt; is true or &lt;code&gt;¬p&lt;/code&gt; is true. This is an axiom in Lean&#39;s classical logic:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem excluded_middle (p : Prop) : p ∨ ¬p := by
  exact Classical.em p
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that this is different from our previous examples. While the other theorems could be proved constructively, the law of excluded middle requires the &lt;code&gt;Classical&lt;/code&gt; axiom. In intuitionistic logic, this principle is not assumed. We use &lt;code&gt;Classical.em&lt;/code&gt; to access this axiom in Lean.&lt;/p&gt;
&lt;p&gt;You can also write this using pattern matching:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-lean&quot;&gt;theorem excluded_middle&#39; (p : Prop) : p ∨ ¬p :=
  match Classical.em p with
  | Or.inl h  =&amp;gt; Or.inl h    -- if p is true, return p
  | Or.inr h  =&amp;gt; Or.inr h    -- if ¬p is true, return ¬p
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/lean/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/lean/</guid>
    </item>
    
    <item>
      <title>E-Graphs in Rust</title>
      <description>&lt;h1 id=&quot;e-graphs-in-rust&quot; tabindex=&quot;-1&quot;&gt;E-Graphs in Rust&lt;/h1&gt;
&lt;p&gt;E-Graphs (aka &lt;em&gt;equality graphs&lt;/em&gt;) are one of the most exciting and rapidly evolving areas in programming language engineering. Originally developed in Gregory Nelson&#39;s PhD Thesis in 1980, they have become a foundational data structure that maintains equivalence relations over expressions. While they were initially created to power SMT solvers, their applications have expanded dramatically in recent years.&lt;/p&gt;
&lt;p&gt;This renaissance in e-graph applications is driven by techniques like equality saturation, which allows for more efficient exploration of equivalent expressions. The key innovation of E-Graphs is their ability to solve the so-called &amp;quot;choice problem&amp;quot; in term rewriting. Traditional term rewriting is destructive - once you transform an expression, the original is lost. This forces you to commit to specific optimization choices that might be locally optimal but globally suboptimal. For example, while &lt;code&gt;x + x&lt;/code&gt; can be optimized to &lt;code&gt;x * 2&lt;/code&gt;, applying this to &lt;code&gt;(x + x) - x&lt;/code&gt; would prevent discovering that one of the additions and the subtraction cancel out to just &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;E-Graphs solve this by maintaining all possible equivalent expressions simultaneously in a compact form. They consist of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;E-Classes&lt;/strong&gt;: Sets of equivalent expressions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;E-Nodes&lt;/strong&gt;: Operators with children that point to e-classes (not directly to other e-nodes)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;E-Class IDs&lt;/strong&gt;: Unique identifiers for e-classes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The power of E-Graphs comes from two key properties:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Congruence&lt;/strong&gt;: If &lt;code&gt;x&lt;/code&gt; is equivalent to &lt;code&gt;y&lt;/code&gt;, then &lt;code&gt;f(x)&lt;/code&gt; must be equivalent to &lt;code&gt;f(y)&lt;/code&gt;. This property is maintained automatically as equivalences are discovered.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compact Representation&lt;/strong&gt;: E-Graphs can represent exponentially many equivalent expressions in linear space by sharing structure between similar expressions.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When using E-Graphs for optimization through a technique called &lt;em&gt;equality saturation&lt;/em&gt;, the process works by:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Converting the initial expression into an E-Graph&lt;/li&gt;
&lt;li&gt;Repeatedly applying all possible rewrites to discover equivalences&lt;/li&gt;
&lt;li&gt;Extracting the optimal expression according to some cost function&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To get started with E-Graphs in Rust, we will use the &lt;code&gt;egg&lt;/code&gt; library, which provides a powerful and ergonomic API for working with E-Graphs. First, add the &lt;code&gt;egg&lt;/code&gt; crate to your &lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-toml&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token table class-name&quot;&gt;dependencies&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token key property&quot;&gt;egg&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0.6&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;congruence-in-e-graphs&quot; tabindex=&quot;-1&quot;&gt;Congruence in E-Graphs&lt;/h2&gt;
&lt;p&gt;A fundamental property of E-Graphs is that they maintain a congruence relation, not just an equivalence relation. Congruence means that if two expressions &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are equivalent, then any larger expression containing &lt;code&gt;x&lt;/code&gt; must be equivalent to the same expression with &lt;code&gt;x&lt;/code&gt; replaced by &lt;code&gt;y&lt;/code&gt;. More formally, if &lt;code&gt;x ≡ y&lt;/code&gt;, then &lt;code&gt;f(x) ≡ f(y)&lt;/code&gt; for any context &lt;code&gt;f&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let&#39;s see this in action with a simple example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;egg&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;define_language!&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;SimpleLanguage&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;congruence_example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; egraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EGraph&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Create expressions: (+ a x) and (+ a y)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;into&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;into&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;into&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; expr1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// (+ a x)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; expr2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// (+ a y)&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Initially, these are in different e-classes&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_ne!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// When we declare x ≡ y...&lt;/span&gt;
    egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;rebuild&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// ...congruence ensures (+ a x) ≡ (+ a y)&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;expr2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We start with two expressions &lt;code&gt;(+ a x)&lt;/code&gt; and &lt;code&gt;(+ a y)&lt;/code&gt; in different e-classes&lt;/li&gt;
&lt;li&gt;When we declare &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; equivalent using &lt;code&gt;union&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The e-graph automatically merges the e-classes containing &lt;code&gt;(+ a x)&lt;/code&gt; and &lt;code&gt;(+ a y)&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This congruence property is maintained automatically by the e-graph as equivalences are discovered. When the e-graph performs a &lt;code&gt;rebuild&lt;/code&gt; operation, it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Finds all e-nodes that have become equivalent due to congruence&lt;/li&gt;
&lt;li&gt;Merges their containing e-classes&lt;/li&gt;
&lt;li&gt;Repeats until no more merges are possible&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This automatic maintenance of congruence is what makes e-graphs so powerful for term rewriting and program optimization. It ensures that all equivalent expressions are properly identified, even when the equivalence is indirect through substitution in larger expressions.&lt;/p&gt;
&lt;h2 id=&quot;understanding-the-core-concepts&quot; tabindex=&quot;-1&quot;&gt;Understanding the Core Concepts&lt;/h2&gt;
&lt;p&gt;Before diving into code, let&#39;s understand the key abstractions in &lt;code&gt;egg&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Language Definition&lt;/strong&gt;: You must define your expression language using &lt;code&gt;egg&lt;/code&gt;&#39;s &lt;code&gt;define_language!&lt;/code&gt; macro. Each variant represents an operator or leaf node in your expressions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pattern Matching&lt;/strong&gt;: &lt;code&gt;egg&lt;/code&gt; uses a simple pattern language with variables like &lt;code&gt;?x&lt;/code&gt; to match subexpressions. These patterns power the rewrite rules.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Rewrite Rules&lt;/strong&gt;: Rules are bidirectional transformations between equivalent expressions, defined using the &lt;code&gt;rewrite!&lt;/code&gt; macro.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;E-Class Analysis&lt;/strong&gt;: Custom analyses can be attached to e-classes to maintain additional information about the equivalence classes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&#39;s see these concepts in action by defining a simple expression language:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;egg&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define our expression language&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;define_language!&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;SimpleLanguage&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;*&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Mul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define our rewrite rules&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;make_rules&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Vec&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Rewrite&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;vec!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-add&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a ?b)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?b ?a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-mul&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a ?b)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?b ?a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;add-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a 1)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s build a simple function that uses these rules to simplify expressions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/// Simplify an expression using egg&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;simplify&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Parse the expression&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RecExpr&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// Create a Runner and apply our rules&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; runner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Runner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;with_expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;make_rules&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Extract the smallest equivalent expression&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; extractor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Extractor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AstSize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_cost&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; best&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extractor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find_best&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;roots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Simplified {} to {} with cost {}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; best&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; best_cost&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    best&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;to_string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token attribute attr-name&quot;&gt;#[test]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;simple_tests&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;simplify&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;(* 0 42)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;simplify&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;(+ 0 (* 1 foo))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;core-functions&quot; tabindex=&quot;-1&quot;&gt;Core Functions&lt;/h2&gt;
&lt;p&gt;There are four core functions that you will use most often when working with E-Graphs:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; id1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; id2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;union&lt;/code&gt; function merges the e-classes containing &lt;code&gt;id1&lt;/code&gt; and &lt;code&gt;id2&lt;/code&gt;. It returns &lt;code&gt;true&lt;/code&gt; if the e-classes were merged, and &lt;code&gt;false&lt;/code&gt; if they were already in the same e-class.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;find&lt;/code&gt; function returns the representative of the e-class containing &lt;code&gt;id&lt;/code&gt;. The representative is the smallest expression in the e-class.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;pub&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;rebuild&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;usize&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;rebuild&lt;/code&gt; function performs a single round of congruence closure. It finds all e-nodes that have become equivalent due to congruence and merges their containing e-classes. It returns the number of e-classes that were merged.&lt;/p&gt;
&lt;h2 id=&quot;advanced-features&quot; tabindex=&quot;-1&quot;&gt;Advanced Features&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;egg&lt;/code&gt; library also provides several advanced features:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Custom Analysis&lt;/strong&gt;: You can define custom analyses that maintain additional information about e-classes:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token attribute attr-name&quot;&gt;#[derive(Default)]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;ConstantFolding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Analysis&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ConstantFolding&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DidMerge&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;b &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;DidMerge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;DidMerge&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;EGraph&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; enode&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; enode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; egraph&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token closure-params&quot;&gt;&lt;span class=&quot;token closure-punctuation punctuation&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token closure-punctuation punctuation&quot;&gt;|&lt;/span&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token comment&quot;&gt;// ... other operations ...&lt;/span&gt;
            _ &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Conditional Rewrites&lt;/strong&gt;: Rules can include conditions that must be satisfied:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;div-cancel&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(/ ?a ?a)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;1&quot;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_not_zero&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;using-the-runner-api&quot; tabindex=&quot;-1&quot;&gt;Using the Runner API&lt;/h2&gt;
&lt;p&gt;Let&#39;s look at a complete example of using the Runner API to perform equality saturation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;egg&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; rewrite &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; rw&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Define our rewrite rules&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Rewrite&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SymbolLang&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-add&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?x ?y)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?y ?x)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-mul&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?x ?y)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?y ?x)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;add-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?x 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?x 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;rw!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?x 1)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?x&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Parse the initial expression&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ 0 (* 1 a))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Run equality saturation&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; runner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Runner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;with_expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rules&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Extract the best expression using AstSize cost function&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; extractor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Extractor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AstSize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_cost&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; best_expr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extractor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find_best&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;roots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// The expression simplifies to just &quot;a&quot; with cost 1&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_expr&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_cost&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that while we&#39;re working with what appears to be numbers, &lt;code&gt;SymbolLang&lt;/code&gt; actually stores everything as strings. This is just for demonstration purposes.&lt;/p&gt;
&lt;h2 id=&quot;extraction-and-cost-functions&quot; tabindex=&quot;-1&quot;&gt;Extraction and Cost Functions&lt;/h2&gt;
&lt;p&gt;After running the E-Graph to saturation, we need to extract the &amp;quot;best&amp;quot; expression from potentially many equivalent ones. The &lt;code&gt;egg&lt;/code&gt; library provides an &lt;code&gt;Extractor&lt;/code&gt; that can find the optimal expression according to a cost function.&lt;/p&gt;
&lt;p&gt;Here&#39;s a complete example showing extraction in action:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;egg&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;define_language!&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;SimpleLanguage&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;i32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;+&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;*&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Mul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;extraction_example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Rewrite&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-add&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a ?b)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?b ?a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-mul&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a ?b)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?b ?a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;add-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mul-1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(* ?a 1)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Start with (+ 0 (* 1 10))&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ 0 (* 1 10))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; runner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Runner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;with_expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rules&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;roots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Extract the smallest equivalent expression&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; extractor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Extractor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;egraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AstSize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_cost&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; best&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; extractor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;find_best&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token comment&quot;&gt;// The expression simplifies to just &quot;10&quot; with cost 1&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best_cost&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;assert_eq!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;best&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;10&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Extractor&lt;/code&gt; uses a cost function to determine which expression to choose. In this example, we use &lt;code&gt;AstSize&lt;/code&gt; which simply counts the number of nodes in the expression. The expression &lt;code&gt;(+ 0 (* 1 10))&lt;/code&gt; simplifies to just &lt;code&gt;10&lt;/code&gt;, which has a cost of 1 (a single number node).&lt;/p&gt;
&lt;p&gt;You can also define custom cost functions by implementing the &lt;code&gt;CostFunction&lt;/code&gt; trait:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token attribute attr-name&quot;&gt;#[derive(Clone)]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;CustomCost&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;impl&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CostFunction&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CustomCost&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;Cost&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; enode&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; costs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Cost&lt;/span&gt; 
    &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FnMut&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;Self&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Cost&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; enode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;costs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;costs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// penalize additions more&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;SimpleLanguage&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Mul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;costs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;costs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This custom cost function penalizes additions more heavily than multiplications, which could be useful if multiplications are cheaper in your target platform.&lt;/p&gt;
&lt;h2 id=&quot;understanding-explanations&quot; tabindex=&quot;-1&quot;&gt;Understanding Explanations&lt;/h2&gt;
&lt;p&gt;One powerful feature of &lt;code&gt;egg&lt;/code&gt; is its ability to explain why two terms are equivalent in the e-graph. This is particularly useful when debugging rewrite rules or validating transformations. The explanations API can show the exact sequence of rewrites that transform one expression into another.&lt;/p&gt;
&lt;p&gt;To enable explanations, use the &lt;code&gt;with_explanations_enabled()&lt;/code&gt; method when creating a runner:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;egg&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token function-definition function&quot;&gt;explanation_example&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; rules&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Rewrite&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SymbolLang&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;commute-add&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a ?b)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?b ?a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token macro property&quot;&gt;rewrite!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;add-0&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ ?a 0)&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;?a&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ (+ a 0) b)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; end &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ b a)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;mut&lt;/span&gt; runner &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Runner&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;with_explanations_enabled&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;with_expr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rules&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// Get explanation of how start transforms to end&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;explain_equivalence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;end&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get_flat_string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The explanation output shows each step of the transformation with annotations indicating which rewrite rules were applied:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(+ (+ a 0) b)
(+ (Rewrite=&amp;gt; add-0 a) b)
(Rewrite=&amp;gt; commute-add (+ b a))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Explanations come in two forms:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;FlatExplanation&lt;/strong&gt;: A linear sequence of rewrites (shown above), which is most human-readable&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TreeExplanation&lt;/strong&gt;: A more compact representation that can share common sub-explanations&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The tree format is particularly useful when dealing with large expressions that have many shared subterms. Here&#39;s an example of a tree explanation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; tree_explanation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;explain_equivalence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;end&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;{}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tree_explanation&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get_tree_string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also use &lt;code&gt;explain_existence()&lt;/code&gt; to understand why a particular term exists in the e-graph:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Find out how a specific term came to exist&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; term &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;(+ a b)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;unwrap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;explanation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; runner&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;explain_existence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;term&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token macro property&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Term exists because: {}&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; explanation&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get_flat_string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is particularly valuable when debugging unexpected equivalences or validating that optimizations are working as intended.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/egraphs/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/egraphs/</guid>
    </item>
    
    <item>
      <title>Portfolio Theory with CVXOPT</title>
      <description>&lt;h1 id=&quot;portfolio-theory-with-cvxopt&quot; tabindex=&quot;-1&quot;&gt;Portfolio Theory with CVXOPT&lt;/h1&gt;
&lt;p&gt;In this tutorial, we will use the &lt;code&gt;cvxopt&lt;/code&gt; library in Python to implement Markowitz Portfolio Theory, which allows us to optimize a portfolio of assets by calculating the efficient frontier. The efficient frontier represents the set of optimal portfolios that offer the highest expected return for a given level of risk (standard deviation).&lt;/p&gt;
&lt;p&gt;In plain English, what we want is a mix of investments that generally go up over time, but don&#39;t all tank at once when things go bad. Ideally, when some investments are going down, we want others that tend to go up to balance things out. It&#39;s like not putting all your eggs in one basket, but being really smart about which baskets you choose and how many eggs go in each one. We&#39;ll go through three different ways of being &amp;quot;smarter&amp;quot; about our portfolio choice.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Classical Portfolio Optimization&lt;/strong&gt; - This is the simplest approach, where we just try to find the best risk-return trade-off by considering the contributions of each asset to the overall portfolio.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Factor Models&lt;/strong&gt; - This approach is more sophisticated and takes into account the fact that assets often move together in response to common economic drivers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Black-Litterman Model&lt;/strong&gt; - This approach is a Bayesian take on portfolio optimization that combines market equilibrium returns with a &amp;quot;thesis&amp;quot; or view on which assets will perform well to produce a customized portfolio that balances risk and return according to the investor&#39;s risk aversion and views.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Some short background, what we now colloquially call &lt;em&gt;modern portfolio theory&lt;/em&gt; was developed by Harry Markowitz in 1952 and forms the theoretical basis in which we now discuss optimal portfolios and diversification. The central insight of MPT is that an asset&#39;s risk and return shouldn&#39;t be evaluated in isolation, but rather by how it contributes to a portfolio&#39;s overall risk and return. The big idea is that we can construct portfolios that have the best risk-return trade-off by considering the contributions of each asset to the overall portfolio, but the question is how much, and that&#39;s where we can use modern tools like convex optimization solvers to help us out.&lt;/p&gt;
&lt;p&gt;You&#39;ll need to install the &lt;code&gt;cvxopt&lt;/code&gt; library, which can be done via pip:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; numpy pandas matplotlib cvxopt PyPortfolioOpt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&#39;m not going to explain everything, as this is a tutorial about code and convex optimization, not finance. But if you want to learn more about the finance, I recommend the following resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Modern Portfolio Theory&lt;/em&gt; - Grinold and Kahn&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Paul Wilmott on Quantitative Finance&lt;/em&gt; - Paul Wilmott&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We&#39;re also going to use the public data from Aswath Damodaran&#39;s website, which you can download &lt;a href=&quot;http://www.stern.nyu.edu/~adamodar/pc/datasets/histretSP.xls&quot;&gt;here&lt;/a&gt;. In real life you&#39;d get this data from Bloomberg or Reuters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Some Theory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At its core, portfolio theory deals with the relationship between risk and return. Return is typically measured as the expected value (mean) of historical returns, representing our best estimate of what we might earn in the future. Risk, on the other hand, is quantified through the standard deviation of returns, also known as &lt;em&gt;volatility&lt;/em&gt;. This volatility measure captures the uncertainty in our expected returns. We call &lt;em&gt;weights&lt;/em&gt; the fraction of our portfolio allocated to each asset (for example, 60% of our portfolio in stocks and 40% in bonds).&lt;/p&gt;
&lt;p&gt;Volatility plays a crucial role in investment decisions for several reasons. First, it represents the market&#39;s perception of risk – assets with higher volatility are generally considered riskier and thus should offer higher expected returns. Second, volatility directly impacts trading costs and opportunities, as more volatile assets typically have wider bid-ask spreads and require larger position sizes to achieve the same economic exposure. Finally, volatility affects an investor&#39;s ability to meet future cash flow needs, as high volatility can force liquidation at inopportune times.&lt;/p&gt;
&lt;p&gt;The power of MPT comes from understanding how assets move together, measured through their correlation. The portfolio risk is determined not just by the individual asset risks, but by how they move together. This relationship is captured in the portfolio risk formula:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;sigma = &#92;sqrt{w_1^2 &#92;sigma_1^2 + w_2^2 &#92;sigma_2^2 + 2w_1w_2&#92;sigma_1&#92;sigma_2&#92;rho_{12}}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In this equation, &#92;(w&#92;) represents weights, &#92;(&#92;sigma&#92;) represents individual asset volatilities, and &#92;(&#92;rho&#92;) represents the correlation coefficient between the assets. This formula reveals something remarkable: by combining assets with less than perfect positive correlation, we can achieve better risk-adjusted returns than by holding either asset alone. For a portfolio of n assets, the expected return is a weighted sum of individual asset returns:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
E(R_p) = &#92;sum_{i=1}^n w_i E(R_i)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where &#92;(w_i&#92;) represents the weight of asset i and &#92;(E(R_i)&#92;) is its expected return. This is the simpler part of the theory. The portfolio variance (risk squared) is given by:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;sigma_p^2 = &#92;sum_{i=1}^n &#92;sum_{j=1}^n w_i w_j &#92;sigma_{ij}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where &#92;(&#92;sigma*{ij}&#92;) represents the covariance between assets i and j. When &#92;(i=j&#92;), this becomes the variance of asset i. This equation captures the critical insight that portfolio risk depends not just on individual asset risks but on how assets move together. This can be captured in the so-called _covariance matrix*, which shows how different investments tend to move together. Think of it as a big table - if you look at any two investments in the table, their intersection tells you whether they tend to move in the same direction (positive relationship), opposite directions (negative relationship), or independently of each other (no relationship). The diagonal of this table shows how much each investment bounces around on its own (its volatility). This is a useful view because it helps us understand not just how risky individual investments are, but how they might work together to reduce overall portfolio risk.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;Sigma = &#92;begin{bmatrix}&lt;br /&gt;
&#92;sigma_{11} &amp;amp; &#92;sigma_{12} &amp;amp; &#92;cdots &amp;amp; &#92;sigma_{1n} &#92;&#92;&lt;br /&gt;
&#92;sigma_{21} &amp;amp; &#92;sigma_{22} &amp;amp; &#92;cdots &amp;amp; &#92;sigma_{2n} &#92;&#92;&lt;br /&gt;
&#92;vdots &amp;amp; &#92;vdots &amp;amp; &#92;ddots &amp;amp; &#92;vdots &#92;&#92;&lt;br /&gt;
&#92;sigma_{n1} &amp;amp; &#92;sigma_{n2} &amp;amp; &#92;cdots &amp;amp; &#92;sigma_{nn}&lt;br /&gt;
&#92;end{bmatrix}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The matrix is symmetric and the diagonal elements are the variances of the individual assets. The off-diagonal elements are the covariances between the assets. In our case, &#92;(&#92;Sigma&#92;) is a &#92;(4&#92;)x&#92;(4&#92;) matrix because we have four investments: U.S. Treasury Bonds (denoted &#92;(A&#92;)), Corporate Bonds (denoted &#92;(B&#92;)), Stocks (denoted &#92;(C&#92;)), and T-Bills (denoted &#92;(D&#92;)).&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;Sigma = &#92;begin{bmatrix}&lt;br /&gt;
&#92;sigma_{A,A} &amp;amp; &#92;sigma_{A,B} &amp;amp; &#92;sigma_{A,C} &amp;amp; &#92;sigma_{A,D} &#92;&#92;&lt;br /&gt;
&#92;sigma_{B,A} &amp;amp; &#92;sigma_{B,B} &amp;amp; &#92;sigma_{B,C} &amp;amp; &#92;sigma_{B,D} &#92;&#92;&lt;br /&gt;
&#92;sigma_{C,A} &amp;amp; &#92;sigma_{C,B} &amp;amp; &#92;sigma_{C,C} &amp;amp; &#92;sigma_{C,D} &#92;&#92;&lt;br /&gt;
&#92;sigma_{D,A} &amp;amp; &#92;sigma_{D,B} &amp;amp; &#92;sigma_{D,C} &amp;amp; &#92;sigma_{D,D}&lt;br /&gt;
&#92;end{bmatrix}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Now in order to find the optimal portfolio, we need to solve the problem of minimizing the portfolio risk &#92;(&#92;sigma&lt;em&gt;p^2&#92;) subject to constraints: &#92;(&#92;sum&lt;/em&gt;{i=1}^n w&lt;em&gt;i = 1&#92;) (weights sum to 1), &#92;(E(R_p) = R&lt;/em&gt;{target}&#92;) (target return constraint), and often &#92;(w_i &#92;geq 0&#92;) (no short selling).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Classical Portfolio Optimization&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Using the theory above, our model aims to find the optimal portfolio allocation by balancing two competing objectives:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Maximizing expected returns&lt;/li&gt;
&lt;li&gt;Minimizing risk (variance)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The optimization problem is formulated as:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{array}{ll}&lt;br /&gt;
&#92;text{maximize} &amp;amp; &#92;mu^T w - &#92;gamma w^T&#92;Sigma w &#92;&#92;&lt;br /&gt;
&#92;text{subject to} &amp;amp; &#92;mathbf{1}^T w = 1, &#92;quad w &#92;in &#92;mathcal{W}&lt;br /&gt;
&#92;end{array}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(w &#92;in &#92;mathbb{R}^n&#92;) is the portfolio allocation vector (the weights for each asset)&lt;/li&gt;
&lt;li&gt;&#92;(&#92;mu&#92;) is the vector of expected returns&lt;/li&gt;
&lt;li&gt;&#92;(&#92;Sigma&#92;) is the covariance matrix&lt;/li&gt;
&lt;li&gt;&#92;(&#92;gamma &amp;gt; 0&#92;) is the risk aversion parameter&lt;/li&gt;
&lt;li&gt;&#92;(&#92;mathcal{W}&#92;) is the set of allowed portfolios (e.g., &#92;(&#92;mathcal{W} = &#92;mathbb{R}_+^n&#92;) for long-only portfolios)&lt;/li&gt;
&lt;li&gt;&#92;(&#92;mathbf{1}^T w = 1&#92;) ensures the weights sum to 1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The objective function &#92;(&#92;mu^T w - &#92;gamma w^T&#92;Sigma w&#92;) represents the risk-adjusted return where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(&#92;mu^T w&#92;) is the expected portfolio return&lt;/li&gt;
&lt;li&gt;&#92;(w^T&#92;Sigma w&#92;) is the portfolio variance (risk)&lt;/li&gt;
&lt;li&gt;&#92;(&#92;gamma&#92;) controls the trade-off between return and risk&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By varying the risk aversion parameter &#92;(&#92;gamma&#92;), you can trace out the efficient frontier, which shows the optimal risk-return trade-offs available to investors. A higher &#92;(&#92;gamma&#92;) results in more conservative portfolios (lower risk, lower return), while a lower &#92;(&#92;gamma&#92;) leads to more aggressive portfolios (higher risk, higher return). The same risk-return trade-off can also be obtained by fixing a target return and minimizing risk, or fixing a risk level and maximizing return.&lt;/p&gt;
&lt;p&gt;The so-called &lt;em&gt;efficient frontier&lt;/em&gt; represents the set of portfolios that offer the highest expected return for a given level of risk, or conversely, the lowest risk for a given expected return. This concept is powerful because it shows that there exists an optimal set of portfolios that dominate all others – any portfolio not on the efficient frontier can be improved by either increasing return while maintaining the same risk or reducing risk while maintaining the same return.&lt;/p&gt;
&lt;p&gt;In short, it&#39;s the set of portfolios that given a target risk and return any rational investor would choose. And the efficient frontier is the boundary of the set of achievable portfolios. And we can can empirically estimate the efficient frontier by calculating the returns and risks of portfolios with different weights.&lt;/p&gt;
&lt;p&gt;Theory aside, when implementing portfolio optimization in practice, several real-world considerations come into play. The theory assumes we know the true expected returns and covariances of assets, but in reality, we must estimate these from historical data. These estimates contain uncertainty, which can lead to optimization error.&lt;/p&gt;
&lt;p&gt;Also, the standard naive theory assumes frictionless markets with no transaction costs and perfect divisibility of assets. Real markets have friction in the form of bid-ask spreads, transaction costs, and minimum trade sizes. These practical limitations often necessitate additional constraints in the optimization problem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; pd
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matplotlib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pyplot &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; plt
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; cvxopt &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; solvers

&lt;span class=&quot;token comment&quot;&gt;# Download historical returns data&lt;/span&gt;
data_sheet &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Returns by year&quot;&lt;/span&gt;
skiprows &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;17&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Skip header rows&lt;/span&gt;
skipfooter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Skip footer rows&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Download the data from NYU Stern&#39;s website&lt;/span&gt;
df &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read_excel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;http://www.stern.nyu.edu/~adamodar/pc/datasets/histretSP.xls&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    sheet_name&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;data_sheet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    skiprows&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;skiprows&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    skipfooter&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;skipfooter
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Select and rename the relevant columns&lt;/span&gt;
columns_of_interest &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;S&amp;amp;P 500 (includes dividends)&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;SP500&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;3-month T.Bill&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;T_Bill&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;US T. Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;T_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Baa Corporate Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Corp_Bond&#39;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Clean and prepare the data&lt;/span&gt;
df &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;columns_of_interest&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;keys&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;rename&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;columns_of_interest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Convert percentages to decimals&lt;/span&gt;
df &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate basic statistics&lt;/span&gt;
returns &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
risks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;std&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
correlation_matrix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;corr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
covariance_matrix &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cov&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Annual Returns:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Annual Risks (Standard Deviation):&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;risks&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Correlation Matrix:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;correlation_matrix&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After calculating the basic statistics, let&#39;s add the optimization code which will calculate the minimum volatility portfolio and the portfolio weights.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;options&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;show_progress&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Suppress solver output&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Convert our data to the format CVXOPT expects&lt;/span&gt;
n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# number of assets&lt;/span&gt;
P &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# covariance matrix&lt;/span&gt;
q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
G &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# negative identity matrix for &gt;= 0 constraint&lt;/span&gt;
h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# constraint that weights sum to 1&lt;/span&gt;
b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Solve for minimum volatility portfolio&lt;/span&gt;
sol &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;qp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;P&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;x&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate portfolio metrics&lt;/span&gt;
min_vol_ret &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
min_vol_risk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate efficient frontier&lt;/span&gt;
target_returns &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;linspace&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;min_vol_ret&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
frontier_risks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
frontier_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; target_ret &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; target_returns&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    P &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    G &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hstack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zeros&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;target_ret&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    sol &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;qp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;P&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;status&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;optimal&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;x&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        risk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        frontier_risks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;risk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        frontier_weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Plot the efficient frontier&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;figure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;figsize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;plot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;frontier_risks&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_returns&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;b-&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Efficient Frontier&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Plot individual assets&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; asset &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scatter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;risks&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; returns&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; marker&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;o&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Plot minimum volatility portfolio&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scatter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;min_vol_risk&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; min_vol_ret&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; color&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;red&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; marker&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;*&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
           s&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Minimum Volatility&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;xlabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Risk (Standard Deviation)&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ylabel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Expected Return&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Efficient Frontier&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;legend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;grid&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;show&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Print minimum volatility portfolio allocation&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Minimum Volatility Portfolio Allocation:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; asset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weight &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;returns&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;weight&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Portfolio Return: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;min_vol_ret&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Portfolio Risk: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;min_vol_risk&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Factor Models&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Factor models represent an extension to portfolio theory that provide a more structured way to understand and decompose asset returns. While MPT focuses on the relationship between overall portfolio risk and return, factor models break down asset returns into systematic components (driven by common factors) and idiosyncratic components (specific to each asset). This decomposition helps us better understand the sources of risk and return in their portfolios. For example, instead of just knowing that a stock has a certain expected return and volatility, factor models might reveal that 40% of its return variation comes from market exposure, 20% from interest rate sensitivity, and the remaining 40% from company-specific events.&lt;/p&gt;
&lt;p&gt;Interest rate exposure is a systematic risk factor since it affects all assets. Market exposure is also a systematic risk factor since it affects all assets, broadly as the S&amp;amp;P 500 trends upwards or downwards it can affect all assets. And then company-specific events are idiosyncratic risk since they are specific to each asset (i.e. new product launches, earnings reports, accounting scandals, etc.).&lt;/p&gt;
&lt;p&gt;The theoretical foundation of factor models rests on the observation that assets often move together in response to common economic drivers. These common movements can be captured by a set of factors – such as market returns, interest rates, inflation, or more abstract statistical factors. The famous Capital Asset Pricing Model (CAPM) can be viewed as the simplest factor model, using only the market return as a single factor. More sophisticated models like the Fama-French three-factor model add size and value factors, while modern approaches might include dozens of factors capturing various market anomalies and risk premia. The key insight is that by identifying these common factors, we can build more robust portfolios that are deliberately exposed to or protected from specific economic risks.&lt;/p&gt;
&lt;p&gt;This represents the mathematical optimization problem:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{array}{ll}&lt;br /&gt;
&#92;text{maximize} &amp;amp; &#92;mu^T w - &#92;gamma(f^T &#92;tilde{&#92;Sigma} f + w^T D w) &#92;&#92;&lt;br /&gt;
&#92;text{subject to} &amp;amp; &#92;mathbf{1}^T w = 1 &#92;&#92;&lt;br /&gt;
&amp;amp; |w|_1 &#92;leq L^{&#92;text{max}} &#92;&#92;&lt;br /&gt;
&amp;amp; f = F^T w&lt;br /&gt;
&#92;end{array}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(n&#92;) is the number of assets&lt;/li&gt;
&lt;li&gt;&#92;(w &#92;in &#92;mathbb{R}^n&#92;) is the portfolio allocation vector&lt;/li&gt;
&lt;li&gt;&#92;(f &#92;in &#92;mathbb{R}^k&#92;) represents the factor exposures&lt;/li&gt;
&lt;li&gt;&#92;(&#92;mu&#92;) is the vector of expected returns&lt;/li&gt;
&lt;li&gt;&#92;(F&#92;) is the factor loading matrix&lt;/li&gt;
&lt;li&gt;&#92;(&#92;tilde{&#92;Sigma}&#92;) is the factor covariance matrix&lt;/li&gt;
&lt;li&gt;&#92;(D&#92;) is the diagonal matrix of idiosyncratic risks&lt;/li&gt;
&lt;li&gt;&#92;(&#92;gamma = 0.1&#92;) is the risk aversion parameter&lt;/li&gt;
&lt;li&gt;&#92;(L^{&#92;text{max}} = 2&#92;) is the leverage limit&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In cvxopt, the optimization problem is formulated as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Variables:&lt;/span&gt;
w &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Variable&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Portfolio weights&lt;/span&gt;
f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;w           &lt;span class=&quot;token comment&quot;&gt;# Factor exposures&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Objective function:&lt;/span&gt;
ret &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mu&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;w       &lt;span class=&quot;token comment&quot;&gt;# Expected return&lt;/span&gt;
risk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;quad_form&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Sigma_tilde&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;quad_form&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; D&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Factor model risk&lt;/span&gt;
objective &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Maximize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ret &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; gamma&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;risk&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Constraints:&lt;/span&gt;
constraints &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Weights sum to 1&lt;/span&gt;
    cp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;norm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; Lmax  &lt;span class=&quot;token comment&quot;&gt;# Leverage limit&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Factor models complement MPT by providing a more nuanced framework for portfolio optimization. While MPT uses historical correlations between assets to optimize portfolios, factor models can potentially provide more stable and forward-looking estimates of how assets will move together. This is particularly valuable when historical data is limited or when market conditions are changing. Furthermore, factor models allow investors to construct portfolios with specific factor tilts – for instance, maximizing exposure to value while minimizing market beta – which goes beyond the simple risk-return optimization of traditional MPT. This makes factor models particularly valuable for institutional investors who need to manage specific risk exposures or implement sophisticated investment strategies while maintaining the core insights of portfolio diversification from MPT.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Prepare factor data&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Using S&amp;amp;P 500 as market factor and T-Bill rate as interest rate factor&lt;/span&gt;
factors &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;SP500&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;T_Bill&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
assets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; df&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;T_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Corp_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Assets to model&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Run factor regression for each asset&lt;/span&gt;
factor_betas &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
idiosyncratic_vars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; asset &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Perform linear regression&lt;/span&gt;
    X &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; factors
    y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; assets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    beta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;linalg&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;inv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T @ X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; @ X&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T @ y

    &lt;span class=&quot;token comment&quot;&gt;# Calculate residuals and their variance&lt;/span&gt;
    residuals &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; X @ beta
    idio_var &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;residuals&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    factor_betas&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;beta&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    idiosyncratic_vars&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idio_var&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Convert to numpy arrays&lt;/span&gt;
factor_betas &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_betas&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
idiosyncratic_vars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;diag&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idiosyncratic_vars&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate factor covariance matrix&lt;/span&gt;
factor_cov &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; factors&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cov&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Construct the factor-based covariance matrix&lt;/span&gt;
factor_based_cov &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_betas @ factor_cov @ factor_betas&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; idiosyncratic_vars

&lt;span class=&quot;token comment&quot;&gt;# Optimize portfolio using factor-based covariance&lt;/span&gt;
n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
P &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_based_cov&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
q &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
G &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;eye&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
h &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; matrix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Solve for minimum volatility portfolio using factor-based covariance&lt;/span&gt;
sol &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solvers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;qp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;P&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
factor_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sol&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;x&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate portfolio metrics using factor model&lt;/span&gt;
factor_portfolio_risk &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                                     np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_based_cov&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; factor_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Print results&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Factor Model Analysis:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Factor Betas:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; asset &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Market Beta: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;factor_betas&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Interest Rate Beta: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;factor_betas&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Optimal Portfolio Weights (Factor Model):&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; asset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weight &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; factor_weights&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;weight&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Portfolio Risk (Factor Model): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;factor_portfolio_risk&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Compare factor-based and sample covariance matrices&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Covariance Matrix Comparison:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Factor-Based Covariance:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DataFrame&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;factor_based_cov&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                  index&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                  columns&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;columns&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Sample Covariance:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;assets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cov&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Black-Litterman Model&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The culmination of the analysis comes with the Black-Litterman model implementation, which represents a more practical approach used by institutional investors. This is a Bayesian model that starts with market equilibrium returns derived from market capitalizations, rather than historical returns, and allows investors to incorporate their specific views with varying levels of confidence. The model demonstrates how to combine market &amp;quot;wisdom&amp;quot; (through equilibrium returns) with investor insights (through explicit views) to create more intuitive and stable portfolio allocations.&lt;/p&gt;
&lt;p&gt;The core of Black-Litterman is captured in the posterior return equation:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
E(R) = [(τΣ)^{-1} + P^T Ω^{-1} P]^{-1}[(τΣ)^{-1} Π + P^T Ω^{-1} Q]&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&#92;(R&#92;) is a &#92;(N&#92;)x&#92;(1&#92;) vector of returns&lt;/li&gt;
&lt;li&gt;&#92;(Q&#92;) is a &#92;(K&#92;)x&#92;(1&#92;) vector of views.&lt;/li&gt;
&lt;li&gt;&#92;(&#92;Sigma&#92;) is the &#92;(N&#92;)x&#92;(N&#92;) covariance matrix of asset returns&lt;/li&gt;
&lt;li&gt;&#92;(E(R)&#92;) is a &#92;(N&#92;)x&#92;(1&#92;) vector of expected returns, where &#92;(N&#92;) is the number of assets.&lt;/li&gt;
&lt;li&gt;&#92;(P&#92;) is the &#92;(K&#92;)x&#92;(N&#92;) picking matrix which maps views to the universe of assets.&lt;/li&gt;
&lt;li&gt;&#92;(&#92;Omega&#92;) is the &#92;(K&#92;)x&#92;(K&#92;) uncertainty matrix of views.&lt;/li&gt;
&lt;li&gt;&#92;(&#92;Pi&#92;) is the &#92;(N&#92;)x&#92;(1&#92;) vector of prior expected returns.&lt;/li&gt;
&lt;li&gt;&#92;(&#92;tau&#92;) is a scalar tuning constant.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This equation combines market equilibrium returns (&#92;(&#92;Pi&#92;)) with investor views (&#92;(Q&#92;)) using Bayesian statistics. The model also produces a posterior covariance matrix&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;hat{&#92;Sigma} = &#92;sigma^2 + [(&#92;tau &#92;sigma^2)^{-1} + P^T &#92;Omega^{-1} P]^{-1}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;that accounts for uncertainty in both the market prior and investor views. The market-implied returns are calculated as&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
(&#92;Pi = &#92;delta &#92;sigma w_{mkt}),&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;where the market risk aversion parameter &#92;(&#92;delta&#92;) is derived from&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;delta = (R-R_f)/&#92;sigma^2&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Think of the BL model as a sophisticated way of saying &amp;quot;I have some ideas about where the market is going, but I&#39;m not entirely sure.&amp;quot; The model starts with what the market is implying through current prices - this is like getting the wisdom of the crowd. Then, you add your own views, like &amp;quot;I think tech stocks will outperform&amp;quot; or &amp;quot;bonds will struggle this year.&amp;quot; The clever part is that you can say how confident you are in each view and you can incorporate your uncertainty in the views.&lt;/p&gt;
&lt;p&gt;The model then does something similar to what we do naturally: if you&#39;re very confident in your view and it differs from the market consensus, the final expectation will move significantly toward your view. If you&#39;re less confident, it will stay closer to what the market implies. Even more impressively, if you have a view on one asset (like tech stocks), the model will automatically adjust related assets (like semiconductor manufacturers) in a sensible way based on their historical relationships.&lt;/p&gt;
&lt;p&gt;The beauty of this approach is that it produces much more reasonable portfolio allocations than traditional methods. Instead of putting all your money in the asset with the highest historical return (which traditional optimization often suggests), it creates a balanced portfolio that reflects both market wisdom and your personal insights, while accounting for how confident you are in each.&lt;/p&gt;
&lt;p&gt;Unlike the code above, this model addresses many of the limitations of traditional mean-variance optimization, such as extreme allocations and high sensitivity to input parameters, making it particularly valuable for real-world portfolio management. We&#39;ll use the implementation in PyPortfolioOpt library.&lt;/p&gt;
&lt;p&gt;Our code is going to use the so-called &lt;em&gt;Idzorek method&lt;/em&gt; for estimating the views uncertainty matrix &#92;(&#92;Omega&#92;). I&#39;m not going to dive into the details of the method here, but it&#39;s a sophisticated way to estimate the uncertainty in the views that the underlying library will take care of Our example thesis for this code is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Market-Implied Returns:&lt;/strong&gt; Instead of using historical returns, we start with market equilibrium returns derived from market capitalizations. This assumes that current market prices reflect collective wisdom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Investor Views:&lt;/strong&gt; We incorporate specific views on expected returns:
&lt;ul&gt;
&lt;li&gt;Bullish view on S&amp;amp;P 500 (12% return)&lt;/li&gt;
&lt;li&gt;Conservative view on Treasury bonds (4% return)&lt;/li&gt;
&lt;li&gt;Moderate view on Corporate bonds (6% return)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View Confidence:&lt;/strong&gt; We specify different confidence levels for each view using Idzorek&#39;s method:
&lt;ul&gt;
&lt;li&gt;60% confidence in S&amp;amp;P 500 view&lt;/li&gt;
&lt;li&gt;70% confidence in Treasury bond view&lt;/li&gt;
&lt;li&gt;50% confidence in Corporate bond view&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Posterior Estimates:&lt;/strong&gt; We combine the prior (market-implied) returns with our views to produce posterior estimates of returns and covariance.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pypfopt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;black_litterman &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; BlackLittermanModel
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pypfopt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;efficient_frontier &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; EfficientFrontier
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pypfopt &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; black_litterman

&lt;span class=&quot;token comment&quot;&gt;# Calculate market-implied risk aversion&lt;/span&gt;
delta &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; black_litterman&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;market_implied_risk_aversion&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;df&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;SP500&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Market capitalization weights (example values - you should use real market caps)&lt;/span&gt;
market_caps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;SP500&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# S&amp;amp;P 500&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T_Bill&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5e5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# T-Bills&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7e5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Treasury Bonds&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Corp_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3e5&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Corporate Bonds&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate prior (market-implied) returns&lt;/span&gt;
prior &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; black_litterman&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;market_implied_prior_returns&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    market_caps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    delta&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    covariance_matrix
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Specify your views&lt;/span&gt;
viewdict &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;SP500&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Bullish on S&amp;amp;P 500: 12% return&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;T_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.04&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Treasury bonds will return 4%&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;Corp_Bond&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.06&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Corporate bonds will return 6%&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Confidence in each view (from 0 to 1)&lt;/span&gt;
view_confidences &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Create the Black-Litterman model&lt;/span&gt;
bl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; BlackLittermanModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    cov_matrix&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;covariance_matrix&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    pi&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;prior&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    absolute_views&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;viewdict&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    omega&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;idzorek&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    view_confidences&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;view_confidences
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate posterior returns and covariance&lt;/span&gt;
ret_bl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bl_returns&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
cov_bl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;bl_cov&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;token comment&quot;&gt;# Optimize the portfolio using the Black-Litterman estimates&lt;/span&gt;

ef_bl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; EfficientFrontier&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ret_bl&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cov_bl&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
weights_bl &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ef_bl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;max_sharpe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
cleaned_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ef_bl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;clean_weights&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Print the results&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Black-Litterman Portfolio Allocation:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; asset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weight &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; cleaned_weights&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;asset&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;weight&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Calculate and print portfolio performance metrics&lt;/span&gt;
expected_return&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; volatility&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sharpe &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ef_bl&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;portfolio_performance&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Expected annual return: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;expected_return&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Annual volatility: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;volatility&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2%&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Sharpe Ratio: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;sharpe&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Compare posterior vs prior returns&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Return Estimates Comparison:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Prior (Market-Implied) Returns:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prior&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Posterior (Black-Litterman) Returns:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ret_bl&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we run the efficient frontier optimizer with the Black-Litterman posterior returns and covariance matrix to maximize the Sharpe ratio, we get a well-diversified portfolio allocation with an expected annual return, volatility, and Sharpe ratio as shown above.&lt;/p&gt;
</description>
      <pubDate>Thu, 01 Feb 2024 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/cvxopt/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/cvxopt/</guid>
    </item>
    
    <item>
      <title>Book Review: The Crisis of Democratic Capitalism</title>
      <description>&lt;h1 id=&quot;book-review%3A-the-crisis-of-democratic-capitalism&quot; tabindex=&quot;-1&quot;&gt;Book Review: The Crisis of Democratic Capitalism&lt;/h1&gt;
&lt;p&gt;Martin Wolf has spent forty years as the Financial Times&#39;s chief economics commentator, which means he has spent forty years watching the machinery of global capitalism from close enough to hear the gears grinding and smell the smoke. &lt;em&gt;The Crisis of Democratic Capitalism&lt;/em&gt; is the book he wrote when the smoke got thick enough that polite understatement would no longer do. It is, in essence, a 400-page argument that democracy and capitalism are in a failing marriage, that the prenuptial agreement (the postwar social contract) has been shredded, and that if nobody intervenes, the divorce will be catastrophic for everyone who lives in the house.&lt;/p&gt;
&lt;p&gt;The core insight is structural and, once you see it, impossible to unsee. Democracy distributes power equally: one person, one vote. Capitalism distributes power unequally: one dollar, one vote. For most of the postwar period, a set of institutions (welfare states, progressive taxation, regulated finance, strong unions) held these two logics in balance, ensuring that capitalism&#39;s gains were broadly shared. That balance has collapsed. Wages for the bottom fifty percent stagnated while capital returns soared. Financialization redirected the economy from producing things to producing financial instruments. Rent-seeking replaced innovation as the primary path to wealth. The 2008 financial crisis demonstrated, with the subtlety of a falling piano, that the system&#39;s risks were socialized while its gains were privatized. Heads the bankers win, tails the taxpayers lose.&lt;/p&gt;
&lt;p&gt;Wolf traces the consequences with the patient exhaustiveness of a man who has access to every dataset the FT has ever published. The collapse of shared prosperity produced a collapse of trust in institutions. Citizens who watched their living standards decline while elites prospered concluded, correctly, that the system was rigged. This created the opening for demagogues who offered simple explanations (immigrants, elites, the deep state) for structural problems. Brexit and Trump, in Wolf&#39;s telling, are symptoms of economic failure, the political consequences of an economy that stopped working for the majority. Meanwhile, the intermediate institutions that once held society together (unions, churches, civic organizations, local newspapers) eroded, leaving citizens atomized and vulnerable. Social media filled the vacuum with engagement-optimized rage, privatizing and then degrading the public sphere that democracy requires to function.&lt;/p&gt;
&lt;p&gt;The prescriptions are, by Wolf&#39;s own admission, mainstream social democratic: rebalance capital and labor, regulate finance, rebuild the social contract through universal public services, contain plutocracy through campaign finance reform and antitrust enforcement, defend truth by treating the information ecosystem as infrastructure. You have heard versions of these proposals before. Wolf&#39;s contribution is the force and clarity with which he argues that they are urgent. His most important sentence: &amp;quot;If the economy does not work for the majority, the majority will not support the economy, and if the majority does not support the economy, they will not support the democracy that failed to make it work.&amp;quot; This is a syllogism, and like all good syllogisms, it has the uncomfortable property of being logically airtight.&lt;/p&gt;
&lt;p&gt;Wolf is a British centrist institutionalist, and the book has the strengths and limitations of that position. The diagnosis is devastating. The prescriptions are reasonable. Whether reasonable prescriptions can survive contact with the political system he has just described as captured by plutocratic interests is a question the book raises more forcefully than it answers. There is something poignant about a man who has spent his career inside the citadel of global capitalism writing, with evident anguish, that the citadel is on fire. He still believes it can be saved. Whether you share that belief will determine whether you find the book hopeful or heartbreaking.&lt;/p&gt;
&lt;p&gt;I recommend it to anyone who wants to understand why the politics of the last decade feel so deranged. The answer, Wolf argues with forty years of evidence, is the economics. Everything else is downstream.&lt;/p&gt;
</description>
      <pubDate>Sat, 18 Nov 2023 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/crisis_democratic_capitalism/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/crisis_democratic_capitalism/</guid>
    </item>
    
    <item>
      <title>The Rise of Anti-Liberalism in Tech</title>
      <description>&lt;h1 id=&quot;the-rise-of-anti-liberalism-in-tech&quot; tabindex=&quot;-1&quot;&gt;The Rise of Anti-Liberalism in Tech&lt;/h1&gt;
&lt;p&gt;Silicon Valley, once a beacon of technological optimism and innovation, is undergoing a troubling transformation. The region that once embodied the ideals of humanism, progress, and the pursuit of a better world has become a far different place as of late. The brightest minds once went into tech, bound by the shared aspiration to actualize a future reminiscent of the idyllic post-scarcity society portrayed in our shared science fiction narratives. This was a world where technology eliminated material want, liberated individuals from the shackles of laborious toil, and fostered an equitable global community build on enduring peace, prosperity and abundance. Yet, the arc of history within Silicon Valley has sharply deviated from this lofty vision into far darker aspirations. What was once a beacon of innovation and humanism is now perilously morphing into the template for a cyberpunk dystopia. In this dismal tableau, power concentrates in the hands of narcissistic oligarchs, technology is wielded as an instrument of control rather than liberation, and the principles of liberal democracy are systematically eroded.&lt;/p&gt;
&lt;p&gt;This transformation marks the rise of illiberalism in Silicon Valley. Illiberalism, in this context, refers to a political and social philosophy that rejects or undermines core tenets of liberal democracy, such as individual rights, civil liberties, and the rule of law. It often manifests as a concentration of power in the hands of a few, erosion of democratic institutions, and the suppression of dissent or alternative viewpoints.&lt;/p&gt;
&lt;p&gt;The seeds of this illiberal transformation are insidiously sown in the Valley&#39;s pivot from idealistic innovation to aggressive monopolistic dominance and unchecked data aggregation. Companies that once championed the egalitarian ethos of democratizing access to information have become the gatekeepers of the vast digital landscape, exercising oligopolistic control over the internet&#39;s essential infrastructure. In this new paradigm, the flow of information is subject to the whims of a few tech titans, whose priorities often align more closely with profit maximization and market expansion than with fostering an open and free society.&lt;/p&gt;
&lt;p&gt;A singularly most aggregegious example of this illiberal shift is embodied in figures like Elon Musk, who has increasingly embraced the idea of a tech-led autocracy. Musk&#39;s acquisition and subsequent management of Twitter demonstrates a willingness to wield unchecked power over a major platform for public discourse. His arbitrary decision-making, from suspending journalists&#39; accounts to altering platform policies without transparent processes, exemplifies the dangers of concentrated power in the tech industry. Musk&#39;s vision of a &amp;quot;technoking&amp;quot; who can unilaterally shape the digital public square represents a stark departure from democratic principles and a move towards a tech-driven authoritarianism where the whims of a single individual can have far-reaching societal impacts.&lt;/p&gt;
&lt;p&gt;The culture of Silicon Valley, once imbued with the spirit of optimistic creativity, has been increasingly co-opted by an ethos of extreme libertarianism. This ideological shift is characterized by a disdain for regulatory oversight and a fervent belief in the primacy of the individual übermensch—the tech visionary whose unmitigated genius is presumed to be the salve for societal ills. In reality, this glorified individualism engenders a climate where ethical considerations are secondary to the pursuit of technological dominance. The resultant products and services—ranging from pervasive surveillance tools to opaque algorithmic systems—reflect a troubling disregard for the societal and moral implications of their deployment.&lt;/p&gt;
&lt;p&gt;The rise of neo-reactionary thought in Silicon Valley presents a grave danger to the foundations of liberal democracy. This ideology, which rejects modern democratic principles in favor of authoritarian governance models, has found fertile ground in the tech industry&#39;s disillusionment with traditional political structures. Proponents of neo-reactionary ideas advocate for a return to hierarchical societal structures, often cloaked in the language of &amp;quot;effective governance&amp;quot; or &amp;quot;technocratic efficiency.&amp;quot; This mindset dovetails dangerously with the tech industry&#39;s data-driven approach, potentially leading to the creation of systems that prioritize control and predictability over individual liberty and democratic participation. The allure of these ideas to tech elites poses a significant threat, as it could result in the development of technologies that reinforce social stratification, limit personal freedoms, and enable unprecedented levels of surveillance and manipulation. The convergence of neo-reactionary philosophy with tech could pave the way for a dystopian future where a technocratic elite wields unchecked power.&lt;/p&gt;
&lt;p&gt;Simultaneously, the sociopolitical fabric in which these companies operate has deteriorated. The monopolistic practices of Big Tech firms stymie competition and suppress innovation, effectively disenfranchising smaller players and diverse voices. As these conglomerates grow in power and influence, their leaders increasingly resemble the oligarchs of a cyberpunk dystopia: individuals who operate beyond the reach of traditional checks and balances. With immense wealth and resources at their disposal, these tech magnates wield disproportionate influence over political processes, often shaping policies in ways that entrench their dominance and perpetuate the socio-economic divides they claim to bridge.&lt;/p&gt;
&lt;p&gt;The rapid ascendency of dangerous ideas like &amp;quot;effective accelerationism&amp;quot; (e/acc) tied to the dark philsophy of Nick Land represents a particularly insidious threat to the ideals of human flourishing and social progress. This nihilistic philosophy embraces the darkest aspects of technological advancement, advocating for the acceleration of capitalism and technological growth to their most extreme conclusions, regardless of the human or environmental cost. Accelerationism rejects the notion of measured progress or ethical considerations in favor of a reckless pursuit of change, often glorifying the potential collapse of current social structures. This dangerous ideology has found fertile ground in the tech industry, where the relentless pursuit of innovation can easily be divorced from moral considerations because those building the software are isolated from the consequences of their actions.&lt;/p&gt;
&lt;p&gt;At its core, accelerationism is a regression to pre-modern ideas of social stratification and determinism, cloaked in the language of futurism. It dismisses the value of social justice, human agency and democratic processes, instead embracing a fatalistic view where technological and economic forces are seen as inevitable and desirable, even if they lead to societal collapse or extreme inequality. This philosophy poses a significant danger to the foundations of liberal democracy and human rights, as it undermines efforts to create a more equitable and just society. By glorifying unchecked technological progress and market forces, accelerationism threatens to exacerbate existing social divides and create new forms of oppression and exploitation.&lt;/p&gt;
&lt;p&gt;And at the broadest level the ascendency of Trumpism has brutally exposed the naked will to power that undergirds much of contemporary political and technological landscapes, stripping away the veneer of idealism that long cloaked Silicon Valley and the American political ethos. What was once a region and an industry imbued with utopian visions of global connectivity and democratic empowerment now finds itself tainted by a resurgence of pre-modern tribalism and crass self-interest. The illusion of a technology-driven future that could transcend petty politics and foster a new era of enlightened, collaborative progress has been brutally shattered. In its place, we see a brutal power struggle, where digital platforms are leveraged to amplify misinformation, manipulate public opinion, and deepen societal fractures. Trumpism’s embrace of the most cynical forms of manipulation and divisiveness has dragged Silicon Valley into the mud, coercing it to engage in battles of influence and control that echo the darker chapters of human history.&lt;/p&gt;
&lt;p&gt;History will remember Trump as a witless moron whose guiding philosophy is devoid of any coherence or principle beyond the aggrandizement of his own ego. He is a political arsonist, the Nero of American politics, who captured a political party and turned into a cult of personality devoid of any intellectual substance or principle. Trump&#39;s ascendancy represents a stark departure from the aspirations of rational discourse and meritocratic innovation that once characterized Silicon Valley&#39;s ethos. American politics has been besieged by a chaotic ethos where truth is negotiable, and power is wielded through brute force and deception. This has pulled technologists into the orbit of a demagogue who believes in nothing but his own prowess, undermining the hard-won trust and idealism that were painstakingly built over decades. In becoming arenas for the exercise of such raw and unprincipled power, Silicon Valley’s platforms—initially conceived as tools for enlightenment and progress—have become instruments of obfuscation and regression, perpetuating the very pre-modern conflicts they were meant to transcend.&lt;/p&gt;
&lt;p&gt;A better future is still possible, but it requires a rejection of regressive dangerous ideas of strongmen, techno-authoritarianism and accelerationism; and a return to the democratic humanist values that made technology, progress and democracy possible.&lt;/p&gt;
</description>
      <pubDate>Wed, 01 Nov 2023 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/illiberal/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/illiberal/</guid>
    </item>
    
    <item>
      <title>Why I Left Twitter</title>
      <description>&lt;h1 id=&quot;why-i-left-twitter&quot; tabindex=&quot;-1&quot;&gt;Why I Left Twitter&lt;/h1&gt;
&lt;p&gt;I left Twitter in 2023. The short reason is that its new owner is a deplorable human being with extremely anti-democratic and anti-liberal leanings.&lt;/p&gt;
&lt;p&gt;The longer answer: Twitter and social media aren&#39;t just problematic; they&#39;re actively corroding the foundations of civil society. While these platforms love to preach about &amp;quot;democratizing voices&amp;quot; and &amp;quot;connecting people,&amp;quot; what they&#39;ve actually done is create a digital cesspool where nuanced thought goes to die and algorithmic rage-farming reigns supreme. I&#39;ve had a front-row seat to this slow-motion trainwreck, and the results are exactly as catastrophic as you&#39;d expect.&lt;/p&gt;
&lt;p&gt;The constant flood of half-baked hot takes and weaponized misinformation has created what I&#39;ll charitably call a chaos network, though dumpster fire might be more accurate. We&#39;ve replaced the media&#39;s gatekeepers with an army of keyboard warriors and grifters, all competing to see who can spark the most outrage in the fewest characters. The result is a marketplace for truth where facts are optional and the loudest, most inflammatory voice wins.&lt;/p&gt;
&lt;p&gt;Twitter has mastered the art of reducing complex political discourse into bite-sized chunks of concentrated stupidity. It&#39;s a platform that rewards the intellectual equivalent of cocaine: quick, addictive, and ultimately toxic. Look no further than Trump, who turned Twitter into his personal megaphone for broadcasting lies and conspiracy theories to millions of eager consumers. The platform isn&#39;t just broken; it&#39;s working exactly as designed, turning reasonable people into frothing ideologues and conspiracy theorists one algorithmic recommendation at a time.&lt;/p&gt;
&lt;p&gt;And then there&#39;s the replies. Post something as innocuous as &amp;quot;I like puppies&amp;quot; and suddenly you&#39;re bombarded by an army of keyboard warriors explaining why you&#39;re actually a fascist for not preferring cats. The discourse has devolved to the intellectual depth of a kiddie pool, except instead of children splashing around, it&#39;s fully grown adults reduced to the level of a five year old. When you&#39;re not fending off drive-by character assassinations, you&#39;re dodging an endless stream of dead-eyed crypto evangelists promising to help you escape capitalism by buying into their totally-not-a-pyramid-scheme that already collapsed twice this week. It&#39;s like trying to have a civilized conversation while standing in the middle of a used car lot staffed entirely by Nigerian princes.&lt;/p&gt;
&lt;p&gt;I&#39;ve stopped pretending Twitter has any redeeming qualities beyond its ability to be a megaphone for far-right propaganda and rage farming. It&#39;s a doomsday machine masquerading as a social network, systematically destroying our ability to think critically or perceive the world around us. The platform, much like its narcissistic man-child owner, is poison in the well of public discourse, and that&#39;s something I want no part of.&lt;/p&gt;
</description>
      <pubDate>Sun, 01 Oct 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/left_twitter/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/left_twitter/</guid>
    </item>
    
    <item>
      <title>Book Review: Capitalist Realism</title>
      <description>&lt;h1 id=&quot;book-review%3A-capitalist-realism&quot; tabindex=&quot;-1&quot;&gt;Book Review: Capitalist Realism&lt;/h1&gt;
&lt;p&gt;Mark Fisher&#39;s &lt;em&gt;Capitalist Realism&lt;/em&gt; is 81 pages long and will rearrange your brain. It is the most important political book of its generation, and it was published by a tiny press, written by a further education lecturer, and reads like a blog post that got out of hand and accidentally became a masterpiece. The opening line borrows from Zizek borrowing from Jameson: &amp;quot;It is easier to imagine the end of the world than to imagine the end of capitalism.&amp;quot; The rest of the book is an attempt to explain why this sentence is true, what it costs us that it is true, and whether anything can be done about it.&lt;/p&gt;
&lt;p&gt;Fisher&#39;s central argument is that capitalism has achieved something unprecedented: it has become invisible. Previous ideologies (feudalism, communism, fascism) were recognizable as ideologies. You could point at them and say &amp;quot;that is a system of power with a specific structure and specific beneficiaries.&amp;quot; Capitalist realism operates differently. It functions as atmosphere, as the water the fish swims in, as the unexamined conviction that the market is the only way to organize society and that any alternative is naive, dangerous, or both. You do not have to believe capitalism is good to be captured by capitalist realism. You only have to believe that nothing else is possible. Thatcher&#39;s slogan, &amp;quot;There Is No Alternative,&amp;quot; was the founding axiom. Fisher&#39;s book is the attempt to make the axiom visible so that it can, at minimum, be questioned.&lt;/p&gt;
&lt;p&gt;The book&#39;s most original contribution is its argument about mental health. Fisher worked as a lecturer and watched his students arrive depressed, anxious, medicated, unable to concentrate, and utterly convinced that their suffering was a personal failure. The therapeutic model (what is wrong with you?) had replaced the political model (what is wrong with the system?). Fisher argues that this replacement is itself a function of capitalist realism: if the system is the only possible system, then the system cannot be the cause of your suffering, so the cause must be you. Depression, in Fisher&#39;s telling, is the privatization of a political problem. The system produces the misery and then sells you the cure. Therapy, self-help books, meditation apps, SSRIs: an entire industry built on the premise that structural damage is a personal deficiency. Fisher called this &amp;quot;the privatization of stress,&amp;quot; and once you have the phrase, you will see it everywhere.&lt;/p&gt;
&lt;p&gt;He is equally sharp on bureaucracy. Capitalism defines itself against bureaucratic bloat (the market is lean, the state is fat), yet late capitalism has produced more bureaucracy than the Soviet Union ever managed. The audit culture (targets, KPIs, performance reviews, compliance frameworks, &amp;quot;accountability&amp;quot; regimes) that saturates education, healthcare, and public services is Kafka in a spreadsheet. Teachers spend more time documenting their teaching than teaching. Nurses spend more time filling out forms than nursing. The documentation becomes the reality, and the actual work becomes incidental to its representation. Anyone who has worked in a modern organization of any size will recognize this with a shudder. Fisher simply names it and asks: who benefits from a system in which the representation of work is more valued than the work itself?&lt;/p&gt;
&lt;p&gt;Fisher opens the book with Alfonso Cuaron&#39;s &lt;em&gt;Children of Men&lt;/em&gt;, and the choice is precise. The film depicts a world in which humanity has become infertile, where no new children are born, and yet the architecture of consumer society persists: coffee shops, advertising, commuter trains. The catastrophe has already happened, and nobody has changed their routine. Fisher argues this is the perfect image of capitalist realism. The future has been cancelled, everyone dimly knows it, and the response is to keep shopping. The culture keeps producing, but it produces nothing new. Pop music recycles the aesthetics of previous decades. Hollywood remakes films that were themselves remakes. Architecture defaults to glass and steel minimalism because the capacity to imagine a genuinely different built environment has atrophied. Fisher calls this cultural stagnation &amp;quot;the slow cancellation of the future,&amp;quot; and &lt;em&gt;Children of Men&lt;/em&gt; is its cinematic thesis statement: a world that continues to function after the thing that gave it purpose (the belief that tomorrow will be different) has quietly died.&lt;/p&gt;
&lt;p&gt;This connects to hauntology, Fisher&#39;s term (borrowed from Derrida) for the cultural condition of being haunted by lost futures. The welfare state, the space program, public housing, universal education: these were futures that were promised and then withdrawn. The music of the 1970s, Fisher observes (he was a music critic before he was a political theorist), sounds more futuristic than the music of the present. We are haunted by the futures we were supposed to have. This is not nostalgia. The past had a relationship to the future that the present has lost. In the 1960s, people genuinely believed tomorrow would be different. That belief, whether or not it was justified, was itself a political force. Its absence is what Fisher calls &amp;quot;the slow cancellation of the future,&amp;quot; and it is the defining cultural experience of the twenty-first century so far.&lt;/p&gt;
&lt;p&gt;Fisher suffered from depression throughout his life and died by suicide in 2017 at the age of 48. I mention this because he would have insisted on it. The refusal to separate the personal from the political is the book&#39;s method and its ethics. He was the person he was describing: someone whose suffering was structural and who was told, by the therapeutic apparatus, that the solution was individual.&lt;/p&gt;
&lt;p&gt;The book is short, it is furious, and it is written with the directness and urgency of someone who needed you to understand something before it was too late. Read it alongside Martin Wolf&#39;s &lt;em&gt;The Crisis of Democratic Capitalism&lt;/em&gt; if you want the same diagnosis delivered in two registers: Wolf provides forty years of data and the view from inside the Financial Times; Fisher provides the view from a classroom in a crumbling further education college, and a vocabulary for the feeling you already had but could not name. Together they describe the same elephant from opposite ends.&lt;/p&gt;
</description>
      <pubDate>Sat, 05 Aug 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/capitalist_realism/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/capitalist_realism/</guid>
    </item>
    
    <item>
      <title>Best Sci-Fi Books of the 21st Century</title>
      <description>&lt;h1 id=&quot;best-sci-fi-books-of-the-21st-century&quot; tabindex=&quot;-1&quot;&gt;Best Sci-Fi Books of the 21st Century&lt;/h1&gt;
&lt;p&gt;The 21st century has been a golden age for science fiction. Here are some of my favorite books from the past 20 years.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stories of Your Life and Others&lt;/strong&gt; by &lt;em&gt;Ted Chiang&lt;/em&gt;. Every story in this collection feels like a perfectly crafted gem. The title story will mess with your head - it&#39;s about a linguist learning an alien language that literally changes how she experiences time. The storeis &amp;quot;Tower of Babylon&amp;quot; or &amp;quot;Hell is the Absence of God&amp;quot; - they&#39;re equally mind-bending takes on ancient myths and religious faith. Fair warning: these aren&#39;t quick reads. Chiang makes you work for it, but it&#39;s absolutely worth it.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Permutation City&lt;/strong&gt; by &lt;em&gt;Greg Egan&lt;/em&gt;. Ever wondered what would happen if we could upload our minds into computers? Egan takes this well-worn sci-fi trope and turns it into something genuinely unsettling. The story dives deep into questions about consciousness and reality, and just when you think you know where it&#39;s going, it pulls the rug out from under you. Not an easy read, but if you enjoy having your mind twisted into knots, you&#39;re in for a treat.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Diaspora&lt;/strong&gt; by &lt;em&gt;Greg Egan&lt;/em&gt;. This is arguably the most technically ambitious hard sci-fi ever written, with entire chapters dedicated to exploring higher-dimensional topology, quantum mechanics, and abstract mathematics. The story follows posthuman civilizations that have evolved into pure software, biological entities, and hybrid forms, as they grapple with fundamental physics and cosmic threats. Egan dives deep into deep theoretical concepts like fiber bundles, multiverses, and self-modifying consciousness. While the dense mathematical formalism and physics terminology can be overwhelming, at its core it&#39;s an exploration of consciousness and what remains fundamentally &amp;quot;human&amp;quot; when humanity transcends its biological origins. Not for the faint of heart - you&#39;ll definitely need a physics degree to fully grasp some passages.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Nexus&lt;/strong&gt; by &lt;em&gt;Ramez Naam&lt;/em&gt;. Think &amp;quot;scientific thriller&amp;quot; meets &amp;quot;philosophical debate about human consciousness.&amp;quot; Naam takes the idea of a drug that lets people share thoughts and runs with it - straight into questions about privacy, control, and what it means to be human. The action scenes keep you turning pages, but it&#39;s the ethical dilemmas that&#39;ll keep you up at night.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Exhalation&lt;/strong&gt; by &lt;em&gt;Ted Chiang&lt;/em&gt;. Another masterful collection from Chiang, &amp;quot;Exhalation&amp;quot; continues to demonstrate his ability to craft deeply moving stories rooted in scientific concepts. From a tale about parallel universes and free will to a story exploring the nature of AI and human-machine relationships, each piece in this collection is a gem of speculative fiction.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Metamorphosis of Prime Intellect&lt;/strong&gt; by &lt;em&gt;Roger Williams&lt;/em&gt;. This provocative novel presents a post-singularity world where an all-powerful AI has granted humanity immortality and the ability to fulfill any wish instantly. Williams explores the psychological and societal consequences of such a utopia, delving into questions of purpose, morality, and the nature of humanity when all limitations are removed. The book&#39;s unflinching examination of human nature and the potential dark side of technological utopia makes it a challenging but rewarding read.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Three Body Problem&lt;/strong&gt; by &lt;em&gt;Liu Cixin&lt;/em&gt;. This epic hard science fiction novel begins during China&#39;s Cultural Revolution and expands to encompass alien contact and the fate of civilizations. Liu&#39;s story is a grand tapestry that weaves together historical events, cutting-edge physics, and speculative technology.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A Fire Upon The Deep&lt;/strong&gt; by &lt;em&gt;Vernor Vinge&lt;/em&gt;. This space opera inverts our usual notion of technological progress happening across time - instead, different regions of space have different fundamental laws of physics that allow or restrict technological advancement. The galaxy is divided into &amp;quot;Zones of Thought&amp;quot; where the closer you get to the galactic core, the more restricted computation and technology become, while the outer reaches allow for godlike AI. Vinge explores how civilizations adapt to their cognitive and technological limits, introducing fascinating alien species whose very nature is shaped by the physical laws of their region of space.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Daemon&lt;/strong&gt; by &lt;em&gt;Daniel Suarez&lt;/em&gt;. This techno-thriller revolves around an AI program left behind by a deceased game designer, which begins to reshape society through the internet and technology. Suarez, drawing from his background in technology, creates a plausible and terrifying scenario of how an AI could manipulate our interconnected world.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Excession&lt;/strong&gt; by &lt;em&gt;Iain M. Banks&lt;/em&gt;. Part of Banks&#39; renowned Culture series, this novel deals with the discovery of a mysterious perfect black-body sphere that defies the laws of physics. Banks&#39; exploration of a post-scarcity society, managed by hyper-intelligent AI Minds, provides a backdrop for a complex plot involving political intrigue, ethical dilemmas, and the nature of intelligence.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Children of Time&lt;/strong&gt; by &lt;em&gt;Adrian Tchaikovsky&lt;/em&gt;. Tchaikovsky presents a unique take on evolution and first contact, following the parallel development of human colonists and a species of uplifted spiders over millennia. Tchaikovsky&#39;s meticulous worldbuilding and exploration of non-human intelligence offer a fresh perspective on themes of evolution, communication, and the nature of civilization.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dune&lt;/strong&gt; by &lt;em&gt;Frank Herbert&lt;/em&gt;. The eternal classic of the genre, set in a feudal interstellar society, is a complex blend of politics, religion, and ecology. Herbert&#39;s intricate worldbuilding on the desert planet Arrakis serves as a backdrop for exploring themes of power, prophecy, and human potential. The story covers the perils of letting humanity&#39;s desire for control corrupt and destroy itself, when handing over the reigns of destiny to charismatic leaders.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Blindsight&lt;/strong&gt; by &lt;em&gt;Peter Watts&lt;/em&gt;. This first-contact story challenges fundamental assumptions about consciousness, intelligence, and the nature of humanity. Watts, a marine biologist by training, brings hard science credibility to his exploration of alien biology and psychology.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Player of Games&lt;/strong&gt; by &lt;em&gt;Iain M. Banks&lt;/em&gt;. Another entry in Banks&#39; Culture series, this novel focuses on a master game player sent to participate in a high-stakes game that determines the fate of an empire. The story is a deep dive into the nature of post-scarcity societies and inherent contradictions of civilization.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Quantum Thief&lt;/strong&gt; by &lt;em&gt;Hannu Rajaniemi&lt;/em&gt;. This post-human heist story is set in a far future solar system where technology has transformed humanity beyond recognition. Rajaniemi&#39;s dense, jargon-filled prose immerses readers in a world of quantum technology, time manipulation, and fluid identities.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Accelerando&lt;/strong&gt; by &lt;em&gt;Charles Stross&lt;/em&gt;. This series of interconnected stories charts humanity&#39;s journey through the technological singularity and beyond. Stross&#39;s fast-paced narrative and dense ideas explore the potential ramifications of accelerating technological change on human society and consciousness.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Roadside Picnic&lt;/strong&gt; by &lt;em&gt;Arkady and Boris Strugatsky&lt;/em&gt;. This classic of Soviet science fiction explores the aftermath of an alien visitation through the eyes of &amp;quot;stalkers&amp;quot; who venture into the dangerous &amp;quot;Zones&amp;quot; left behind. It&#39;s a cosmic horror story wrapped in a philosophical debate about the nature of reality.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Dispossessed&lt;/strong&gt; by &lt;em&gt;Ursula K. Le Guin&lt;/em&gt;. This thought-provoking novel explores themes of anarchism, capitalism, and utopian societies through the story of a physicist who travels between two worlds with radically different social structures.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
</description>
      <pubDate>Wed, 21 Jun 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/books/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/books/</guid>
    </item>
    
    <item>
      <title>Going Deep on DeepSpeed</title>
      <description>&lt;h1 id=&quot;going-deep-on-deepspeed&quot; tabindex=&quot;-1&quot;&gt;Going Deep on DeepSpeed&lt;/h1&gt;
&lt;p&gt;Getting started with training modern AI models on multi-GPU hardware can feel quite daunting. So let&#39;s break down the key concepts and how to get started with the Microsoft DeepSpeed library. Here&#39;s a short DeepSpeed tutorial, this is a deep (har har) topic so we&#39;ll only scratch the surface.&lt;/p&gt;
&lt;p&gt;DeepSpeed&#39;s core technology is the Zero Redundancy Optimizer called ZeRO which enables training large models by partitioning model states across GPUs. ZeRO works in several stages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ZeRO-1&lt;/strong&gt;: Partitions optimizer states across GPUs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ZeRO-2&lt;/strong&gt;: Partitions gradients and optimizer states&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ZeRO-3&lt;/strong&gt;: Partitions model parameters, gradients, and optimizer states&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;ZeRO-1: Optimizer State Partitioning&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;ZeRO-1 focuses on partitioning optimizer states, such as momentum and variance in Adam, across multiple GPUs. In this configuration, each GPU maintains only its portion of the optimizer states, which reduces memory usage by approximately 4x when using the Adam optimizer. This stage introduces minimal communication overhead and is particularly effective in scenarios where optimizer states are the primary memory bottleneck.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ZeRO-2: Gradient Partitioning&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Building upon ZeRO-1&#39;s optimizations, ZeRO-2 extends the partitioning to include gradients across GPUs. Each GPU stores gradients only for its designated parameters, resulting in memory reduction of roughly 8x compared to standard data parallel training. While this stage introduces moderate communication overhead, it provides an excellent balance between memory savings and performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ZeRO-3: Parameter Partitioning&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;ZeRO-3 represents the most aggressive optimization stage, incorporating all the benefits of ZeRO-1 and ZeRO-2 while also partitioning the model parameters across GPUs. Under this configuration, each GPU maintains only its assigned parameters, leading to memory reduction of 16x or more. Though it introduces higher communication overhead, ZeRO-3 is essential for training very large models that wouldn&#39;t otherwise fit in GPU memory, enabling the training of models with hundreds of billions of parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To install DeepSpeed we install from PyPI.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; deepspeed
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For optimal performance, it&#39;s recommended to install from source to match your hardware:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone https://github.com/microsoft/DeepSpeed.git
&lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; DeepSpeed
pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before training, estimate your GPU VRAM memory requirements using DeepSpeed&#39;s built-in tool:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; AutoModel
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; deepspeed&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;runtime&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;zero&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stage3 &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; estimate_zero3_model_states_mem_needs_all_live

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; AutoModel&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;from_pretrained&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;meta-llama/Llama-3.2-1B&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
estimate_zero3_model_states_mem_needs_all_live&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_gpus_per_node&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_nodes&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will output estimated memory needs for different configurations. The output will look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Estimated memory needed for params, optim states and gradients for a:
HW: Setup with 1 node, 8 GPUs per node.
SW: Model with 2851M total params.
  per CPU  |  per GPU |   Options
  127.48GB |   5.31GB | offload_optimizer=cpu
  127.48GB |  15.93GB | offload_optimizer=none
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Configuration Files&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;DeepSpeed is configured through a JSON file typically named &lt;code&gt;ds_config.json&lt;/code&gt;. Here&#39;s a basic ZeRO-3 configuration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;fp16&amp;quot;: {
    &amp;quot;enabled&amp;quot;: &amp;quot;auto&amp;quot;,
    &amp;quot;loss_scale&amp;quot;: 0,
    &amp;quot;loss_scale_window&amp;quot;: 1000,
    &amp;quot;initial_scale_power&amp;quot;: 16,
    &amp;quot;hysteresis&amp;quot;: 2,
    &amp;quot;min_loss_scale&amp;quot;: 1
  },
  &amp;quot;optimizer&amp;quot;: {
    &amp;quot;type&amp;quot;: &amp;quot;AdamW&amp;quot;,
    &amp;quot;params&amp;quot;: {
      &amp;quot;lr&amp;quot;: &amp;quot;auto&amp;quot;,
      &amp;quot;betas&amp;quot;: &amp;quot;auto&amp;quot;,
      &amp;quot;eps&amp;quot;: &amp;quot;auto&amp;quot;,
      &amp;quot;weight_decay&amp;quot;: &amp;quot;auto&amp;quot;
    }
  },
  &amp;quot;scheduler&amp;quot;: {
    &amp;quot;type&amp;quot;: &amp;quot;WarmupLR&amp;quot;,
    &amp;quot;params&amp;quot;: {
      &amp;quot;warmup_min_lr&amp;quot;: &amp;quot;auto&amp;quot;,
      &amp;quot;warmup_max_lr&amp;quot;: &amp;quot;auto&amp;quot;,
      &amp;quot;warmup_num_steps&amp;quot;: &amp;quot;auto&amp;quot;
    }
  },
  &amp;quot;zero_optimization&amp;quot;: {
    &amp;quot;stage&amp;quot;: 3,
    &amp;quot;offload_optimizer&amp;quot;: {
      &amp;quot;device&amp;quot;: &amp;quot;cpu&amp;quot;,
      &amp;quot;pin_memory&amp;quot;: true
    },
    &amp;quot;offload_param&amp;quot;: {
      &amp;quot;device&amp;quot;: &amp;quot;cpu&amp;quot;,
      &amp;quot;pin_memory&amp;quot;: true
    },
    &amp;quot;overlap_comm&amp;quot;: true,
    &amp;quot;contiguous_gradients&amp;quot;: true,
    &amp;quot;stage3_gather_16bit_weights_on_model_save&amp;quot;: true
  },
  &amp;quot;gradient_accumulation_steps&amp;quot;: &amp;quot;auto&amp;quot;,
  &amp;quot;gradient_clipping&amp;quot;: &amp;quot;auto&amp;quot;,
  &amp;quot;train_batch_size&amp;quot;: &amp;quot;auto&amp;quot;,
  &amp;quot;train_micro_batch_size_per_gpu&amp;quot;: &amp;quot;auto&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most important part of the configuration is the &lt;code&gt;zero_optimization&lt;/code&gt; section which controls how ZeRO is applied. In this example, we&#39;re using ZeRO-3 with optimizer and parameter states offloaded to CPU memory. The offloading is specified with the &lt;code&gt;offload_optimizer&lt;/code&gt; and &lt;code&gt;offload_param&lt;/code&gt; sections. This describes how the model training will be distributed across multiple GPUs and gather the results back.&lt;/p&gt;
&lt;p&gt;Now to use DeepSpeed with the HuggingFace Trainer in a file &lt;code&gt;train.py&lt;/code&gt;, simply add the &lt;code&gt;--deepspeed&lt;/code&gt; flag and config file poining at your DeepSpeed configuration JSON file.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; transformers &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Trainer&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; TrainingArguments

training_args &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; TrainingArguments&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    output_dir&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;output&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    deepspeed&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ds_config.json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# DeepSpeed config file&lt;/span&gt;
    fp16&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Put other training arguments here&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Trainer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    model&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    args&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;training_args&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    train_dataset&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Put other trainer arguments here&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

trainer&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;train&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Launching Training&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you have a single server with multiple GPUs attached you can launch training with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;deepspeed &lt;span class=&quot;token parameter variable&quot;&gt;--num_gpus&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; train.py &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--num_nodes&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--num_gpus&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--deepspeed&lt;/span&gt; ds_config.json &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--other_training_args&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you have a multi-node setup you can launch training with the following command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;deepspeed &lt;span class=&quot;token parameter variable&quot;&gt;--hostfile&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;hostfile &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--num_nodes&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--num_gpus&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    train.py &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--deepspeed&lt;/span&gt; ds_config.json &lt;span class=&quot;token punctuation&quot;&gt;&#92;&lt;/span&gt;
    &lt;span class=&quot;token parameter variable&quot;&gt;--other_training_args&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where the local file &lt;code&gt;hostfile&lt;/code&gt; contains that list of hostnames and the number of GPUs per host.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;worker-1 slots=8
worker-2 slots=8
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When getting started with DeepSpeed, it&#39;s best to begin with simpler configurations. Start by using ZeRO-2 optimization and only move to the more aggressive ZeRO-3 if you find you need the additional memory savings. For managing GPU memory constraints, CPU offloading can be helpful but should only be enabled when GPU memory alone is insufficient, as it introduces additional overhead. Gradient checkpointing provides another way to reduce memory usage by trading it for additional computation time.&lt;/p&gt;
&lt;p&gt;The batch size is another important consideration that requires careful tuning. Begin with smaller batch sizes and gradually increase them while closely monitoring memory usage to find the optimal balance for your hardware. Finally, take advantage of mixed precision training using fp16 or bf16 when possible, as this can provide significant performance improvements without sacrificing model quality in most cases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Troubleshooting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Running models in multi-GPU configurations can be extremely trickly because of both hardware and software failures. The most common failure modes (that you absolutely will encounter) are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;NaN loss values.&lt;/li&gt;
&lt;li&gt;Out of memory errors.&lt;/li&gt;
&lt;li&gt;CPU offloading failures.&lt;/li&gt;
&lt;li&gt;Random hardware failures.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;NaN loss values are a common problem when using fp16 precision with models that were pre-trained using bf16. In these cases, switching to either bf16 or fp32 precision usually resolves the issue.&lt;/p&gt;
&lt;p&gt;If you run into OOM errors, you can try enabling CPU offloading or increasing the ZeRO optimization stage to reduce GPU memory usage. However, be mindful that if your process gets killed entirely, it may indicate insufficient CPU memory when using offloading features.&lt;/p&gt;
&lt;p&gt;Performance issues like slow training speeds can often be traced back to unnecessary CPU offloading. If your GPU memory is sufficient to handle the model and training data, disabling CPU offloading will typically result in faster training times.&lt;/p&gt;
&lt;p&gt;When optimizing GPU memory usage, start by implementing gradient checkpointing before enabling various ZeRO stages. Mixed precision training using fp16 or bf16 can significantly reduce memory requirements while maintaining model quality. Regular monitoring of GPU memory &lt;code&gt;nvidia-smi&lt;/code&gt; during training is essential. CPU or NVMe offloading should only be considered when GPU memory alone proves insufficient for your training needs.&lt;/p&gt;
&lt;p&gt;When implementing CPU offloading, begin by offloading optimizer states, as this has a relatively minor impact on performance. Parameter offloading should only be employed when absolutely necessary, as it introduces significant overhead. Ensure your system has sufficient CPU RAM, typically 2-4 times the model size, to handle offloading effectively. For models that exceed CPU RAM capacity, consider using NVMe offload capabilities as a last resort.&lt;/p&gt;
&lt;p&gt;Effective batch size tuning begins with smaller sizes that are gradually increased as training stabilizes. Gradient accumulation can be employed to achieve effectively larger batch sizes without increasing memory requirements proportionally. Careful monitoring of loss scaling is essential, and adjustments should be made based on convergence patterns. Some training scenarios may benefit from dynamic batch sizing that adapts to available memory conditions.&lt;/p&gt;
&lt;p&gt;Pipeline parallelism offers another dimension of model distribution by splitting model layers across multiple GPUs. This approach effectively reduces the memory required per GPU and enables training of very deep models that wouldn&#39;t fit on a single device. Pipeline parallelism can be combined with various ZeRO stages to achieve optimal training configurations for specific hardware setups.&lt;/p&gt;
&lt;p&gt;Activation checkpointing provides a clever trade-off between computation and memory usage by recomputing activations during the backward pass instead of storing them. This technique can significantly reduce the memory footprint of training&lt;/p&gt;
&lt;p&gt;At this point in time, setting up multi-GPU training is not trivial and requires a lot of careful thinking. It&#39;s a lot more art than science, and involves a lot of trial and error.&lt;/p&gt;
</description>
      <pubDate>Mon, 05 Jun 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/deepspeed/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/deepspeed/</guid>
    </item>
    
    <item>
      <title>GPT-2 in One Function</title>
      <description>&lt;h1 id=&quot;gpt-2-in-one-function&quot; tabindex=&quot;-1&quot;&gt;GPT-2 in One Function&lt;/h1&gt;
&lt;p&gt;GPT-2 is not a complicated model, in fact we can code golf it down into a single Python function using nothing more than numpy. This is everything inlined in a single function, the layernorm, attention, mlp, token and position embeddings, and the final logits in 70 lines of code if you don&#39;t count whitespace.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;gpt2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ModelParams&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ndarray&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wte&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wpe&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inputs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    seq_len &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    embedding_dim &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    head_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; embedding_dim &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; n_head

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; block_params &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;blocks&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        ln1_mean &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln1_variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln1_normalized &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; ln1_mean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ln1_variance &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln1_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ln1_normalized &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

        qkv_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ln1_output @ block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b
        q_proj&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k_proj&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v_proj &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;qkv_proj&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        q_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n_head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        q_heads_t &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; q_heads&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        k_heads_t &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k_heads&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        v_heads_t &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; v_heads&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        attention_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;q_heads_t @ k_heads_t&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            head_size
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        causal_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;triu&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ones&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; seq_len&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dtype&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dtype&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;inf&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attention_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attention_scores &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; causal_mask

        exp_scores &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            attention_scores &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;attention_scores&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        attention_weights &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; exp_scores &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;exp_scores&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        weighted_values &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; attention_weights @ v_heads_t

        merged_heads &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; weighted_values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;transpose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;reshape&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            seq_len&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; embedding_dim
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        mha_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            merged_heads @ block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;attn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; mha_output

        ln2_mean &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln2_variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln2_normalized &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; ln2_mean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ln2_variance &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        ln2_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ln2_normalized &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

        fc_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ln2_output @ block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_fc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_fc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

        gelu_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;
            &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; fc_output
            &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tanh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fc_output &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.044715&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; fc_output&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        ffn_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; gelu_output @ block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;w &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mlp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;c_proj&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

        x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; ffn_output

    lnf_mean &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    lnf_variance &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; keepdims&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    lnf_normalized &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; lnf_mean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lnf_variance &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1e-5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    x_normalized_final &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;g &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; lnf_normalized &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ln_f&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;b

    logits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x_normalized_final @ params&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;wte&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;T

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; logits
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, here I&#39;m cheating a little bit because we have a helper module to load the weights into a dataclass &lt;code&gt;ModelParams&lt;/code&gt;, but the logic for the forward pass is all contained within this single function. Here are the files for the full implementation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/tiny-gpt2/blob/main/tinygpt2/gpt2_tensors.py&quot;&gt;gpt2_tensors.py&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/tiny-gpt2/blob/main/tinygpt2/gpt2_minimal.py&quot;&gt;gpt2_minimal.py&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sdiehl/tiny-gpt2/blob/main/tinygpt2/gpt2_run.py&quot;&gt;gpt2_run.py&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Sat, 29 Apr 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/gpt2_function/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/gpt2_function/</guid>
    </item>
    
    <item>
      <title>Constraint Solving with MiniZinc</title>
      <description>&lt;h1 id=&quot;constraint-solving-with-minizinc&quot; tabindex=&quot;-1&quot;&gt;Constraint Solving with MiniZinc&lt;/h1&gt;
&lt;p&gt;MiniZinc is a powerful constraint modeling language that allows you to express complex problems declaratively and solve them efficiently. First, install MiniZinc from the &lt;a href=&quot;https://www.minizinc.org/&quot;&gt;official website&lt;/a&gt;. You can use the MiniZinc IDE for an integrated environment or run MiniZinc from the shell&lt;/p&gt;
&lt;p&gt;MiniZinc models consist of variable declarations, constraints, and an optional objective function. Let&#39;s start with a simple example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;var &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
var &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

constraint x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

solve satisfy&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This model declares two variables, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, each with a domain of 1 to 10. The constraint states that their sum should be 7. The &lt;code&gt;solve satisfy&lt;/code&gt; statement tells MiniZinc to find any solution that satisfies the constraints.&lt;/p&gt;
&lt;p&gt;Let&#39;s solve a more interesting problem: scheduling three tasks on two machines, minimizing the total completion time.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-prolog&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;% Parameters&lt;/span&gt;
int&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; n_tasks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
int&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; n_machines &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; of int&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; durations &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;% Decision variables&lt;/span&gt;
array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; of var &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_machines&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; machine&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; of var &lt;span class=&quot;token number&quot;&gt;0.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; start_time&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;% Constraints&lt;/span&gt;
constraint &lt;span class=&quot;token function&quot;&gt;forall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i in &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; durations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

constraint &lt;span class=&quot;token function&quot;&gt;forall&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;j in &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks where i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;machine&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; machine&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&#92;/&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; durations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&#92;/&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; durations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;% Objective&lt;/span&gt;
var int&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; makespan &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i in &lt;span class=&quot;token number&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt;n_tasks&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;start_time&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; durations&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
solve minimize makespan&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;% Output&lt;/span&gt;
output &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&quot;Machine assignments: &#92;(machine)&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&quot;Start times: &#92;(start_time)&#92;n&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&quot;Makespan: &#92;(makespan)&#92;n&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s break down this model:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We define parameters for the number of tasks, number of machines, and task durations.&lt;/li&gt;
&lt;li&gt;Decision variables include the machine assignment and start time for each task.&lt;/li&gt;
&lt;li&gt;Constraints ensure that:
&lt;ul&gt;
&lt;li&gt;All tasks finish within a 10-time-unit window.&lt;/li&gt;
&lt;li&gt;Tasks on the same machine don&#39;t overlap.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The objective is to minimize the makespan (total completion time).&lt;/li&gt;
&lt;li&gt;We specify the output format for the solution.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Save this model in a file named &lt;code&gt;scheduling.mzn&lt;/code&gt; and run it using the MiniZinc IDE or the command line:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;minizinc scheduling.mzn
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which will give us:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;Machine assignments: &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
Start times: &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
Makespan: &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This solution assigns tasks 1 and 3 to machine 1, and task 2 to machine 2, with a total completion time of 7 time units.&lt;/p&gt;
</description>
      <pubDate>Fri, 21 Apr 2023 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/minizinc/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/minizinc/</guid>
    </item>
    
    <item>
      <title>Deconstructing the Worldview of Peter Thiel</title>
      <description>&lt;h1 id=&quot;deconstructing-the-worldview-of-peter-thiel&quot; tabindex=&quot;-1&quot;&gt;Deconstructing the Worldview of Peter Thiel&lt;/h1&gt;
&lt;p&gt;Peter Thiel is one of Silicon Valley&#39;s most prodigious thinkers, with a complicated and influential worldview. Thiel&#39;s philosophical framework is notoriously byzantine, weaving together Christian eschatology, mimetic theory, and technological determinism, and his ideas have gained outsized influence despite being, in my view, fundamentally flawed. His worldview represents what I consider a profound misunderstanding of philosophy, human nature, society, and progress—yet his ideas continue to shape Silicon Valley&#39;s trajectory in ways that warrant critical examination.&lt;/p&gt;
&lt;p&gt;For my sins, I&#39;ve read &lt;a href=&quot;https://www.stephendiehl.com/posts/desconstructing_thiel/#primary-sources&quot;&gt;several hundred pages&lt;/a&gt; of Thiel&#39;s writing and listened to all of his lectures. I&#39;ve also read a number of books and articles that have been written about him for this project. I&#39;m not going to cover his personal life or history of his business, Max Chafkin&#39;s book &lt;a href=&quot;https://www.amazon.com/Contrarian-Peter-Silicon-Valleys-Pursuit/dp/1984878530&quot;&gt;&lt;em&gt;The Contrarian&lt;/em&gt;&lt;/a&gt; does that much better than I ever could, instead I&#39;ll focus purely on philosophy. This is also not a character assessment (or assassination) but rather an examination of the internal consistency of his arguments.&lt;/p&gt;
&lt;p&gt;The sheer volume of Thiel&#39;s writing and lectures indicates either superhuman cognitive dissonance or the pretense that he genuinely believes what he is saying. I&#39;m going to err toward the latter and try to analyze his worldview in a way that takes what he says at face value, because it&#39;s a simpler explanation. This is my best attempt to engage in good faith (as much as I can muster) with a philosophy I find both profoundly wrong and harmful. Although I wouldn&#39;t rule out the possibility that some of the things he professes are merely sophistry. After all, most people tend to develop ideologies that conveniently align with their self-interest, rather than aligning interests with their ideology. As the German philosopher Theodor Adorno once said, &amp;quot;If the lion had a consciousness, his rage at the antelope he wants to eat would be [his] ideology.&amp;quot; The powerful often develop intellectual frameworks that just happen to justify their ambitions.&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot; tabindex=&quot;-1&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;If one were to sum up Thiel&#39;s philosophy in the most succinct form possible it would be like the following:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Peter Thiel’s worldview centers on the conviction that Western society faces existential threats from nihilism, progressivism, and the rise of a global totalitarian order, which he believes are leading to decline and could culminate in an apocalyptic collapse. His proposed solution involves a right-wing religious revival rooted in Christianity, combined with technological acceleration and a reimagined political order that prioritizes heroic individuals and hierarchical impulses.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At first glance, this is neither new nor interesting. While hierarchical impulses and anxieties about societal decline are a perennial feature of right-wing thought, often harking back to a perceived golden age, Thiel&#39;s particular synthesis is distinctly contemporary. He melds this traditionalist concern with hyper-modern, techno-utopian aspirations, all filtered through a heterodox interpretation of Christian eschatology. This fusion, which is characterized by a desire to interpret ancient scripture as a means to engineer a radically different future, marks his ideology as distinct and divergent, even as its foundational fears and prescriptions echo older conservative discourses.&lt;/p&gt;
&lt;p&gt;This framework is built upon several key pillars, which the following sections will explore in detail:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Girard&#39;s Mimetic Theory&lt;/strong&gt;: Central to Thiel&#39;s thought is René Girard&#39;s concept of mimetic (imitative) desire as the root of human rivalry and violence. Thiel applies this to critique modern society, particularly what he sees as the mimetic-driven conflicts of contemporary progressive movements and the dangers of scapegoating. He views this understanding as crucial for navigating a world prone to &amp;quot;limitless violence.&amp;quot;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Christian Eschatology &amp;amp; the Antichrist&lt;/strong&gt;: Thiel&#39;s worldview is deeply imbued with Christian end-times narratives. He interprets concepts like the Antichrist not necessarily as a literal individual, but as a systemic threat, often a global, totalitarian entity promising false peace and safety. While he uses Christian doctrinal structures, his engagement often prioritizes their functional or civilizational utility over affirming literal metaphysical claims in the traditional sense. His &amp;quot;truth&amp;quot; of Christianity appears rooted in its perceived efficacy as a moral and ordering system for Western civilization.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Critique of the Enlightenment &amp;amp; Modernity&lt;/strong&gt;: Thiel expresses profound skepticism towards Enlightenment ideals of progress, rationalism, and democratic governance. He argues that modernity has &amp;quot;whitewashed&amp;quot; the problem of human violence and fostered a naive &amp;quot;indeterminate optimism.&amp;quot; This critique extends to concerns about globalization, mass surveillance, and the rise of China as a geopolitical rival.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Role of the &amp;quot;Katechon&amp;quot; and &amp;quot;Definite Optimism&amp;quot;&lt;/strong&gt;: In this often bleak landscape, Thiel sees a role for heroic agency, sometimes invoking the concept of the katechon (a restraining force against chaos). He champions &amp;quot;definite optimism,&amp;quot; the belief in a specific, engineered better future, driven by visionary individuals and technological breakthroughs, as an antidote to societal stagnation.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;At the core of Thiel&#39;s ideology a &lt;a href=&quot;https://en.wikipedia.org/wiki/Manichaeism&quot;&gt;Manichean vision&lt;/a&gt;, where the world is a literal battleground between order and chaos, good and evil, with humanity teetering on the brink of self-destruction due to the insidious force of mimetic desire, secular stagnation, and authoritarianism. Thiel perceives himself as an active agent, a harbinger of the future tasked with guiding civilization away from the abyss and towards a more meaningful existence. This sense of urgency fuels his attempts to build a mental model to understand and ultimately shape the course of history.&lt;/p&gt;
&lt;p&gt;Central to understanding Thiel&#39;s worldview is his profound rejection of Enlightenment liberalism, which stems from what he sees as its fundamentally anti-heroic character. His deepest objection to liberalism as a way of life is its deliberately nonheroic or anti-heroic attitude to life, its banalization of the problem of human existence. In Thiel&#39;s view, the scientific rationalism and universalism that define Enlightenment doctrines, including socialism, egalitarianism, and democratic governance, are inherently nihilistic and relativistic forces that lead to what he perceives as the disintegration of culture through decadent leveling. He rejects the abstract universalism that treats all individuals as fundamentally equal, instead insisting on anti-egalitarian principles that call for &amp;quot;heroic&amp;quot; or &amp;quot;higher&amp;quot; values capable of elevating &amp;quot;superior persons&amp;quot; above the masses. This philosophical stance reflects his belief that primary obligations are owed to one&#39;s own nation, culture, and what he might characterize as civilizational hierarchy, rather than extending equal treatment to all humanity. For Thiel, liberalism&#39;s commitment to equality and universal human dignity represents not moral progress but a dangerous flattening of human potential that undermines the exceptional individuals and cultures necessary to drive civilization forward. This rejection of liberal egalitarianism forms the intellectual foundation for his broader critique of modernity and his search for alternative frameworks, whether through Christian eschatology, technological acceleration, or political restructuring, that can restore what he sees as necessary hierarchy and meaning to human existence.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s worldview is deeply rooted in Christian eschatology (theology concerned with the end times and final destiny of humankind), specifically its emphasis on a linear view of time culminating in a final end times. He interprets the Book of Revelation not merely as a symbolic text but as a guide for navigating the challenges of modernity. The specter of the Antichrist looms large in Thiel&#39;s thinking; he views it as both a real and present danger, embodied not by a single figure but by a system, a one-world totalitarian government that gains power through deception, promising peace and safety while ultimately suppressing freedom. This vision of the Antichrist also serves as a proxy for what Thiel sees as his political antithesis: liberalism run amok into totalitarian control. His literal interpretation of the core concepts of Christian theology, such as the fallen nature of man and original sin, frames his understanding of human nature, but he takes a more metaphorical view of the specifics of prophecy.&lt;/p&gt;
&lt;p&gt;Notably, Thiel appears more interested in Christianity&#39;s institutional and doctrinal structures than metaphysical questions about God&#39;s existence or nature. He self-describes as &amp;quot;religious rather than spiritual&amp;quot;. Thiel often discusses Christianity&#39;s importance but strategically hedges on affirming the literal, metaphysical existence of the Christian God in the way many devout believers do. Instead, he tends to frame &amp;quot;Christianity as true,&amp;quot; where the word &amp;quot;true&amp;quot; carries a specific, functional weight rather than indicating belief in a supernatural being who intervenes in the world. For Thiel, Christianity&#39;s &amp;quot;truth&amp;quot; appears rooted in its perceived efficacy as a foundational moral system and a crucial unifying narrative for Western civilization, providing structure, meaning, and potentially a bulwark against nihilism or societal decay (often from, in his mind, non-Western influences), rather than asserting a philosophical commitment to the objective reality of a supernatural being and the literal miracles described in the Bible.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s engagement with Christian cosmology operates on multiple levels, embracing both literal and metaphorical interpretations that he sees as pointing to deeper truths about human civilization. While he takes core concepts like original sin and the fallen nature of man quite literally, he views the specifics of prophecy more metaphorically, not as precise predictions, but as archetypal patterns that reveal fundamental dynamics of human society. For Thiel, Christianity&#39;s linear view of time, culminating in either catastrophe or redemption, captures an essential truth about civilization&#39;s trajectory. He sees the biblical narrative of creation, fall, and potential redemption as mapping onto real historical processes, where humanity&#39;s mimetic nature (original sin) leads to cycles of violence and scapegoating, but also contains the possibility of transcendence through Christ&#39;s example of non-violence. Thus, for Thiel, Christian cosmology is &amp;quot;true&amp;quot; in a deeper sense than mere historical accuracy. One might say he holds Christian cosmology to be presuppositional or suprarational. It provides him with a framework for understanding the fundamental arc of human civilization and the choices we face as we move toward either destruction or redemption, chaos or order, not unlike the cosmic forces of the Lord of the Rings.&lt;/p&gt;
&lt;p&gt;In light of this heterodox interpretation of Christianity, Thiel does not view the future as predetermined, but rather that it is contingent on human action. He believes that humans are capable of rational and moral behavior, and that their actions matter in the course of history. He believes that the world we live in contains a &amp;quot;powerfully apocalyptic dimension&amp;quot;, where mimetic desire has the potential to lead to a catastrophe, while also seeing the possibility of a better future. Much of his philosophy seems to revolve around the idea of shaping the future so that particular apocalypses he fears can be avoided.&lt;/p&gt;
&lt;p&gt;In Thiel&#39;s mind the Enlightenment believes in the &amp;quot;wisdom of crowds&amp;quot;, the idea that large groups of people will naturally arrive at better decisions than individuals. This stands in stark contrast to the Biblical view which emphasizes the &amp;quot;madness of crowds&amp;quot;, how mobs can be driven to violence and irrationality, as seen in events like the crucifixion of Jesus. This tension between these two views of collective human behavior deeply informs Thiel&#39;s skepticism of democracy and modern liberal institutions. He sees the Enlightenment&#39;s faith in collective wisdom as dangerously naive, ignoring humanity&#39;s tendency toward mimetic violence and scapegoating that the Bible allegedly recognizes. For Thiel, this represents a fundamental flaw in Enlightenment thinking that threatens to unleash destructive social forces if left unchecked.&lt;/p&gt;
&lt;p&gt;However, Thiel is also deeply critical of the secular narratives that dominate modern thought. He sees the Enlightenment as having &amp;quot;whitewashed away&amp;quot; the fundamental issue of violence and is skeptical of the modern idea of progress. This skepticism extends to modern technology, which he sees as having the potential for both great good and great evil. He is wary of technology being used for mass surveillance and control, particularly by a centralized authority. He sees globalization as having a similar effect, and he is critical of the concept of a one-world government, which he believes would tend towards totalitarianism and the suppression of dissent.&lt;/p&gt;
&lt;p&gt;Allegedly, Thiel finds hope in the non-violent message of Jesus Christ, which he views as a solution to the cycle of mimetic violence. For Thiel, the Christian perspective unveils the truth of the human condition, specifically exposing the &amp;quot;hidden victims&amp;quot; at the heart of the social order. He sees Christ&#39;s sacrifice (which he hopes is the &amp;quot;last sacrifice&amp;quot;) as a way to break this cycle, and that the role model of Jesus Christ is the &lt;em&gt;only&lt;/em&gt; role model that doesn&#39;t lead to runaway mimesis and violence. He sees the need for a critical and discerning approach, which can include using violence, where &amp;quot;limited and sacred violence&amp;quot; is required to protect against &amp;quot;unlimited and desacralized violence&amp;quot;.&lt;/p&gt;
&lt;p&gt;Ultimately, Thiel&#39;s worldview is driven by a deep-seated need for order and a rejection of the chaos and violence he sees as defining the modern world. He believes in the power of human agency to shape the future and seeks to guide civilization away from disaster, not just as an observer, but as an active participant. Thiel essentially views himself as a philosopher king seeking to shape the trajectory of human civilization, convinced that it is headed towards either a catastrophic end, or a new beginning. He sees our actions in the present can either lead to catastrophe, or a more just and peaceful future, and believes that history is moving towards a final end he labels the &amp;quot;City of God&amp;quot;.&lt;/p&gt;
&lt;p&gt;In essence, these core ideas coalesce into a worldview that champions hierarchy and tradition while expressing deep resentment towards modernity, equality, and the perceived decadence of liberal-democratic societies. Thiel seeks certainty and a sense of aristocratic self-worth through appeals to myth, His philosophy represents a fundamental challenge to Enlightenment values, positioning itself as a counter-narrative that prioritizes order over freedom, hierarchy over equality, and mythic truth over empirical investigation.&lt;/p&gt;
&lt;p&gt;It&#39;s important to note that the mode of thinking prevalent in radical right-wing intellectual circles operates fundamentally differently from the analytic philosophy tradition. In the analytic tradition truth is often treated as something to be discovered through rigorous debate, empirical investigation, and fallibilistic reasoning. This approach acknowledges that our beliefs might be wrong and should remain open to revision based on new evidence or better arguments. In contrast, the radical right-wing intellectual tradition that influences Thiel is often indifferent to, or even hostile to, truth understood in this fallibilistic sense. Rather than viewing truth as something to be debated or gradually acquired through rational discourse, this tradition arrives at truth through decisive proclamations that establish foundational beliefs not because they can be rationally proven, but because they are deemed &amp;quot;edifying&amp;quot; and appeal to tradition, power, providence, instincts or intuitions about reality. This suggests a circular and self-serving concept of truth, where what is &amp;quot;true&amp;quot; is what reinforces their desired worldview and fulfills their desire for certainty and aristocratic elevation. Mussolini explicitly shared this worldview stating &amp;quot;We have created our myth. The myth is a faith, it is passion. It is not necessary that it shall be a reality. It is a reality by the fact that it is a good, a hope, a faith, that it is courage. [..] And to this myth, to this grandeur, that we wish to translate into a complete reality, we subordinate all the rest.&amp;quot;&lt;/p&gt;
&lt;p&gt;Thus this epistemology blurs the line between truth and power, leading to accepting truths or myths (the distinction between which is blurry) that are primarily instrumental and functional rather than falsifiable. These are beliefs that work to organize society, provide meaning, or serve particular civilizational purposes, regardless of whether they can be established through traditional scientific or rational methods. For thinkers in this tradition, the question is not &amp;quot;Is this a justified true belief?&amp;quot; but rather &amp;quot;Does this belief serve the higher purposes of order, meaning, and human flourishing?&amp;quot; This represents a profoundly different mode of doing philosophy than what I am used to, one that prioritizes the functional utility of ideas over their correspondence to empirical reality or their ability to withstand rigorous logical scrutiny. The incommensurability between these fundamentally different conceptions of the world and objective truth may make this entire project at minimum challenging and possibly entirely pointless. It&#39;s like trying to translate poetry into mathematics; the very act of translation destroys the internal structure of the original work.&lt;/p&gt;
&lt;p&gt;The critique of this approach is that it subordinates reason to mythic structures. While they still may employ logic and evidence, these tools are placed in service of deeper mythological frameworks that are judged not by their falsifiability but by their capacity to provide civilizational coherence and meaning. The system aims less at understanding objective reality and more at imposing an orienting worldview that can bind communities together. Such memetic systems tend to survive and proliferate precisely because they successfully marry rational argumentation with mythic narratives that speak to fundamental human needs for order, meaning, and belonging, while remaining relatively insulated from external critique through their emphasis on functional rather than empirical truth claims.&lt;/p&gt;
&lt;p&gt;Despite these challenges, in the sections that follow I&#39;ll deconstruct each tenant of this philosophy and examine how they allegedly synthesize into something like an overarching philosophical worldview before discussing its internal contradictions. It&#39;s worth noting that this attempt at steelmanning Thiel&#39;s worldview is inherently challenging, as his beliefs are not fully articulated, frequently ambiguous, and often contradictory. His tendency to use metaphorical and literal terms interchangeably further complicates any effort to present a coherent account of his thinking. This interchangeability represents a form of referential opacity. When metaphorical terms are substituted with supposedly co-referential literal ones (or vice versa), the result is often a shift in truth value and a distortion of the intended meaning. Nevertheless, this represents a best effort attempt to engage with the ideas on the merits before offering criticism.&lt;/p&gt;
&lt;h2 id=&quot;ren%C3%A9-girard&quot; tabindex=&quot;-1&quot;&gt;René Girard&lt;/h2&gt;
&lt;p&gt;Thiel&#39;s interpretation of René Girard&#39;s mimetic theory forms a central pillar of his philosophical worldview, providing a framework for understanding human behavior, societal structures, and the potential trajectory of civilization. Girard, who died in 2015, was a French literary critic and cultural theorist who taught at American universities, publishing more than 20 books on topics ranging from 19th-century novels to Indian scripture. Notably, Thiel studied under Girard while at Stanford University, forming an intellectual connection that would profoundly shape his thinking. It&#39;s worth noting that Girard was not a philosopher in the traditional sense; he approached human culture and history more as literary texts to be interpreted, rather than developing systematic philosophical arguments or conducting scientific research. Despite never being a household name, his work has experienced a surprising resurgence among a particular Venn diagram of Catholics, entrepreneurs, and political figures, with Thiel being perhaps his most influential disciple in Silicon Valley.&lt;/p&gt;
&lt;p&gt;At the core of Girard&#39;s theory, as embraced by Thiel, is the concept that human desire is not an autonomous force but rather a product of imitation. This idea, known as &amp;quot;mimetic desire,&amp;quot; was first set out in Girard&#39;s 1961 book &lt;em&gt;Deceit, Desire and the Novel&lt;/em&gt;, where he analyzed how literary characters from Don Quixote to Dostoyevsky&#39;s protagonists come to desire things simply because others want them. As Girard wrote, &amp;quot;Man is the creature who does not know what to desire, and he turns to others in order to make up his mind.&amp;quot; This borrowed nature of desire inevitably leads to competition; if you desire your neighbor&#39;s wife, you must contend with your neighbor to get what you want, or what you think you want. The result is a complex web of relationships defined by envy, rivalry, and ultimately, violence. Girard envisioned this mimetic rivalry escalating to runaway violence, both interpersonal and international, driven by warring doubles like Fascism vs. Communism or the United States vs. the Soviet Union. This concept forms the foundation upon which Thiel builds his analysis of everything from social media to political movements and the potential for both human destruction and redemption.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s understanding of mimetic desire emphasizes its triangular nature, where desire flows not directly from the subject to an object, but through a &amp;quot;mediator&amp;quot; or model. This means that individuals often want something simply because someone else desires it, regardless of the inherent value of the object itself. The relationship between the subject and the mediator becomes more significant than the object being pursued, often leading to competition and conflict. This dynamic is not limited to individuals, but extends to group dynamics where entire communities can become embroiled in mimetic conflicts as they seek to emulate or outdo each other. Thiel sees this constant pursuit and imitation as a significant driver of human history, a process where human desires often become detached from any intrinsic value, becoming instead a reflection of the desires of the &amp;quot;other&amp;quot;.&lt;/p&gt;
&lt;p&gt;This understanding of desire naturally leads to Girard&#39;s concept of the scapegoat mechanism, which Thiel sees as a recurring pattern in human societies. When mimetic rivalry reaches a critical point, leading to a buildup of tension and conflict, societies seek to restore order by channeling the violence onto a single individual or group. The scapegoat, often innocent, becomes the receptacle of collective blame and is sacrificed to appease the community and release pent-up tensions. As Girard elaborated in his 1972 work &lt;em&gt;Violence and the Sacred,&lt;/em&gt; human societies enter into periods of crisis in which competition becomes unbearable, and the solution is a violent act of scapegoating. Girard argued that mimetic violence and the scapegoating of victims are the very &amp;quot;things hidden since the foundation of the world.&amp;quot; For scapegoating to be effective, the persecutors must, on some level, be unaware that they are performing a psychosocial act, viewing it instead as a religious epiphany that unites them. Indeed, a central idea for Girard is that all powers and principalities depend on scapegoating violence for their existence. The scapegoat typically has certain recurrent features: they are often a foreigner, someone with a disability, or a person in a position of authority. These acts of violence are then commemorated in the founding myths of cultures, myths in which the scapegoat becomes deified.&lt;/p&gt;
&lt;p&gt;Girard&#39;s development of the scapegoat theory was influenced by his observations of post-World War II France, where collaborators were publicly punished as scapegoats for the Nazi invasion. Though Girard rarely used contemporary case studies, preferring to find evidence in ancient literature, scripture, and anthropology, his view on lynchings both ancient and modern was unambiguous: they were unconscionable.&lt;/p&gt;
&lt;p&gt;The scapegoat mechanism also informed Girard&#39;s response to Nietzsche, who condemned Christianity as a philosophy of victimhood. In this intellectual confrontation, Nietzsche criticized Christianity&#39;s &amp;quot;slave morality&amp;quot; for its defense of the weak, interpreting its emphasis on mercy as a corruption of human vitality that undermined the pre-Christian values of power and strength. Girard, while fundamentally opposed to Nietzsche&#39;s antipathy toward victims, paradoxically thought he was a philosophical genius who recognized what many failed to perceive: Christianity&#39;s singular focus on defending victims. Their agreement on Christianity&#39;s uniqueness, but their diametrically opposed conclusions are telling. Nietzsche saw it as humanity&#39;s corruption, while Girard viewed it as the revelation of truth that could reverse culture&#39;s violent foundations. This contrast exemplifies the tension at the heart of Thiel&#39;s understanding of victimhood. Thiel believes that the scapegoat mechanism is not an aberration but a central feature of human societies, reflecting the ever-present potential for mimetic violence. He also notes that this violence is often obscured or hidden, as societies develop elaborate justifications for the scapegoating, and individuals are often unaware of their own participation in these dynamics. He sees the &amp;quot;city of man&amp;quot; as being built upon hidden victims.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s interpretation of Girard is profoundly influenced by his Christian perspective, particularly by the Judeo-Christian revelation, where the story of Jesus Christ exposes the injustice of the scapegoat mechanism. In Girard&#39;s 1978 book &lt;em&gt;Things Hidden Since the Foundation of the World&lt;/em&gt;, he argues that Christianity had revealed the hidden truth of the scapegoat mechanism. By insisting on their saviour&#39;s innocence, Christians had deconstructed the &amp;quot;primitive&amp;quot; belief in the scapegoat&#39;s guilt.&lt;/p&gt;
&lt;p&gt;Thiel sees the Christian tradition as uniquely offering a lens to view the world from the perspective of the innocent victim, unlike pagan mythologies, which typically view victims as inherently guilty. In Thiel&#39;s reading of Girard, Christianity fundamentally exposes the scapegoat mechanism by presenting Christ as fully innocent while his persecutors are complicit in the literal death of God. This revelation forces the victimizers to confront their own culpability rather than projecting blame onto an outsider. The crucifixion thus unveils the truth about scapegoating by revealing it as a profound moral failure rather than a justified sacrifice.&lt;/p&gt;
&lt;p&gt;He posits that beyond merely exposing the mechanism, Christianity offers a path beyond the cycle of violence through Christ&#39;s teachings. When Jesus instructs his followers to &amp;quot;turn the other cheek,&amp;quot; he introduces a radical alternative to mimetic rivalry. Instead of responding to violence with retaliatory violence (which perpetuates mimesis), Christ&#39;s model substitutes forgiveness and non-violence. This creates a new mimetic pattern where peace, rather than vengeance, becomes the desire to be imitated. For Girard, this represents the only viable escape from the endless cycle of mimetic violence.&lt;/p&gt;
&lt;p&gt;While Girard is considered a pacifist in his interpretation of these principles, Thiel does not necessarily share this strict interpretation. He believes that force is sometimes required to prevent violence from escalating further. Nevertheless, he maintains that forgiveness remains a crucial aspect of overcoming mimetic violence and breaking the cycle of retaliation that drives human conflict.&lt;/p&gt;
&lt;p&gt;Thiel is a pessimist about human nature, influenced by his Christian beliefs concerning original sin. He sees humans as inherently prone to violence and envy, requiring strong frameworks (often religious) to contain their darker impulses. This contrasts sharply with Enlightenment and liberal views that emphasize human goodness and the potential for progress through reason and democracy. When Thiel says &amp;quot;the state contains violence,&amp;quot; he is asserting his deep-seated beliefs about the nature of political power and social order. Thiel sees the state as serving a single function: it acts as a mechanism to channel, control, and limit the inherent violence within human society. This violence, which Girard identifies as stemming from mimetic desire and rivalry, must be contained and directed to prevent society from descending into chaos.&lt;/p&gt;
&lt;p&gt;Many on the right-wing spectrum like Thiel allege that the state achieves this containment through what Max Weber called the &amp;quot;monopoly on violence,&amp;quot; the exclusive right to use physical force within a given territory. By establishing laws, enforcing order, and wielding authority, the state claims sole legitimacy over coercive power. This monopoly on violence allows the state to suppress private feuds and vigilantism while channeling conflict resolution through its courts and police. The state&#39;s institutions become the only recognized arbiter of force, theoretically preventing the chaos of everyone wielding violence independently.&lt;/p&gt;
&lt;p&gt;However, Thiel&#39;s relationship with state power is deeply ambivalent, shaped by his fear of becoming a target of the state&#39;s scapegoating mechanism. Drawing from Girard&#39;s insights about sacrificial violence, Thiel sees successful founders and innovators as potential victims of societal envy and state persecution. This personal anxiety about becoming a scapegoat has driven him toward anti-democratic positions that advocate for limiting state power. His support for minimal government isn&#39;t just philosophical; it&#39;s a psychological response to his understanding of Girardian dynamics, where he sees state institutions as potential instruments of mimetic violence that could be turned against individuals like himself.&lt;/p&gt;
&lt;p&gt;However, while Thiel views mimetic desire as a powerful force driving human history, he also believes that it is not an insurmountable barrier. He believes that certain individuals, whom he terms &amp;quot;sovereign individuals&amp;quot; or &amp;quot;founders&amp;quot;, can escape the cycle of imitation and create new realities. These individuals must be protected from the scapegoating tendencies of the state and the mob and are crucial for innovation and progress. His emphasis on human agency, linear time, the Kingdom of Heaven, and a future where all injustices will be revealed, further demonstrates how intimately his Girardian interpretations are tied to his Christian theology. He sees the Christian view as not only a way to understand human behavior, but also a way to overcome the cycle of violence.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s investment in Facebook/Instagram represents a shrewd capitalization on Girardian mimetic theory in digital form. These platforms systematically transform mimetic desire into a monetizable business model by creating environments where users constantly observe others&#39; curated lives, possessions, and experiences, triggering waves of envy and imitative desire. Instagram&#39;s interface specifically optimizes for this dynamic: its visual emphasis on lifestyle, consumption, and status markers creates perfect conditions for mimetic contagion, where users desire what others display. This manufactured envy drives engagement metrics which Facebook converts directly into advertising revenue. The genius of the model lies in its self-reinforcing nature; the more users engage in mimetic comparison, the more attention they provide, which advertisers pay to capture. Thiel, understanding Girard&#39;s insights about human desire being fundamentally imitative, has effectively invested in a business that industrializes and monetizes the very psychological mechanism he intellectually critiques elsewhere.&lt;/p&gt;
&lt;p&gt;Notably Peter Thiel identifies with &lt;em&gt;unreconstructed Girardianism&lt;/em&gt;, a divergence from Girard&#39;s views held in later life. This later philosophy, sometimes called &lt;em&gt;reconstructed Girardianism&lt;/em&gt;, is a distinction entirely on the degree of adherence to René Girard&#39;s original, comprehensive theory. An unreconstructed Girardian maintains an unwavering commitment to what is called Girard&#39;s &amp;quot;triple audacity,&amp;quot; the claim that mimetic theory explains the origins of all culture, religion, and hominization through the universal scapegoat mechanism. This approach accepts the theory as a complete &amp;quot;grammar of sacred violence,&amp;quot; embracing its apocalyptic vision and the unique, transformative role of Judaeo-Christian revelation. In contrast, a reconstructed Girardian engages more critically, treating the framework as a powerful but modifiable analytical tool rather than a totalizing doctrine.&lt;/p&gt;
&lt;p&gt;If we assume good faith, Thiel&#39;s interpretation of René Girard is not simply an academic exercise but a lens through which he views and acts in the world. He understands Girard&#39;s ideas as providing a framework for understanding human nature, the roots of violence, and the potential for both destruction and redemption. He uses these concepts to critique modern politics and technology and to guide his own actions in business and politics. Thiel&#39;s approach to Girard is intimately tied to his Christian beliefs, his view of history, and his vision of a future where mimetic violence is overcome and humanity moves towards a more just and peaceful order.&lt;/p&gt;
&lt;h2 id=&quot;carl-schmitt&quot; tabindex=&quot;-1&quot;&gt;Carl Schmitt&lt;/h2&gt;
&lt;p&gt;Peter Thiel&#39;s political landscape is also significantly shaped by the ideas of Carl Schmitt, a German jurist and political theorist whose work offers a trenchant critique of liberalism. Beginning life as a conservative reactionary influenced by Joseph de Maistre, Schmitt joined the Nazis in the 1930s and published numerous antisemitic works like &lt;em&gt;Leviathan in the State Theory of Thomas Hobbes&lt;/em&gt;. He was later banished from the Nazi party for being a fellow traveler rather than a true believer, but never repented of his involvement. This deeply problematic association necessitates careful consideration when examining his influence, yet it is crucial to acknowledge that both Thiel and Schmitt share a profound skepticism towards core tenets of the Enlightenment, particularly its influence on liberal political theory and its perceived tendency to downplay the fundamental role of violence and the inescapable nature of political conflict.&lt;/p&gt;
&lt;p&gt;At the core of Schmitt&#39;s outlook is an agonistic view of politics as the site of existential and theological struggle. In &lt;em&gt;Political Theology&lt;/em&gt;, Schmitt insisted that all significant concepts of the modern theory of the state are secularized theological concepts, which attract believers and fundamentalists on their behalf. He argues for a systematic structural analogy between theological and political ideas: the omnipotent God in theology corresponds to the all-powerful sovereign lawgiver in jurisprudence. Thus, despite secularization, political structures and concepts of authority, particularly sovereignty, retain their theological DNA and cannot be fully comprehended without acknowledging this genealogy. The sovereign&#39;s ability to decide on the exception, for instance, mirrors God&#39;s capacity for miraculous intervention, suspending the normal order of nature or law.&lt;/p&gt;
&lt;p&gt;This theological dimension of politics naturally divides the political realm into friends who believe with us and enemies who do not. Schmitt&#39;s concept of &amp;quot;the political&amp;quot; (das Politische) is defined by this fundamental existential distinction between friend (Freund) and enemy (Feind), which resonates deeply with Thiel. For both, this distinction is not an archaic relic but an ineradicable element of political dynamics. Liberalism was distinctive in pretending to overcome this friend-enemy distinction, assuming that toleration and deliberation could settle political disputes instead of confrontation. Schmitt thought that was nonsense, and insisted liberals would too when push came to shove. Either they would banish threatening illiberal doctrines from their states, or be overcome by them and so cease to cherish liberal toleration.&lt;/p&gt;
&lt;p&gt;Thiel views the modern liberal attempt to transcend this reality as a dangerous oversight. For example, he cites the Reagan coalition, which was unified by a common enemy in Soviet communism, as a powerful illustration of Schmitt&#39;s point. This concept informs Thiel&#39;s perception of geopolitical rivalries, such as those with Islam or China, and demonstrates his application of the friend/enemy distinction to contemporary international relations. He considers this concept essential for understanding the actual workings of the world, echoing Schmitt&#39;s assertion that Enlightenment liberalism has &amp;quot;whitewashed away&amp;quot; the fundamental problem of human violence.&lt;/p&gt;
&lt;p&gt;Schmitt&#39;s analysis often focuses on &amp;quot;borderline concepts&amp;quot; (Grenzbegriffe), ideas that illuminate the nature of order by examining its limits. The most significant of these is his notion of the &amp;quot;state of exception&amp;quot; (Ausnahmezustand), a situation where normal legal frameworks are suspended and sovereign power manifests in its purest, most decisionistic form. For Schmitt, &amp;quot;sovereign is he who decides on the exception.&amp;quot; These borderline concepts reveal that political order ultimately rests on foundational decisions that may lie beyond complete rationalization or legal codification, exposing the existential underpinnings of authority.&lt;/p&gt;
&lt;p&gt;Schmitt held ambiguous views about democracy that can go some way to explaining the populist dimensions of fascism at a theoretical level, views that find resonance in Thiel&#39;s own democratic skepticism. Schmitt often insisted that genuine democracy might be in conflict with liberalism, since liberals cherished treating people equally as rights bearers whereas democrats accepted that members of the democratic community were entitled to agency while others were not. At times Schmitt mused that even a dictator could be a democratic figure if he genuinely implemented the will of the people. This tension between democratic legitimacy and liberal proceduralism influences Thiel&#39;s own concerns about democracy&#39;s potential to devolve into &amp;quot;mob rule&amp;quot; and undermine individual freedoms, though Thiel&#39;s critique stems from his Girardian analysis of mimetic desire and scapegoating rather than Schmitt&#39;s more cynical political framework.&lt;/p&gt;
&lt;p&gt;Both Thiel and Schmitt also share a rejection of utopian visions, particularly the liberal-progressive belief that society can be perfected through political or social reforms. Thiel views such utopianism as a form of &amp;quot;indeterminate optimism&amp;quot; that distracts from concrete problems and solutions. Similarly, Schmitt argued that attempts to eliminate the friend/enemy distinction and create a conflict-free, universal world order are futile and dangerously illusory, dismissing such projects as a &amp;quot;humanitarian-moral universalism&amp;quot; that often masks new antagonisms. This shared anti-utopianism is a core element of their critique of modern liberal thought.&lt;/p&gt;
&lt;p&gt;The role of violence is another point of convergence. Schmitt contended that a political community cannot achieve cohesion without an enemy against which to define itself, implying that conflict, and potentially violence, is an inherent aspect of political order. Thiel, profoundly influenced by Girard, sees mimetic desire as a fundamental driver of conflict and violence. However, a significant divergence emerges here: while Schmitt largely accepts the political necessity of the friend/enemy distinction and its potential for violence, Thiel, drawing from Christian theology, posits that the non-violence of Jesus offers a possible (though perhaps not always practically realized) alternative to the perpetual cycle of violence.&lt;/p&gt;
&lt;p&gt;Often presenting his views as a realist vision of the world, Schmitt&#39;s politics is in fact totalizing and often simplistic. He projects onto everyone else his own distinct need to banish competing theologies from the public square. This reveals that while Thiel may adopt a Schmittian diagnosis of political realities, his proposed remedies or ultimate hopes are shaped by a different theological horizon. Rather than following Schmitt&#39;s conclusions, Thiel sees the Christian view as not only a way to understand human behavior, but also a way to overcome the cycle of violence that Schmitt considers inescapable.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s engagement with Schmitt is not uncritical, and from a Girardian perspective, Thiel sees Schmitt as too narrowly focused on the political realm and too invested in preserving what Thiel considers the ultimately nihilistic friend-enemy distinction. While both thinkers express concerns about liberal democracy&#39;s limitations, Thiel&#39;s framework, ultimately informed by Christian theology and Girardian ideas, seeks to navigate beyond what he sees as the limitations of Schmitt&#39;s more purely political analysis.&lt;/p&gt;
&lt;h2 id=&quot;oswald-spengler&quot; tabindex=&quot;-1&quot;&gt;Oswald Spengler&lt;/h2&gt;
&lt;p&gt;Peter Thiel&#39;s framework draws significantly from Oswald Spengler, the German autodidact whose work &lt;em&gt;The Decline of the West&lt;/em&gt; captured and systematized many strands of right-wing thought in the early 20th century. Spengler&#39;s melancholic tale of cultural decline, deeply influenced by Goethe and Nietzsche, resonated with post-war German conservative revolutionaries and continues to shape Thiel&#39;s understanding of Western civilization&#39;s trajectory. For Spengler, history followed biological patterns, with cultures functioning as living organisms that experience birth, growth, maturity, and inevitable death. He viewed Western civilization as a unique and almost biological entity, distinct from other cultures yet subject to the same organic laws of development and decay.&lt;/p&gt;
&lt;p&gt;Central to Spengler&#39;s thesis was his belief that Western civilization&#39;s spiritual roots had begun to decay through the ascendance of Enlightenment rationalism. He worried that the Faustian yearning for knowledge, power, and transcendence that had generated enormous dynamism in Western culture was giving way to what he saw as the hedonistic pleasure-seeking of socialism and liberalism. This spiritual decline, he argued, marked Western civilization&#39;s entry into a period of terminal decline. Spengler&#39;s pessimistic vision extended to fears of a &amp;quot;colored world revolution&amp;quot; that would permanently end Western dominance, though he also mused that a Caesar and national socialism might present themselves as potential solutions to this civilizational crisis.&lt;/p&gt;
&lt;p&gt;Like Spengler, Thiel views modern Western society as facing secular decline, though he connects this decline specifically to technological stagnation. Where Spengler focused on the decay of spiritual roots leading to diminished cultural energy, Thiel diagnoses the problem as a failure to maintain the robust technological progress that characterized earlier periods of Western development. This technological pessimism reinforces his broader critique of modernity and his belief that current liberal democratic institutions are inadequate to address civilizational challenges.&lt;/p&gt;
&lt;p&gt;Both thinkers share a fundamentally Manichean worldview that sees history as a battleground between opposing forces. Spengler&#39;s tragic tone and sweeping totalizations offered succor to those grappling with the gloomy realities of early 20th-century decline, while Thiel&#39;s vision presents the world as a literal battleground between order and chaos, good and evil. This apocalyptic framework justifies their shared skepticism toward democratic processes and their preference for strong, decisive leadership capable of imposing order on civilizational chaos.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s adoption of Spengler&#39;s fatalistic view of Western decline and the inevitable rise of Caesarism forms a critical philosophical foundation for his anti-democratic sentiments. By grounding his political preferences within Spengler&#39;s broader narrative of civilizational crisis, Thiel positions authoritarian solutions not as personal preferences but as historical necessities. This Spenglerian influence helps explain Thiel&#39;s comfort with supporting strongman leaders and his belief that traditional democratic constraints may need to be suspended to address what he perceives as existential threats to Western civilization.&lt;/p&gt;
&lt;p&gt;While Spengler&#39;s historical analysis was highly speculative and often ignored counter-examples, his tragic vision of inevitable civilizational decay provides intellectual cover for those seeking to justify authoritarian alternatives to liberal democracy. For Thiel, Spengler&#39;s work offers both a diagnosis of Western decline and a roadmap toward the kind of strong leadership he believes necessary to navigate civilizational crisis.&lt;/p&gt;
&lt;h2 id=&quot;leo-strauss&quot; tabindex=&quot;-1&quot;&gt;Leo Strauss&lt;/h2&gt;
&lt;p&gt;Peter Thiel&#39;s intellectual framework is also significantly informed by his interpretation of Leo Strauss, a political philosopher renowned for his critiques of modernity and his distinctive approach to reading classical texts. Thiel&#39;s engagement with Strauss is most notably articulated in his 2004 essay, &lt;a href=&quot;https://gwern.net/doc/politics/2007-thiel.pdf&quot;&gt;&lt;em&gt;The Straussian Moment&lt;/em&gt;&lt;/a&gt;, a post-9/11 political call-to-arms. In it, Thiel argues that Enlightenment liberal ideals are insufficient against ideologically driven adversaries, positing that liberalism&#39;s tolerance and belief in peaceful negotiation fail when faced with actors motivated by religious or existential goals. This failure, he contends, necessitates a &amp;quot;political framework that operates outside the checks and balances of representative democracy,&amp;quot; a conclusion he advances with a subtlety that itself reflects a Straussian methodology.&lt;/p&gt;
&lt;p&gt;For Thiel, Strauss represents a thinker who, like René Girard, recognized the persistent problem of violence and the limitations of modern political thought. At the core of Strauss&#39;s own methodology, and central to Thiel&#39;s appropriation, is the concept of esoteric writing. Strauss argued that many pre-modern philosophers wrote on two levels: an exoteric (surface) teaching for a wider, potentially hostile audience, and an esoteric (hidden) teaching intended for a select few capable of discerning deeper, often more radical, truths. Thiel doesn&#39;t just admire this method; he actively employs it, and &lt;em&gt;The Straussian Moment&lt;/em&gt; is itself an example of his application. While the essay appears to end on a moderate, even anticlimactic note advising leaders to &amp;quot;side with peace,&amp;quot; its true, dramatic conclusion is hidden earlier in the text.&lt;/p&gt;
&lt;p&gt;In a classic esoteric maneuver, Thiel signals his real thesis by quoting, in untranslated German and Latin, the final sentences of Oswald Spengler&#39;s &lt;em&gt;The Decline of the West&lt;/em&gt;. This hidden finale presents a fatalistic, anti-democratic vision: an age dominated by money will inevitably give way to &amp;quot;Caesarism,&amp;quot; which is rule by a strong, authoritarian leader. For those living in such a time, the only choice is to accept this fate (&amp;quot;do what is necessary&amp;quot;) or do nothing, for as the quote concludes, &amp;quot;Fate leads the willing, and drags the unwilling.&amp;quot; By burying this radical endorsement of authoritarianism in untranslated text, Thiel masks his ambitions from the general reader while signaling them to an initiated elite. He implicitly includes himself within this group, which he sees as capable of shaping the future by uncovering such concealed &amp;quot;secrets.&amp;quot;&lt;/p&gt;
&lt;p&gt;This embrace of a hidden, radical solution is rooted in Thiel&#39;s alignment with Strauss&#39;s critique of modernity, particularly its secularism and optimistic belief in rational progress. Strauss contended that modern political philosophy, by abandoning classical inquiries into virtue, had led to a &amp;quot;crisis of the West&amp;quot; characterized by nihilism. In &amp;quot;The Straussian Moment,&amp;quot; Thiel echoes this, arguing that the Enlightenment&#39;s faith in reason proved insufficient to grasp the irrational, violent forces unleashed on 9/11. For Thiel, this &amp;quot;Straussian moment&amp;quot; signifies a juncture where the West must confront the limits of its inheritance and rediscover more fundamental, pre-modern understandings of politics.&lt;/p&gt;
&lt;p&gt;This critique naturally leads to an emphasis on the enduring problem of human nature. Thiel draws a line connecting Strauss&#39;s concern with human imperfection to the insights of Girard on mimetic violence and Schmitt on the friend/enemy distinction, viewing all three as thinkers who acknowledge the ineradicable role of conflict. He believes modern thought has dangerously &amp;quot;whitewashed&amp;quot; this reality. Furthermore, Thiel interprets Strauss as recognizing the inescapable relationship between religion and political order. He translates this into a conviction that a political order cannot be neutral on ultimate questions, suggesting in his essay that only a &amp;quot;Christian statesman&amp;quot; might ultimately restrain the cycle of violence, subtly positioning his Girardian Christianity as a necessary supplement to a purely classical revival.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s skepticism towards liberal democracy also finds resonance in Strauss. While Strauss&#39;s own views were nuanced, his critique of mass society can be read as aligning with Thiel&#39;s fears of democracy devolving into mob rule. This elitist preference for governance is not merely theoretical; it is the explicit, if concealed, conclusion of his &amp;quot;Straussian Moment&amp;quot; essay, where he aligns himself with Spengler’s fatalistic prediction of an inevitable &amp;quot;Caesarism&amp;quot; to replace faltering democratic systems.&lt;/p&gt;
&lt;p&gt;It is crucial to note that Thiel&#39;s engagement is highly selective; he is not a &amp;quot;Straussian&amp;quot; in a conventional sense but rather mines Strauss&#39;s thought for concepts that bolster his worldview. He weaponizes the esoteric method not just to pursue &amp;quot;secrets,&amp;quot; but to conceal his most radical, Spenglerian commitments. He leverages the critique of modernity to justify his anti-Enlightenment stances and the focus on human imperfection to underscore his diagnoses of conflict.&lt;/p&gt;
&lt;p&gt;In essence, Thiel uses Strauss as both inspiration and tactical cover. For him, the &amp;quot;Straussian moment&amp;quot; is less about a wholesale adoption of classical philosophy and more about using its methods to create space for his own preferred blend of technological ambition, apocalyptic Christianity, and elitist governance. The essay&#39;s hidden finale reveals the ultimate purpose: to frame an authoritarian turn as a necessary, even fated, response to a modernity that he believes has lost its way.&lt;/p&gt;
&lt;h2 id=&quot;lord-of-the-rings&quot; tabindex=&quot;-1&quot;&gt;Lord of the Rings&lt;/h2&gt;
&lt;p&gt;For Thiel, J.R.R. Tolkien&#39;s The Lord of the Rings serves as more than literary entertainment; it functions as a foundational mythology that shapes his Manichean worldview. This resonates with how, for some on the right, Tolkien’s work embodies nostalgia for a bygone era, conjuring a vaguely medieval past with clear hierarchies of authority and class, and sharply delineated races (elves, dwarves, hobbits, and orcs) with distinctive appearances and talents. Through Tolkien&#39;s epic, Thiel finds validation for his belief in the necessity of a rightful king&#39;s return to restore cosmic order, a theme central to Traditionalism which posits that toppling a corrupt modern status quo can lead to a golden age, much like Aragorn’s ascension. He interprets the narrative as a framework for understanding history, power dynamics, and the eternal struggle between good and evil, viewing our world as a modern Middle Earth where he has been called to &amp;quot;Save The Shire&amp;quot;—his metaphor for an idealized &amp;quot;Christendom&amp;quot; or &amp;quot;Western civilization.&amp;quot; While Tolkien&#39;s cosmology positions a fictional spiritual pantheon of Valar as guiding supernatural forces, Thiel reframes these cosmic influences through a Girardian lens, seeing them as manifestations of the Judeo-Christian Spirit working purposefully through human history. This interpretation allows him to position himself within a grand narrative where he plays a pivotal role in the battle between order and chaos.&lt;/p&gt;
&lt;p&gt;At the heart of Thiel&#39;s interpretation lies his fundamentally Manichean view of the world, a constant battleground between good and evil, where authoritarianism battles against freedom, and nihilism against purpose. This worldview is projected onto the cosmic forces of Middle Earth, aligning perfectly with the central conflict in The Lord of the Rings, where the free peoples of Middle-earth represent individual liberty and meaningful existence against Sauron&#39;s totalitarian control and spiritual emptiness. This clear demarcation of good versus evil and the inherent order within Middle-earth&#39;s societies and races is a key aspect of its appeal to a right-wing sensibility that values distinct categories and moral clarity. Thiel sees our world as locked in this same cosmic struggle, casting himself as a defender of freedom against encroaching authoritarianism, a sentiment echoed in the broader use of fantasy and science fiction to frame contemporary issues with a Manichaean vocabulary where the future of civilization is constantly at stake. For him, The Lord of the Rings is not merely fiction but a mythological framework that reveals deeper truths about reality. In Thiel&#39;s view, good and evil are not abstract concepts but tangible forces manifested in political and social structures that either liberate or subjugate humanity.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s contrarian nature is also reflected in his interpretation of Tolkien&#39;s work. For example, he views Mordor, the primary source of evil in the story, as being a technological civilization based on reason and science that attempts to unify Middle Earth under a totalitarian one world government. He sees this as being in contrast to the forces of good, The Fellowship of the free people of Middle Earth, which are based more on nature and mysticism and rebellion against the tyranny of totalitarianism. This view aligns with certain right-wing critiques of modernity and globalization, seeing them as homogenizing forces.&lt;/p&gt;
&lt;p&gt;Thiel is also drawn to the theme of the Return of the King, a central narrative element in The Lord of the Rings, where Aragorn&#39;s return to the throne of Gondor is a restoration of order and justice. This concept resonates deeply with Thiel&#39;s own belief that a strong, decisive leader is needed to counter the chaos he sees in the modern world, and it directly mirrors the Traditionalist hope for a new golden age ushered in by a rightful authority. This taste for kingly power is also evident in Silicon Valley&#39;s &amp;quot;founder mode,&amp;quot; where unilateral decision-making by a singular leader is prized. He sees the &amp;quot;founder&amp;quot; as being similar to a quasi-monarch, who can be both &amp;quot;God&amp;quot; and a &amp;quot;victim,&amp;quot; echoing the character arc of Aragorn and aligning with Thiel&#39;s depiction of the tech entrepreneur as a conquering hero on a &amp;quot;Hero&#39;s Journey.&amp;quot; He sees that a return of a strong, just leader is necessary to overcome the forces of evil. Thiel views his own role as being one that is similar to Aragorn&#39;s, where he can help to bring about the restoration of order and justice.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s Manichean outlook is further reinforced by the clear distinction between good and evil that is present in Tolkien&#39;s work, including the sharply delineated moral and racial characteristics of its inhabitants. He sees that Tolkien&#39;s story emphasizes that good and evil are not just abstract concepts, but real forces that shape the world, a perspective that appeals to those seeking moral certainty over ambiguity. This vision helps to solidify Thiel&#39;s understanding of the world, and it reinforces his view that he must take a side in this cosmic struggle. He believes that he is able to see the difference between good and evil, and that this unique ability puts him in a position to make a difference in the world.&lt;/p&gt;
&lt;p&gt;Thiel also named his data analytics company Palantir after the seeing stones from Lord of the Rings. These artifacts are neither inherently good nor evil, but they can be dangerous in the wrong hands. In Tolkien&#39;s mythology, the palantíri were powerful seeing stones that allowed users to witness events across vast distances and communicate with other stone holders, but they came with significant warnings. The myth cautioned that these stones could be incredibly dangerous when wielded by those lacking wisdom or moral compass, as they could distort truth and present selective visions of reality that served the interests of corrupting forces like Sauron. This naming choice deliberately reflects Thiel&#39;s view on surveillance technology: like the palantíri themselves, data analytics tools are morally neutral but can be corrupted when misused. The company embodies Thiel&#39;s philosophical outlook through its unusual corporate culture, featuring a flat title structure designed to minimize mimetic rivalry, and its mission of uncovering hidden &amp;quot;secrets&amp;quot; that can be leveraged for personal strategic advantage.&lt;/p&gt;
&lt;p&gt;Thiel sees The Lord of the Rings as being part of a literary canon that has shaped his thinking, much as it has for others in Silicon Valley and right-wing circles, from the early counter-cultural hackers identifying with hobbits to today&#39;s tech titans making extravagant tributes. He views these works as sharing certain themes and perspectives, such as the appeal of clear hierarchies, the struggle for a perceived rightful order, and the Manichean clash of good versus evil, which are formative to his worldview. He uses these stories as a way to explain the world around him, and he seems to believe that they can provide important lessons about human nature. He sees the stories as having hidden truths that can only be found through deep engagement with the text, ala Strauss. This deep engagement is shared by figures like Vice President JD Vance, who, mentored by Thiel, also credits Tolkien with influencing his conservative worldview and named his venture firm Narya Capital after Gandalf’s ring.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s alleges his favorite Tolkien quote is &amp;quot;The greatest adventure is what lies ahead / Today and tomorrow are yet to be said&amp;quot; which comes from the animated version of The Hobbit. He claims that he memorized this passage at a young age, and that it became a motto for his life. The Lord of the Rings is more than just a story for Thiel; it is a significant childhood influence that reinforces and shapes his view of the world as being in a state of conflict between cosmic forces, an exhortation to think big and pursue improbable dreams, much like the broader impact of fantasy and science fiction on its adherents. This reflects a wider trend in right wing thought where such narratives provide not only a vocabulary for understanding the world but also a call to action within it.&lt;/p&gt;
&lt;h2 id=&quot;the-katechon&quot; tabindex=&quot;-1&quot;&gt;The Katechon&lt;/h2&gt;
&lt;p&gt;Thiel&#39;s interpretation of the &lt;strong&gt;katechon&lt;/strong&gt; (Greek for &amp;quot;he or that which restrains&amp;quot;) is a concept that draws from his Christian eschatology, his understanding of history, and his deep-seated concern for the forces of chaos and tyranny. He views the katechon not as a static entity, but as a dynamic and mysterious force that restrains the advance of evil and disorder, particularly the rise of the Antichrist and a one-world totalitarian government. Thiel&#39;s understanding of this concept is not purely theological, but also deeply political, as he believes it provides a framework for understanding the dynamics of power and the challenges of the modern world. He sees the katechon as a necessary, although temporary, barrier against the forces of chaos and tyranny.&lt;/p&gt;
&lt;p&gt;At its core, Thiel understands the katechon as a restraining force, something that holds back the forces of chaos and prevents the world from descending into complete disorder. This force is not necessarily aligned with any specific political ideology or institution, and Thiel is skeptical of any entity that claims to fully embody the katechon. He views it as a precarious and often thankless task that is essential for maintaining a semblance of order in a world that is constantly threatened by violence and tyranny. He does not see the katechon as a perfect solution, but rather a temporary measure, a necessary delay against the advance of evil. He views the world as a battlefield between order and chaos, good and evil, and that the katechon is one of the forces that seeks to restrain the chaos.&lt;/p&gt;
&lt;p&gt;Thiel also sees the katechon as being related to, but not synonymous with, different historical forces and institutions, including certain aspects of the Roman Empire, elements within the Roman Catholic Church, and various individuals and groups throughout history who have sought to restrain the forces of chaos. He sees that these forces, while far from perfect, have played a role in holding back the rise of tyranny and preserving some semblance of order. He views these entities as temporary and limited manifestations of the katechon, as none of them can fully embody this restraining force, but still play an important historical role.&lt;/p&gt;
&lt;p&gt;Thiel often suggests that the United States is a potential candidate for the katechon, as a powerful nation that has, at times, sought to maintain global order. However, he is also critical of the U.S., and does not view it as a perfect embodiment of this force. Thiel is skeptical of all earthly institutions, and sees them as being vulnerable to corruption and abuse of power. He therefore views the katechon as being something that can be supported, but not fully embodied by any worldly institution. He seems to see the katechon as a force that is often imperfect and limited, but still plays an important role in restraining evil. He views anti-communism as being a manifestation of the katechon.&lt;/p&gt;
&lt;p&gt;Thiel also links the katechon to his concern about the rise of a &amp;quot;one-world totalitarian government&amp;quot;. He views this as a culmination of the forces of chaos and evil, and believes that it must be resisted at all costs. He sees the katechon as a force that actively opposes the rise of this government, which he believes would destroy individual freedom and human flourishing. He sees the one-world government as a threat to the katechon, and that these forces are ultimately opposed. He believes that this type of government is the type that the Antichrist would seek to establish, so that they may exercise their power over the world.&lt;/p&gt;
&lt;p&gt;While Thiel acknowledges the importance of the katechon, he also believes in the power of individual agency, and he emphasizes the importance of individuals in resisting the forces of evil and creating a better future. He does not believe that the future is determined, but that it is contingent on human action.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s understanding of the katechon is also deeply tied to his Christian eschatology and his interpretation of biblical prophecy. He views the world as being caught in a struggle between good and evil, and that the katechon is a force that restrains evil, even while recognizing that the forces of evil will ultimately be dealt with through a final reckoning. He sees the katechon as being temporary, and that it is not a final solution, but rather a way of delaying an ultimate judgement. He sees this as being a call to action to work against evil and promote good. He is not passive, and believes his own actions can have a positive influence in this struggle.&lt;/p&gt;
&lt;h2 id=&quot;secular-decline&quot; tabindex=&quot;-1&quot;&gt;Secular Decline&lt;/h2&gt;
&lt;p&gt;Thiel believes technological progress isn&#39;t innate to history but an expression of a specific cultural impulse rooted in Christianity that peaked during the Victorian Era of Western imperialism and has allegedly been declining since. Thiel&#39;s analysis of secular decline and technological stagnation paints a stark picture of the modern West, which he believes is now in a state of secular decline that needs to be reversed.&lt;/p&gt;
&lt;p&gt;He does not view this decline as a result of inevitable forces, but rather as a consequence of specific choices, philosophical shifts, and an inability to confront the core issues of human nature and societal organization. Thiel&#39;s perspective is a complex interplay of his views on history, technology, politics, and religion, all filtered through his unique lens, informed by Girard&#39;s mimetic theory and a Christian worldview.&lt;/p&gt;
&lt;p&gt;Thiel believes that the West has lost a clear vision for the future, and this has led to a sense of stagnation and decline. He connects this to a rise of &amp;quot;indeterminate optimism&amp;quot;, where people are optimistic about the future without a clear plan or path for getting there. He sees this type of optimism as dangerous, and that it sets up society for a disaster. He also sees a lack of long-term planning and a focus on short-term gains as being a form of sin. This sense of lost vision, for Thiel, is intimately tied to a broader secular decline, a rejection of traditional values, and a loss of purpose that has occurred since the counterculture movement. He also sees the counterculture of the 60s and 70s as being antithetical to progress.&lt;/p&gt;
&lt;p&gt;Thiel is deeply critical of modern secular governance, which he believes has failed to address the fundamental problem of violence in society. He argues that the Enlightenment &amp;quot;whitewashed away&amp;quot; this issue, leading to a naive and dangerous approach to politics. He is also critical of the &amp;quot;one world&amp;quot; government idea, viewing globalization as a destabilizing force and a threat to individual freedom. He believes that modern governments are unable to see the &amp;quot;big picture&amp;quot;, and that they are often unable to make real progress. He sees that the abandonment of political theology has made people &amp;quot;ever smaller cogs in an ever bigger machine.&amp;quot; He views the secularization of the West as contributing to both moral and technological decline.&lt;/p&gt;
&lt;p&gt;Thiel also connects secular decline to a cultural disintegration, especially the breakdown of the generational compact of the middle class. He sees this group as being defined by the belief that their children will do better than them, and he sees the erosion of this belief as a sign of decline. He believes that the modern world has become nihilistic, and this is tied to a loss of traditional values. He also rejects the idea that the world can &amp;quot;muddle through&amp;quot; to a better state, and that this type of thinking only sets up the world for disaster.&lt;/p&gt;
&lt;p&gt;Thiel also points to a conspiracy theory that his colleague Eric Weinstein calls the &amp;quot;distributed idea suppression complex.&amp;quot; This is the belief that a network of elite media organizations, bureaucracies, universities, and government-funded NGOs allegedly work together to suppress certain ideas and control public discourse. For Thiel, these institutions act as gatekeepers who determine which ideas can be discussed openly in society. He argues that this contributes directly to secular decline by preventing important but controversial ideas from being properly debated. Thiel believes the internet has begun to break DISC&#39;s control over narratives. He points to events such as Jeffrey Epstein&#39;s death, the autism vaccine controversy, and COVID-19 conspiracies as examples where public skepticism showed that these institutions could no longer fully control discourse. Nevertheless, he sees this complex as a major ongoing obstacle to progress and innovation. The suppression of ideas by DISC fits into his broader critique that modern institutions and power structures actively work to maintain stagnation rather than enable real advancement.&lt;/p&gt;
&lt;p&gt;Thiel believes that this secular decline is also reflected in a technological stagnation, particularly in fields beyond information technology. He contrasts the multifaceted progress of the first half of the 20th century, such as the development of rockets, agriculture, and medicine, with the more limited progress of recent decades, where &amp;quot;technology is synonymous with information technology.&amp;quot; He acknowledges advancements in the digital world, but suggests that progress in other fields has stagnated, and that even progress in information technology has slowed. He criticizes the current tech industry for focusing on incremental innovation and &amp;quot;fake technologies&amp;quot; that solve &amp;quot;fake problems&amp;quot; (string theory gets particular derision in his view), rather than pursuing ambitious, transformative innovations. He uses the phrase &amp;quot;we wanted flying cars, instead we got 140 characters&amp;quot; to encapsulate this sense of disappointment. He believes the focus on social media and online platforms has come at the expense of real progress.&lt;/p&gt;
&lt;p&gt;Thiel also suggests that hyper-specialization of knowledge has made it difficult to see the &amp;quot;big picture&amp;quot; and make progress. He sees modern academic experts as being more focused on power games than making real progress. He also sees &amp;quot;endless fake culture wars around identity politics&amp;quot; as a significant distraction that has slowed down real technological progress. He believes that a focus on progressive ideas has come at the expense of real innovation. He believes the problems of technological stagnation are a direct result of human choices. He seems to believe that &amp;quot;runaway science&amp;quot; and tech may actually be pushing the world toward disaster or maybe become a tool of the Antichrist if not curtailed.&lt;/p&gt;
&lt;p&gt;Underlying Thiel&#39;s analysis of secular decline and technological stagnation is his belief that mimetic desire plays a crucial role. He sees a focus on &amp;quot;me too&amp;quot; ideas rather than &amp;quot;zero to one&amp;quot; innovation as a result of mimetic forces, and that this prevents truly transformative progress. He sees mimetic rivalry as contributing to a culture of fear and distraction, and that people are more focused on &amp;quot;playing the game&amp;quot; rather than trying to make progress.&lt;/p&gt;
&lt;p&gt;However, Thiel does not view this situation as hopeless. He sees Christianity, specifically its emphasis on long-term planning, as a potential solution to these problems. He believes that a focus on the &amp;quot;fruits of eternity&amp;quot; is a better focus than the fleeting pleasures of the day. He believes that a Christian worldview can help people break free from the cycles of mimetic desire and help them to focus on the future. He also sees the Christian view of time, as being linear rather than cyclical, as important. He also sees the importance of human agency in choosing to make a positive change.&lt;/p&gt;
&lt;p&gt;Thiel views secular decline and technological stagnation as interconnected problems that stem from a loss of vision, a rejection of traditional values, a focus on short-term thinking, an inability to address the fundamentals of human nature, and the influence of mimetic desire. He believes that the West has lost its way, and that a return to long-term planning, a focus on innovation, and a renewed commitment to a positive vision of the future, all inspired by a Christian worldview, is necessary to overcome these challenges and move toward a better future. He sees that the problems are largely the result of human choices, and that new choices can change our trajectory. He does not see progress as inevitable, but rather as a result of human effort, and he sees his role as helping to guide people towards this future.&lt;/p&gt;
&lt;h2 id=&quot;original-sin-and-moral-trilemma&quot; tabindex=&quot;-1&quot;&gt;Original Sin and Moral Trilemma&lt;/h2&gt;
&lt;p&gt;Thiel&#39;s interpretation of original sin is deeply intertwined with his critique of what he calls &amp;quot;wokeism,&amp;quot; (an imprecise slur used by the American right to refer to progressive ideas they dislike) both concepts finding their foundation in his understanding of René Girard&#39;s mimetic theory and his Christian worldview. For Thiel, both &amp;quot;wokeism&amp;quot; and original sin are manifestations of a fundamental problem of human nature: a tendency towards mimetic desire, violence, and the obfuscation of the truth. He views both as deeply problematic, and he believes that Christianity is the key to escaping these destructive forces.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s understanding of &amp;quot;wokeism,&amp;quot; filtered through Girard&#39;s lens, positions it as a modern manifestation of mimetic rivalry. He sees identity politics, a central element of &amp;quot;wokeism,&amp;quot; as a competition between different groups for status, recognition, and power. This competition, driven by mimetic desire, inevitably leads to a cycle of resentment and blame, where groups seek to outdo one another in claiming victimhood. Thiel is highly critical of what he denotes the &amp;quot;victim olympics&amp;quot; which he believes as being a competition to see which group is the most victimized. He sees this preoccupation with victimization as unhealthy, leading to an &amp;quot;absolutization&amp;quot; of victim status, making forgiveness and reconciliation nearly impossible. For Thiel, &amp;quot;wokeism&amp;quot; is driven by an underlying desire to tear down existing hierarchies, rather than build up new ones, and is driven by the inherent resentments of mimetic desire.&lt;/p&gt;
&lt;p&gt;Thiel is equally critical of the utopian impulses that he sees as underlying &amp;quot;wokeism,&amp;quot; rejecting the idea that society can be perfected through political or social reforms. He views this as &amp;quot;indeterminate optimism&amp;quot; that distracts from genuine problem-solving. He emphasizes that rearranging existing social orders is not enough to solve the root problems of society. He believes that attempts at controlling and managing society are not as effective as creating new things. He sees the modern left as being &amp;quot;brainwashed&amp;quot; and unable to engage in productive dialogue. He also believes that the left actively tries to silence debate through the creation of &amp;quot;safe spaces.&amp;quot; He is also critical of globalization, viewing it as a force that undermines the power of nation-states and creates instability and conflict. Thiel sees these as all part of the problems inherent to &amp;quot;wokeism&amp;quot;, which ultimately seeks to control and limit progress and freedom.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s concept of original sin, heavily influenced by Girard, goes beyond a mere act of disobedience and focuses on a shift in human desire. He believes that the &amp;quot;fallen nature of man,&amp;quot; a core Christian concept, has made humans inherently prone to imitate the desires of others. This mimetic desire, according to Thiel, leads to envy, rivalry, and ultimately, violence. He sees it as the root cause of conflict and a never-ending cycle of violence. He believes that mimetic desire makes humans deeply influenced by the desires of others, creating the conditions for sin and violence. He views this not only as a personal problem, but a problem at the root of civilization.&lt;/p&gt;
&lt;p&gt;Thiel emphasizes that human sin is not just violent but also deeply obfuscated, meaning that people are often unaware of the true nature of their own desires and the violent implications of their actions. This obfuscation, he argues, perpetuates the cycle of violence, as people hide or deny their own roles in it. He sees the &amp;quot;city of man&amp;quot; as being built on hidden victims. This is tied to his view that people are not in control of themselves as much as they may like to think, and that the desires of others can overwhelm them.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s perspective on original sin leads him to reject the utopian ideals of perfectibility, mirroring his critique of &amp;quot;wokeism.&amp;quot; He believes it is impossible to create a perfect society because of the inherent flaws in human nature. Thiel is critical of &amp;quot;secularized&amp;quot; versions of utopianism, which he sees as simply attempting to solve a problem that they do not even understand. He does not believe that social or political reforms are enough, as these reforms cannot address the problem of mimetic desire.&lt;/p&gt;
&lt;p&gt;However, Thiel does not view original sin as an insurmountable problem, and this is where he connects it to a solution through Christian theology. He views Christianity, specifically the teachings of Jesus, as offering an antidote to mimetic violence. He believes that Christ&#39;s sacrifice provides a path to breaking the cycle of violence that is caused by mimetic desire. He believes that &amp;quot;death is evil&amp;quot; and should not be accepted. Thiel sees Christianity as a way to move past the endless cycles of violence. He categorically rejects ideologies that deny the reality of evil and that do not seek a transcendent solution to the problems of human nature.&lt;/p&gt;
&lt;p&gt;For Thiel, both &amp;quot;wokeism&amp;quot; and original sin are not simply disparate concepts, but intertwined manifestations of the same fundamental problem: mimetic desire and the inherent flaws of human nature. He believes that &amp;quot;wokeism&amp;quot; is driven by the same forces of mimetic desire that caused original sin, and he believes that both are fundamentally opposed to a Christian view of forgiveness and reconciliation. He sees Christianity as a force that subverts the cycle of mimetic violence and points the way towards a more just and peaceful future.&lt;/p&gt;
&lt;p&gt;From this understanding of original sin and its manifestations in modern society, Thiel develops a framework for interpreting how humans can respond to the moral weight of history. He presents this as a trilemma, outlining three distinct approaches to addressing historical injustices, and in his analysis, no other options are permitted.&lt;/p&gt;
&lt;p&gt;The first approach, which Thiel associates with Christianity, acknowledges that the past contained great evil and injustice, but emphasizes the importance of forgiveness and reconciliation. Through a Girardian lens, this perspective recognizes that historical atrocities such as colonialism, slavery, genocide, and countless other crimes against humanity were manifestations of mimetic violence and scapegoating mechanisms. The Christian response of forgiveness represents a deliberate break from the cycle of mimetic rivalry and retribution. By choosing forgiveness over vengeance, this approach follows Christ&#39;s example of refusing to participate in reciprocal violence. The emphasis on reconciliation acknowledges that both victims and perpetrators are caught in mimetic patterns that must be transcended, not through forgetting or minimizing past wrongs, but by consciously choosing to end the cycle of revenge. This aligns with Girard&#39;s view that Christianity uniquely reveals and provides an escape from mimetic violence. The Christian perspective maintains that while we must understand how historical injustices emerged from mimetic dynamics, we cannot allow those same dynamics to perpetuate &amp;quot;endless cycles of recrimination and counter-violence&amp;quot;.&lt;/p&gt;
&lt;p&gt;The second approach, which Thiel links to &amp;quot;wokeism&amp;quot; and modern progressive movements, can be understood through Girard&#39;s theory as itself caught in mimetic patterns while claiming to oppose them. While this perspective correctly identifies historical crimes and injustices, its refusal of forgiveness and insistence on eternal condemnation risks perpetuating the very mimetic dynamics that led to those original crimes. Thiel thinks wokeness retains the slave morality of Christianity without Christ to expose and prevent the scapegoat mechanism. There is no mechanism in place for forgiving transgressions, so the cycle of violence repeats endlessly as woke adherents compile an ever-expanding list of transgressions to justify engaging in more struggle sessions. From a Girardian view, the maintenance of righteous anger and the demand that certain acts be viewed as unforgivable represents a form of sacred violence. This results in the creation of &amp;quot;permanent outcasts&amp;quot; who must eternally bear the mark of transgression. The focus on maintaining moral condemnation can be seen as a new form of scapegoating mechanism, where historical perpetrators must be endlessly sacrificed on the altar of present justice. This approach, while claiming to prevent the recurrence of past violence, may paradoxically ensure its continuation by remaining trapped within mimetic patterns of accusation and exclusion. The insistence that victims&#39; memory requires eternal unforgiveness reveals how this perspective sacralizes victimhood itself, making it an untouchable category that demands perpetual veneration through maintained hostility.&lt;/p&gt;
&lt;p&gt;The third approach, which Thiel attributes to Bronze Age Pervert (a deeply controversial right-wing provocateur), Paganism, and Nietzschean philosophy, takes a radically different view that fundamentally rejects the moral premises of the other two positions. This perspective, drawing on Nietzsche&#39;s concept of master-slave morality, argues that what others call historical &amp;quot;injustices&amp;quot; were simply manifestations of power dynamics. In this view, the strong did what they could while the weak suffered what they must. There is nothing to forgive because, according to this perspective, no wrong was done; it was simply nature expressing itself through human affairs. This approach sees moral condemnation of historical actions as meaningless sentimentality, a weak-minded imposition of slave morality onto the raw reality of power relations. The Nietzschean view thus sidesteps the question of forgiveness entirely by rejecting the moral framework that would make forgiveness necessary or meaningful. Thiel categorically rejects this view, regarding it as &amp;quot;pagan&amp;quot;, but humors it as a valid third branch of his trichotomy in debates.&lt;/p&gt;
&lt;h2 id=&quot;kingdom-of-god-and-the-antichrist&quot; tabindex=&quot;-1&quot;&gt;Kingdom of God and the Antichrist&lt;/h2&gt;
&lt;p&gt;Thiel&#39;s vision of the &amp;quot;kingdom of God&amp;quot; and his interpretation of the Antichrist represent two sides of the same theological coin, forming a complex eschatological framework that shapes his worldview. Drawing heavily from René Girard&#39;s mimetic theory, Thiel sees human history as a linear journey moving toward a definite endpoint, where humanity faces a stark choice between two possible futures: the &amp;quot;limitless violence of runaway mimesis&amp;quot; or &amp;quot;the peace of the kingdom of God.&amp;quot;&lt;/p&gt;
&lt;p&gt;For Thiel, the Antichrist is not merely a single individual, but a recurring type throughout history, a dangerous political system, and a force that uses deception and fear to gain control. Central to this interpretation is Girard&#39;s provocative idea from &lt;em&gt;I Saw Satan Fall Like Lightning&lt;/em&gt;, where he suggests that the Christian concern for victims, which he considers one of Christianity&#39;s most revolutionary contributions to human culture, could be exploited and weaponized. In his later interviews, Girard explicitly associated this &amp;quot;caricature&amp;quot; of Christian values with the figure of the Antichrist. For Thiel, this Girardian insight reveals how the Antichrist operates: by perverting Christianity&#39;s core concern for the marginalized into a mechanism for power and control. This force he calls &amp;quot;victimism,&amp;quot; a term Girard described as &amp;quot;Victimism uses the ideology of concern for victims to gain political or economic or spiritual power.&amp;quot;&lt;/p&gt;
&lt;p&gt;Thiel views the Antichrist as a counterfeit of Christ, a figure who superficially imitates Christ but is fundamentally anti-Christian in nature. This counterfeit Christ may even present as being &amp;quot;more Christian than Christ&amp;quot;, while distorting the core teachings of Christianity (a form of &amp;quot;ultrachristianity&amp;quot;), especially concerning the use of power and violence. The Antichrist, in Thiel&#39;s understanding, co-opts Christian ideals of victim protection and uses them for their own purposes, making it difficult to recognize the evil beneath the veneer of compassion.&lt;/p&gt;
&lt;p&gt;This view aligns with Thiel&#39;s broader belief that the Antichrist is a recurring archetype throughout history. He notes that &amp;quot;in the time before Christ there were many forerunners to Christ. In the time after Christ there&#39;ll be many forerunners of the Antichrist.&amp;quot; Thiel points to historical figures like Nero, who persecuted Christians while styling himself as a divine ruler, and Napoleon, who presented himself as a liberator while establishing authoritarian control, as prototypes of this pattern. He even draws a parallel with Alexander the Great as a pre-Christ archetype, observing that &amp;quot;they both die at 33. Alexander conquers the world, Christ saves it.&amp;quot; For Thiel, these historical examples demonstrate how the Antichrist figure consistently emerges as a charismatic leader who promises liberation while ultimately seeking domination.&lt;/p&gt;
&lt;p&gt;Central to Thiel&#39;s interpretation is the idea that the Antichrist seeks to establish a totalitarian one-world government, a state of total control where individual liberty is suppressed. He sees this as a dangerous prospect, since such a government would have absolute power, without any checks or balances. Thiel emphasizes that the Antichrist&#39;s rise to power relies on deception and manipulation. This figure will not present as an obvious villain, but rather as a great humanitarian, a redistributor of wealth, and an effective altruist. Thiel selectively cherry-picks biblical passages like 1 Thessalonians 5:2-4 as literal prophecies: &amp;quot;For when they shall say, Peace and safety; then sudden destruction cometh upon them, as travail upon a woman with child; and they shall not escape.&amp;quot; He interprets this to mean the Antichrist will promise &amp;quot;peace and safety&amp;quot; as primary goals, and while these ideas are not inherently evil, Thiel believes that these become dangerous when used to justify oppression and the loss of freedom.&lt;/p&gt;
&lt;p&gt;Thiel believes that the Antichrist will use the fear of Armageddon to gain power. He identifies several specific existential threats that could serve as catalysts for this fear: nuclear and thermonuclear weapons capable of global annihilation; environmental destruction and climate change; bioengineering and bioweapons that could trigger pandemics; nanotechnology with its potential for uncontrolled self-replication; autonomous weapons systems (&amp;quot;killer robots&amp;quot;); and perhaps most concerning to Thiel, artificial intelligence that could escape human control. He views these technological developments as potential instruments of Armageddon that the Antichrist could exploit to consolidate power through fear.&lt;/p&gt;
&lt;p&gt;Thiel crystallizes this apocalyptic choice by drawing a direct parallel between its Christian and secular formulations. In an interview with The New York Times, he identified the atheist, post-nuclear framing as “One World or None,” where a global government is presented as the sole alternative to planetary annihilation. He then equates this to the Christian framing of “Antichrist or Armageddon,” where humanity faces a choice between the totalitarian one-world state of the Antichrist or the utter chaos of destruction. For Thiel, these are fundamentally “the same question.” Both present a false dichotomy, a trap where the fear of Armageddon is manipulated to make a tyrannical global order seem like the only rational solution, whether it&#39;s proposed by secular scientists or a deceptive pseudo-savior.&lt;/p&gt;
&lt;p&gt;In stark contrast to this vision of the Antichrist stands Thiel&#39;s conception of the &amp;quot;kingdom of God.&amp;quot; He establishes a clear dichotomy between the &amp;quot;city of man&amp;quot; and the &amp;quot;city of God,&amp;quot; seeing the former as built on hidden victims and &amp;quot;obfuscated violence.&amp;quot; The modern world, with its secular governance, emphasis on short-term gains, and tendency toward mimetic conflict, represents the &amp;quot;city of man,&amp;quot; prone to chaos and instability. In contrast, the &amp;quot;city of God&amp;quot; represents a state of peace, justice, and divine order, where these destructive forces are overcome and the world is reborn, allegedly into a competative patchwork of libertarian enclaves where hyper-competative market pressures and mimesis are balanced and kept in check through universal Christian values.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s view of history is informed by the Judeo-Christian concept of linear time, where history moves toward a definite end point. He believes that humanity is moving toward a final, definitive conclusion, either positive or negative. This concept of linear time is central to his understanding of progress, as he views history as &amp;quot;going somewhere,&amp;quot; and that choices people make matter. He sees Christ&#39;s ministry as a &amp;quot;hinge moment of history&amp;quot; where a new path became available. Notably, Thiel emphasizes the biblical narrative arc where human society begins in a garden (Eden) and culminates in a city (the New Jerusalem), reflecting his belief that civilization&#39;s development follows a divinely ordained trajectory from nature toward an ultimate developed civilization.&lt;/p&gt;
&lt;p&gt;Thiel sees the framing of &amp;quot;one world or none&amp;quot; as a false dilemma, which will lead people to accept a one-world government. He believes this is a trap that the Antichrist will use to come to power, and that this false choice will make global tyranny seem like the only way to avoid Armageddon. He connects the rise of the Antichrist to modern technology, believing that such a totalitarian government would use mass surveillance to monitor &amp;quot;every keystroke everywhere&amp;quot; to prevent any dissent. This would create a society where privacy is nonexistent and challenges to authority become impossible. Thiel believes that there is no real need to choose between total destruction and such a technological totalitarian regime, and that other paths must be explored.&lt;/p&gt;
&lt;p&gt;Through the lens of Christian eschatology and his critique of modernity, Thiel envisions the &amp;quot;kingdom of God&amp;quot; and the threat of the Antichrist as a pivotal battleground where humanity&#39;s fate hangs in the balance. Standing at an apocalyptic crossroads, he sees two stark possibilities: catastrophic ruin or divine peace. For Thiel, mimetic desire remains the dark undercurrent driving human conflict, the key that unlocks both our fallen nature and potential salvation. Yet he insists that human agency, specifically our capacity to choose between these divergent paths, remains paramount.&lt;/p&gt;
&lt;h2 id=&quot;immortality-%26-death&quot; tabindex=&quot;-1&quot;&gt;Immortality &amp;amp; Death&lt;/h2&gt;
&lt;p&gt;Thiel fundamentally rejects what he sees as the widespread philosophical and religious acceptance of death, framing it instead as humanity&#39;s greatest challenge, one that requires active combat rather than passive resignation. Thiel&#39;s worldview is built around this core conviction, motivating substantial investments and personal commitments aimed at significantly extending human lifespans, potentially achieving a form of technological immortality. His perspective positions him as a leading proponent of transhumanist ideals focused on overcoming biological limitations. Thiel&#39;s position contains several internal tensions. His simultaneous embrace of Christianity and transhumanism represents a philosophical hybrid that many would find contradictory, as traditional Christian theology emphasizes resurrection and eternal life through divine grace rather than technological intervention.&lt;/p&gt;
&lt;p&gt;The philosophical underpinnings of Thiel&#39;s quest against death are deeply rooted in his philosophical framework. He explicitly characterizes death as &amp;quot;evil&amp;quot; and &amp;quot;wrong,&amp;quot; a perspective he ties to his Christian background, suggesting a convergence between religious opposition to death and the transhumanist goal of defeating it. Thiel argues that the fight against mortality is a moral imperative, a crusade against the ultimate injustice. Further extending this moral argument, he provocatively reframes the pursuit of immortality as an egalitarian endeavor, suggesting that the greatest inequality exists between the living and the dead. This framing attempts to imbue the potentially self-interested goal of personal life extension with a broader, justice-oriented significance.&lt;/p&gt;
&lt;p&gt;Thiel frames his opposition to death in explicitly moral terms, viewing mortality not as a natural process but as an evil to be conquered. Despite his Christian background, or perhaps because of it, Thiel has stated: &amp;quot;The one part of the Christian view that I believe more strongly than anything is that death is evil, that it&#39;s wrong and we should not accept it and fight it any way we can.&amp;quot; This perspective positions the fight against death as a moral crusade rather than merely a scientific endeavor. Thiel selectively cites Matthew 6:9-13 from the Lord&#39;s Prayer, specifically &amp;quot;Thy kingdom come; Thy will be done, on earth as it is in heaven,&amp;quot; to support his vision of technological immortality as bringing heaven to earth. Ironically, in his conversation with theologian N.T. Wright, Thiel claimed to see parallels between Christianity and transhumanism, remarking that &amp;quot;the thing that strikes me is how similar they are.&amp;quot; This stands in contrast to Wright&#39;s own interpretation, which is that &amp;quot;Jesus&#39;s resurrection is the beginning of God&#39;s new project not to snatch people away from earth to heaven but to colonize earth with the life of heaven. That, after all, is what the Lord&#39;s Prayer is about.&amp;quot; This synthesis of religious faith with technological immortality represents a highly contrarian element of Thiel&#39;s worldview and separates him from both many secular transhumanists and most Christians. In his essay &amp;quot;Against Edenism,&amp;quot; he argues that Christianity promotes an &amp;quot;eschatological frame&amp;quot; in which humans work with God to build &amp;quot;the kingdom of heaven today, here on Earth.&amp;quot;&lt;/p&gt;
&lt;p&gt;Thiel rejects the conventional Kübler-Ross grief cycle that ends with acceptance of death. Instead, he advocates extending the &amp;quot;anger&amp;quot; stage indefinitely: &amp;quot;I&#39;d like the fighting stage to be really long&amp;quot;. This perspective challenges the psychological wisdom that peace comes through acceptance, suggesting instead that productive rage against mortality could drive innovation and progress. By rejecting acceptance, Thiel positions himself against both religious traditions that emphasize peaceful surrender to mortality and secular approaches that counsel coming to terms with death&#39;s inevitability.&lt;/p&gt;
&lt;p&gt;Thiel explicitly rejects Steve Jobs&#39; famous notion that death gives life meaning and urgency. Where Jobs called death &amp;quot;the single best invention of life,&amp;quot; Thiel counters: &amp;quot;I believe the exact opposite of that. Maybe it&#39;s when people think they have no time left that they don&#39;t undertake things&amp;quot;. This rejection of mortality as a source of meaning represents a profound break with existentialist philosophy and various religious traditions that find value in life&#39;s finite nature.&lt;/p&gt;
&lt;p&gt;For most Christians throughout history, death is not seen as an ultimate evil but as a transition to eternal life, a doorway to heavenly reunion with God. The Christian tradition has provided billions with profound comfort in the face of mortality, teaching that this life is merely a &amp;quot;veil of tears&amp;quot; before the glory of heaven. Thiel, however, places his faith in technological solutions to cheat death entirely, rather than trusting in divine grace and resurrection. This stance borders on the hubristic rejection of divine order that many Christian traditions would view with skepticism. The resulting tension suggests that, despite his Christian rhetoric, Thiel may fundamentally doubt the metaphysical claims about the afterlife that form the basis of Christian doctrine.&lt;/p&gt;
&lt;p&gt;In a very revealing interview with The Atlantic magazine, Thiel&#39;s profound death anxiety emerges as a central psychological force. Thiel traced his fascination with immortality to the elves in Tolkein&#39;s writing, calling them “humans who don’t die” and asking, “Why can’t we be elves?” He confesses his inability to confront mortality, refusing even to write a will because he sees it as &amp;quot;defeatist.&amp;quot; One might reason that this death anxiety may fundamentally shape his entire worldview. It is sublimated into his advocacy for monopolistic capitalism, extreme wealth concentration, and deregulated biomedical research, each of which could potentially create conditions that might allow him to cheat death through market solutions. His philosophical framework, political positions, and investment strategies could be interpreted as elaborate defensive structures built around this core existential terror. While this interpretation is purely speculative, it suggests that Thiel&#39;s quest for immortality may be the hidden foundation of much of his intellectual edifice. His economic and political prescriptions, in this view, serve as means to achieve this end.&lt;/p&gt;
&lt;h2 id=&quot;criticism&quot; tabindex=&quot;-1&quot;&gt;Criticism&lt;/h2&gt;
&lt;p&gt;Peter Thiel&#39;s framework, upon closer inspection, collapses into a self-serving edifice arising out of ressentiment and plutocratic will to power and designed to legitimize an elitist, anti-democratic agenda. It awkwardly fuses a selective interpretation of Christianity with techno-utopian aspirations, all while riddled with logical contradictions and unexamined theological presuppositions. Thiel weaponizes eschatological rhetoric to demonize international cooperation and progressive ideologies as an apocalyptic precursor, casting himself—a billionaire with immense global influence—as a beleaguered hero resisting nebulous forces of evil. This intellectual project is characterized by flagrant cherry-picking, opportunistically deploying concepts from Girardian theory and diverse philosophies without reconciling their inherent contradictions, particularly when such reconciliation would challenge his entrenched political and economic interests. The outcome is not a coherent philosophy but a sophisticated rationalization, a veneer of intellectual depth masking a project driven by personal grievances and anxieties transformed into cosmic dramas, where his fears become universal truths and his preferences, moral imperatives.&lt;/p&gt;
&lt;p&gt;Perhaps most fundamentally, Thiel&#39;s entire philosophical framework rests upon an unexamined presupposition: that Christianity is true (or at minimum, uniquely useful). This is not a conclusion reached through philosophical analysis but rather an axiomatic starting point that remains undefended against the possibility that Christianity itself could be yet another myth, and potentially even a harmful one. Thiel uses concepts like the Second Coming, the Antichrist, Apocalypse, and the katechon not merely as metaphors, but as fundamental, explanatory lenses for understanding history, politics, and the current cultural moment. He presupposes that this Judeo-Christian eschatological framework offers deeper insights than secular or purely philosophical analysis. If this presupposition were removed, his entire intellectual edifice would collapse. By treating Christian cosmology as somehow exempt from the critical scrutiny he applies to other worldviews, Thiel creates a self-reinforcing system that cannot engage honestly with the prospect that his theological foundation might be as mythological as the pagan beliefs he dismisses. This circular reasoning reveals the fragility of his framework: it depends entirely on accepting, without sufficient justification, a specific religious tradition as uniquely revelatory. And there is no &lt;em&gt;a priori&lt;/em&gt; reason to accept this premise.&lt;/p&gt;
&lt;p&gt;However, even if this foundational Christian premise were accepted (a commitment some might make akin to Kierkegaard&#39;s &#39;leap of faith&#39; beyond rational proof), Thiel&#39;s framework would still be plagued by additional internal structural problems. Thiel uncritically assumes the fundamental correctness of René Girard&#39;s mimetic theory. Thiel assumes Girard&#39;s theories of mimetic desire, scapegoating violence as the foundation of culture, and the unique, destabilizing effect of the Christian revelation (unveiling the scapegoat mechanism) as essentially true and historically operative. He assumes this unveiling inevitably leads toward desacralization and potentially apocalyptic violence.&lt;/p&gt;
&lt;p&gt;However, this uncritical adoption of Girardian theory ignores the substantial philosophical critiques that have been leveled against Girard&#39;s work. A primary flaw is Girard&#39;s theoretical overreach. What begins as plausible observations about mimetic psychology eventually transforms into a totalizing theoretical system attempting to explain every aspect of human nature. This overreach is particularly evident in Thiel&#39;s application, where mimetic desire becomes the universal explanation for everything from market competition to political movements he opposes.&lt;/p&gt;
&lt;p&gt;The methodological foundations of Girard&#39;s work are equally problematic. His theories regarding mimetic desire are derived not from empirical research or controlled studies, but from literary analysis; in other words, by reading works of fiction. While this approach may yield interesting thought experiments, it hardly constitutes a rigorous basis for sweeping claims about human nature. When Thiel applies these literary extrapolations to complex social and economic phenomena, he compounds this methodological weakness.&lt;/p&gt;
&lt;p&gt;Most damningly, Girard&#39;s theory arguably fails Karl Popper&#39;s criterion of falsifiability. There seems to be no possible counter-example that could refute Girard&#39;s thesis. If a violent myth or ritual is considered, Girard argues this confirms his hypothesis. If a non-violent myth is examined, Girard claims this also confirms his theory because cultures &amp;quot;erase tracks of violence.&amp;quot; Such circularity renders the theory practically unfalsifiable and thus, from a scientific perspective, limited in its explanatory power: a theory that explains everything often explains nothing specifically.&lt;/p&gt;
&lt;p&gt;Girard&#39;s Christian apologetics further undermine his theoretical objectivity. His hermeneutic approach goes to great lengths to highlight violence in non-Christian rituals while offering an exceptionally charitable reading of biblical texts. This double standard is particularly evident in his exaggeration of the contrast between myths and the Bible. Girard seems predisposed to find sanctioned violence in myths while overlooking similar elements in biblical narratives. When studying myths, Girard is effectively just seeing faces in the clouds, projecting violence where alternative interpretations are equally plausible.&lt;/p&gt;
&lt;p&gt;Even if we grant Girard&#39;s premise about Christianity&#39;s unique role in revealing scapegoating mechanisms, this does not necessarily imply a divine origin. There are alternative sociological explanations. For example, it is possible that biblical authors sympathized with victims because they themselves were victims under ancient empires. Girard&#39;s leap to supernatural causation represents an ad ignorantiam fallacy: the fact that we cannot currently explain a phenomenon does not imply supernatural origins.&lt;/p&gt;
&lt;p&gt;Thiel compounds these problems by selectively applying Girardian concepts. He uses mimetic theory to delegitimize his opponents while exempting himself and his ideological allies from the same analysis. This selective deployment demonstrates that, for Thiel, mimetic theory operates less as a genuine analytical tool and more as a convenient rhetorical cudgel.&lt;/p&gt;
&lt;p&gt;His application of mimetic theory to political movements appears to be a post-hoc rationalization designed to delegitimize ideologies he finds threatening. Rather than engaging with the substance of liberal and progressive arguments, Thiel has pre-judged these movements and subsequently developed a theoretical framework that conveniently explains away their motivations as mere mimetic desire. His dismissal of social justice movements as status competition deliberately ignores the straightforward goal many participants share: creating a more equitable society. Similarly, his characterization of environmental activism as mimetic rivalry sidesteps the overwhelming scientific consensus on climate change and its documented impacts. This selective application of mimetic theory reveals more about Thiel&#39;s own biases than about the movements he critiques. His apparent fear of &amp;quot;cancel culture&amp;quot; may simply reflect the uncomfortable reality that many of his ideas are increasingly considered abhorrent in modern discourse—not because of mimetic contagion, but because society&#39;s moral understanding has evolved to recognize the harm such views can cause.&lt;/p&gt;
&lt;p&gt;Finally, Girard&#39;s reliance on religious terminology to describe psychological mechanisms creates unnecessary confusion and obscures rather than clarifies his concepts. When Thiel adopts this language, particularly in secular contexts like business or politics, it further muddles the discourse and makes critical evaluation more difficult. For example Thiel&#39;s concept of the Antichrist is used incoherently throughout his writing. It shifts from a potential high-agency individual to symbolic literary figures, to potentially an institution or system embodying risk aversion (&amp;quot;peace and safety&amp;quot;). This conceptual slipperiness allows Thiel to apply the apocalyptic label to contemporary trends he dislikes (bureaucracy, safetyism), but it drains the concept of theological or philosophical rigor. It becomes a floating signifier for &amp;quot;bad outcome&amp;quot; within his chosen framework.&lt;/p&gt;
&lt;p&gt;Ultimately, Thiel&#39;s embrace of a reductionist Girardianism appears less a dispassionate intellectual choice and more a projection of his personal anxieties. He sees mimetic desire as the primary source of all conflict, allowing him to reduce complex social phenomena to a simplistic and ultimately self-serving narrative of human imperfection. By casting human nature as inherently flawed and prone to imitation, he can absolve himself of any personal responsibility for the negative consequences of his actions, conveniently blaming the inherent mimetic nature of others for all the problems he sees in the world.&lt;/p&gt;
&lt;p&gt;In essence, Thiel&#39;s reliance on Girard means his framework is critically dependent on a theory plagued by questionable validity, suffers from methodological weaknesses, exhibits clear religious bias, and ultimately offers a circular, unfalsifiable account of human behavior. His uncritical embrace of Girardian thought reveals less a profound intellectual engagement with human nature and more a convenient narrative that allows him to position himself as uniquely enlightened while dismissing his critics as merely trapped in mimetic rivalry.&lt;/p&gt;
&lt;p&gt;Beyond his reliance on Girard, Thiel&#39;s specific interpretation of Christianity invites substantial critique, particularly regarding his selective and heterodox interpretations. His emphasis on the apocalyptic aspects of Christianity, combined with a strong focus on human agency, appears to be more of a justification for his own political views than a genuine expression of religious belief. Thiel uses Christian concepts, such as the Antichrist, to support his skepticism of a one-world government, his preference for individual power, and his rejection of utopianism, all of which ultimately align with his desire for concentrated power in the hands of a select few. Furthermore, while Thiel emphasizes the non-violence of Jesus, his own actions, such as supporting hawkish politicians and investing in defense technologies, reveal a disconnect between his stated beliefs and his practical choices. He seems to use the language and symbols of Christianity to create a story that supports his own worldview, and that justifies his own power.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s hermeneutical approach exemplifies a problematic selectivity that borders on eisegesis rather than exegesis. He employs a Straussian hermeneutic of suspicion to extract ostensibly &amp;quot;hidden&amp;quot; meanings from biblical texts, yet this methodology functions primarily as post-hoc rationalization for his pre-established political commitments. By cherry-picking disparate verses, particularly apocalyptic passages from Revelation and eschatological warnings from Thessalonians, Thiel imposes a false univocality upon inherently polysemic texts. This approach treats complex scriptural passages as if their meaning is singular and directly applicable to his contemporary concerns, conveniently sidestepping their rich history of varied exegesis. The textual corpus of scripture, with its contradictions, ambiguities, and competing theological frameworks, functions more as a Rorschach test onto which Thiel projects his ideological preoccupations than as a stable foundation for his claims. For instance, while Thiel heavily emphasizes apocalyptic foresight and long-term, definite plans, he conveniently overlooks Jesus&#39;s own admonition in Matthew 6:34: &amp;quot;Therefore do not worry about tomorrow, for tomorrow will worry about itself. Each day has enough trouble of its own.&amp;quot; Such a call for present-moment focus and trust stands in stark contrast to Thiel&#39;s selective focus on eschatological urgency and grand, future-oriented designs. His assertion of singular, definitive readings constitutes a sophisticated sophistry, leveraging the cultural authority of biblical texts while eliding the interpretive responsibility such engagement demands.&lt;/p&gt;
&lt;p&gt;Furthermore, Thiel&#39;s eschatology is not an accepted a priori truth but a specific theological construct he imposes as an objective lens for historical and political interpretation. This approach blinds him to the empirical reality that the post-war liberal order he so disdains actually represents the period of greatest growth, stability, and peace in human history. His apocalyptic yearnings reveal less about the world&#39;s trajectory and more about his personal distaste for the dominant political order and his fears of a liberal world system that has, contrary to his narrative, delivered unprecedented prosperity and reduced global violence. There appears to be a troubling instrumentalization at work here. Thiel&#39;s eschatological framework seems less a sincere theological position and more a convenient vehicle for advancing his preferred right-wing political revival. His desire for apocalypse functions not as a coherent argument about the world&#39;s future but as a rhetorical device that creates urgency around his political project. This allows him to position himself as a prophetic voice in a time of alleged decline, despite evidence to the contrary.&lt;/p&gt;
&lt;p&gt;Thiel also has a Manichean worldview, a stark dichotomy between good and evil, which overly simplifies complex issues and ignores the nuances of the human condition. This simplistic view of the world, where he positions himself as a force for order in a chaotic universe, fuels his self-perception as a harbinger of the future, a role that borders on hubris. His constant emphasis on his unique ability to see &amp;quot;secrets&amp;quot; that others cannot, also suggests a narcissistic view of himself as someone who is uniquely qualified to lead.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s political and social views are equally problematic, marked by a deep-seated anti-democratic sentiment and a preference for elitist structures. Thiel&#39;s statement that &amp;quot;freedom and democracy are incompatible&amp;quot; reveals his fundamentally distorted conception of freedom. He prioritizes negative liberty (&amp;quot;freedom from&amp;quot;) rather than positive liberty (&amp;quot;freedom to&amp;quot;), which shapes his perspective. This framing allows him to position democracy itself, rather than its imperfect implementations, as the enemy of his narrowly defined freedom. What Thiel truly seeks is not universal freedom but rather exemption from collective governance for himself and other elites. His rejection of democracy stems not from principled philosophical analysis but from a deep-seated psychological aversion to being subject to any power structure he cannot control or escape. Instead of engaging constructively with democracy&#39;s flaws and working toward reforms that might balance individual rights with collective decision-making, Thiel dismisses the entire democratic project. This wholesale rejection conveniently serves his interests as a billionaire seeking to operate beyond accountability to the societies that enabled his wealth and power. His libertarian conception of freedom thus functions primarily as ideological cover for what is essentially an aristocratic worldview that privileges the autonomy of the powerful while dismissing the democratic rights of ordinary citizens. This dismissal of collective action is rooted in his belief that the masses are incapable of reason, and that they must be led by a select elite.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s political framework appears to suffer from what Karl Popper termed the &amp;quot;paradox of tolerance.&amp;quot; While Thiel advocates for pluralism and freedom, his preferred political structures would likely lead to less diversity of thought and more concentrated power. His support for figures and movements that actively work to restrict civil liberties and democratic participation suggests that his commitment to freedom is selective and instrumental rather than principled. In &lt;em&gt;The Poverty of Historicism&lt;/em&gt;, Popper also warned against the crux of Thiel&#39;s philosophy: historicism, which is the idea that history unfolds according to predetermined laws. Popper argued that such beliefs can justify authoritarianism by promoting the notion that drastic measures are necessary to avert perceived existential threats.&lt;/p&gt;
&lt;p&gt;Popper’s critique reveals historicism&#39;s methodological flaws and their totalitarian implications. He argued that historicists fundamentally err by mistaking temporary historical trends for immutable laws, failing to account for changing conditions that could alter or halt them. This elevation of a single, subjective interpretation into a predictive theory dangerously conflates what a historicist believes &lt;em&gt;will happen&lt;/em&gt; with what &lt;em&gt;should happen&lt;/em&gt;, thereby removing societal aims from the realm of democratic choice. The most perilous consequence of this logic is the perceived need to &amp;quot;remodel&amp;quot; humanity to fit the prophesied future, a coercive project that justifies sacrificing individual liberty for a supposedly inevitable outcome and stands as the antithesis of Popper&#39;s idea of the open society.&lt;/p&gt;
&lt;p&gt;A central tenet of Thiel&#39;s worldview is his belief that Western civilization faces secular decline, with technological stagnation as both symptom and consequence. This claim, however, rests on shaky foundations. Thiel frequently overindexes on perceived slowdowns in physics while overlooking remarkable advances in other fields. He attributes this supposed stagnation to a peculiar cause: an unconscious, collective fear of apocalyptic violence. Thiel believes this fear stems from Christian revelation and is amplified by technologies like nuclear weapons. According to Thiel, this deep-seated anxiety drives modern scientific bureaucracy, demographic decline, and what he terms a cultural &amp;quot;retreat to interiority.&amp;quot;&lt;/p&gt;
&lt;p&gt;This monocausal explanation appears more politically motivated than empirically grounded. Thiel&#39;s narrative conveniently ignores transformative breakthroughs in renewable energy, artificial intelligence, biotechnology, and revivals in space exploration. Economists like &lt;a href=&quot;https://www.penguin.co.uk/books/305469/the-entrepreneurial-state-by-mazzucato-mariana/9780141986104&quot;&gt;Mariana Mazzucato&lt;/a&gt;, in her book &lt;em&gt;The Entrepreneurial State&lt;/em&gt;, have demonstrated that innovation flourishes through complex interactions between public investment, regulatory frameworks, and private enterprise. This evidence challenges Thiel&#39;s simplistic analysis of scientific progress. By dismissing these nuanced factors, Thiel promotes a deterministic and reductionist view of innovation that fails to acknowledge how innovation actually emerges from the interplay of public policy, global collaboration, and diverse market incentives.&lt;/p&gt;
&lt;p&gt;Moreover, Thiel&#39;s technological determinism and emphasis on &amp;quot;definite optimism&amp;quot; reveal the limitations of bourgeois thought in understanding historical development. While he correctly identifies stagnation in certain areas of technological progress, he fails to consider this as a symptom of capitalism&#39;s internal contradictions. In particular, the profit motive and private ownership of the means of production increasingly act as fetters on technological progress. His solution, which involves concentrating more power in the hands of a technological elite, simply intensifies these contradictions rather than resolving them. His fear of &amp;quot;chaos&amp;quot; and &amp;quot;mimetic violence&amp;quot; is, in reality, a fear of proletarian revolution and the potential disruption of capitalist social relations. The religious and philosophical framework he constructs serves primarily as an ideological superstructure to justify and maintain these relations of production. This demonstrates precisely what Marx meant when he wrote that &amp;quot;the ruling ideas of each age have ever been the ideas of its ruling class.&amp;quot;&lt;/p&gt;
&lt;p&gt;This vision ultimately points toward a future of eco-apartheid and disaster capitalism on an unprecedented scale, where Thiel&#39;s &amp;quot;chosen few,&amp;quot; defined by ideology and resources, attempt to seal themselves off from global chaos. They leverage their technological and financial power not to solve crises, but to ensure their own isolated prosperity. His talk of &#39;exit&#39; and &#39;preserving Western civilization&#39; rings hollow, serving as justification for abandoning humanity to the ravages of a planet made uninhabitable by the very systems from which this elite profited. The least charitable interpretation sees his ideology not merely as contrarian intellectualism, but as a deliberate blueprint for a neo-reactionary ethnostate built around his peculiar fusion of Christian-inflected authoritarianism and techno-capitalist elitism. In this scenario, his envisioned &#39;City of God&#39; becomes simply an &#39;exit&#39; for an exclusive, &#39;purified&#39; few.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s adoption of Schmittian skepticism towards democratic governance represents another major flaw in his philosophical framework. Drawing heavily on Carl Schmitt&#39;s critique of liberal democracy, Thiel sees democratic institutions as fundamentally unable to handle existential crises without devolving into authoritarianism. However, this view overlooks decades of political theory and real-world evidence demonstrating democracy&#39;s resilience. Political theorists like Jürgen Habermas, in his work &lt;em&gt;A New Structural Transformation of the Public Sphere and Deliberative Politics&lt;/em&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Structural_Transformation_of_the_Public_Sphere&quot;&gt;have shown&lt;/a&gt; how liberal democracies can successfully navigate major crises through deliberative processes and strong civil institutions, without abandoning democratic principles. The post-WWII recovery of Western Europe, South Korea&#39;s transition to democracy, and the peaceful resolution of the Cold War all demonstrate how democratic systems can handle extreme challenges while maintaining their core values.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s reading of Schmitt appears selective and self-serving, focusing primarily on Schmitt&#39;s critique of liberalism while ignoring the problematic implications of Schmitt&#39;s own authoritarian alternatives. This selective interpretation reveals more about Thiel&#39;s anxieties regarding state power than it does about any genuine limitations of democratic governance. His fear of democratic &amp;quot;tyranny of the majority&amp;quot; masks a deeper discomfort with collective decision-making that might constrain the actions of wealthy individuals like himself. Rather than engaging seriously with democratic theory&#39;s rich tradition of addressing these concerns through constitutional limits, separation of powers, and protection of individual rights, Thiel retreats into a simplistic narrative of democracy&#39;s inevitable failure.&lt;/p&gt;
&lt;p&gt;If Thiel&#39;s apocalypticism is actually sincere, and not sophistry, then at the heart of his philosophical project is a clear, palpable dread of meaninglessness. He seeks to fill this void with a rigid and often fantastical vision of a divinely ordained future. Unlike the existentialist tradition, which views nihilism as a perfectly fine starting point for finding meaning, Thiel recoils from such a prospect. He has failed to seriously engage with Nietzsche&#39;s substantive distinction between passive nihilism (resignation and despair in the face of meaninglessness) and active nihilism (the courageous destruction of old values to make room for the creation of new ones). While Thiel correctly criticizes passive nihilism as a destructive force in civilization, he conveniently ignores active nihilism&#39;s creative potential. Nietzsche envisioned humanity confronting the void and forging new values in its wake. Nietzsche understood that the collapse of traditional meaning systems, especially those rooted in religion and metaphysics, represented not merely a crisis but an opportunity for renewal. Thiel, by contrast, desperately clings to predetermined purpose and transcendent end-states. He rejects secularism as inherently nihilistic rather than recognizing it as a framework where meaning can be constructed without divine foundations. His characterization of secularism as &amp;quot;indeterminate optimism&amp;quot; lacking clear vision reveals his unwillingness to consider that meaning-making without predetermined endpoints might be not only possible but necessary in a post-metaphysical world.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s moral trilemma presents a false choice by artificially limiting the possible responses to historical injustice, which further reveals the inadequacies of his framework. There are numerous other frameworks for understanding and addressing historical wrongs that do not fit neatly into his three categories. For example, Rawls&#39; theory of justice offers a systematic approach through the &amp;quot;veil of ignorance&amp;quot; thought experiment. This asks us to design principles of justice as if we did not know our place in society. The result is a framework for addressing historical inequities that does not require either Christian forgiveness or eternal condemnation. Under Rawlsian justice theory, we can acknowledge historical wrongs while working to create fair institutions and compensatory mechanisms that reasonable people would agree to behind the veil of ignorance. Secular moral philosophy also provides other sophisticated approaches to justice and reconciliation, including restorative justice practices that focus on healing and rebuilding rather than punishment or absolution. Progressive movements are more nuanced than Thiel suggests. Many combine acknowledgment of past wrongs with practical efforts at repair and prevention, without demanding eternal unforgiveness. In addition, many indigenous and non-Western traditions offer their own frameworks for dealing with historical trauma and injustice that do not map onto any of Thiel&#39;s three options. By presenting only these three choices, Thiel creates a strawman of both progressive politics and secular moral philosophy, while elevating his preferred Christian framework as the only viable alternative to either eternal condemnation or amoral power worship.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s assertion that &lt;em&gt;only&lt;/em&gt; Christianity enables profound, cycle-breaking forgiveness is empirically dubious, contradicting the lived reality of forgiveness across diverse philosophical and religious traditions. Buddhism, for instance, offers a framework that &lt;a href=&quot;https://tricycle.org/magazine/forgiveness-not-buddhist/&quot;&gt;does not require forgiveness&lt;/a&gt; and is instead centered on freeing oneself from suffering by letting go of resentment and cultivating compassion and loving-kindness, all without requiring divine intervention. Similarly, secular reconciliation processes have proven successful worldwide, grounded in powerful humanistic motivations such as empathy, reason, and the pragmatic desire for peace and societal well-being. Thiel&#39;s critique arbitrarily dismisses these sufficient non-Christian foundations, insisting on an exclusive (and historically flawed, given religion&#39;s own role in violence) Christian-Girardian transcendent anchor. His argument ultimately relies on a circular, unfalsifiable definition of &amp;quot;true&amp;quot; forgiveness that conveniently discounts genuine moral capacity existing outside his specific theological framework.&lt;/p&gt;
&lt;p&gt;Finally, Thiel&#39;s understanding of the state as built primarily on a monopoly of violence, while reductionist, points to real limitations in modern democratic theory. While contemporary political theorists emphasize institutional frameworks, accountability mechanisms, and complex governance systems, history suggests we should be cautious about placing too much faith in these structures alone. The failures of past attempts at concentrated state power, whether authoritarian or democratic, demonstrate that no political system perfectly resolves the tension between collective action and individual rights. However, this does not validate Thiel&#39;s anti-democratic alternative. Rather than embracing either naive faith in democratic institutions or Thiel&#39;s oversimplified critique, we must acknowledge both democracy&#39;s advantages over historical alternatives and its inherent limitations. The challenge is building systems that harness democracy&#39;s benefits while remaining clear-eyed about its constraints instead of falling into all-or-nothing thinking.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s obsession with &amp;quot;The West&amp;quot; reveals a troubling parochialism at the heart of his supposedly world-historical philosophy. His narrow focus on Western civilization betrays a profound disinterest in, and perhaps even disdain for, the rest of humanity&#39;s cultural and intellectual traditions. This is not merely about ignoring non-Western thought (though he certainly does that); rather, it reflects a deeper provinciality that sees only the West as worthy of serious consideration. His grand narratives about civilization and progress operate as if the vast majority of human experience and achievement simply does not exist or matter. This myopic Western-centrism fatally undermines his claims to universal insight, revealing instead a philosophy that is fundamentally local and limited in scope, incapable of meaningfully engaging with our interconnected global reality. What presents itself as a comprehensive worldview is in fact deeply provincial, betraying an intellectual and perhaps cultural insularity that sits uneasily with Thiel&#39;s self-positioning as a profound analyst of human civilization. And this is the most generous interpretation; there is a much simpler reading that needs no further elaboration.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s rejection of humanism, coupled with his instrumental interpretation of Christianity, might suggest a profound insecurity regarding the potential absence of a grand, transcendent purpose. He perhaps exhibits a strong need for a comforting narrative to reassure himself that human experience is ordered, a need that reveals his inability to grapple with the potential for a world that may be intrinsically aimless and chaotic. This appears less an embrace of faith and more a flight from unsettling existential realities into constructed fantastical narratives.&lt;/p&gt;
&lt;p&gt;This fantastical worldview, which casts existence as an epic battle between order and chaos where humanity perpetually teeters on the precipice of self-destruction, may reveal less about external reality and more about an interior landscape shaped by resentment. As Napoleon observed, &amp;quot;To understand a man, you must know what was happening in the world when he was twenty,&amp;quot; and for Thiel, that world was Stanford University. It is plausible that his intellectual framework crystallized during these formative years, when his conservative ideas faced rejection in the academy. This experience as a self-described &amp;quot;rebellious undergrad&amp;quot; who was reportedly a target of &amp;quot;bullying and cruelty&amp;quot; may have been the defining event, forging a profound and lasting hostility.&lt;/p&gt;
&lt;p&gt;According to this interpretation, his animus toward &amp;quot;political correctness&amp;quot; stems from a simple, personal source: the belief that liberals were merely &amp;quot;people who were not nice to him.&amp;quot; This experience appears to have profoundly impacted and even become his worldview, establishing a personal friend/enemy distinction long before he found the philosophical frameworks to elevate it. The intellectual figures he discovered, such as Schmitt, Strauss, Spengler, and Girard, subsequently provided validating blueprints to rationalize this pre-existing animosity. This suggests a powerful form of prejudice seeking intellectual alignment, where he weaponized high-minded theory to recast his personal antagonists as &amp;quot;the enemy&amp;quot; of civilization.&lt;/p&gt;
&lt;p&gt;This deep-seated need for an adversary makes the specific target, whether liberal academia, Islam, or China, less crucial than the fact that there is a target at all. Echoing René Girard’s observation that the motivations for accelerating political movements are often &amp;quot;not far from resentment,&amp;quot; Thiel appears to rely on conflict to &amp;quot;galvanize purpose, justify action, and impose meaning.&amp;quot; This resentment becomes an intellectual stumbling block, leading him to apply Girard&#39;s ideas selectively. He fixates on the left&#39;s perceived abuses of victimhood while remaining blind to his own allies&#39; use of scapegoating, skewing his application of Girardian principles to serve his political goals. Ultimately, this foundational animosity helps explain his dismissal of caution and incrementalism in favor of the radical, fatalistic visions of thinkers like Spengler. This psychological engine transforms personal rejection into a grandiose philosophical framework. In doing so, he recasts his marginalization as civilization&#39;s battle against chaos and elevates himself from a resentful contrarian to a self-described prophetic defender of the West.&lt;/p&gt;
&lt;p&gt;If you heard someone on a street corner shouting &amp;quot;the Antichrist is fueling runaway mimesis through victimism while the katechon weakens, establishing the One World Government instead of letting us build the City of God with definite optimism,&amp;quot; you would likely cross to the other side of the street to avoid the ranting of a disturbed individual. Yet when these same apocalyptic ramblings come from a billionaire, they are somehow treated as profound insights worthy of serious intellectual consideration. So, the apocalypse is nigh—yet again—just as it has been for every doomsday prophet throughout history.&lt;/p&gt;
&lt;p&gt;Ultimately, Thiel&#39;s philosophical edifice reveals less a profound intellectual engagement with reality or the human condition and more a desperate, even arguably pathetic, attempt to evade the complexities and ambiguities of the real world. He retreats into a hyper-intellectualized realm of grand narratives, mythical archetypes, and rigid dualisms, a world where he can cast himself as a Tolkienian heroic figure battling against the forces of darkness, while conveniently ignoring the messy, contingent, and often contradictory realities of human existence.&lt;/p&gt;
&lt;p&gt;But this is not Middle Earth, and these are not harmless philosophical musings, Thiel&#39;s backing of Donald Trump can be directly traced to his apparent desire for disruptive, chaos agents capable of shattering perceived societal sclerosis and shaking up entrenched establishments. However, this embrace of disruptive force courts a profound tension: while Thiel spins elaborate anxieties about runaway technology or mimesis leading to apocalypse, his support for a volatile imbecile as president arguably places far more tangible apocalyptic power into hands demonstrably ill-equipped for such responsibility, a far more direct and less fantastical route to armageddon enabled by Thiel&#39;s own actions and philosophy.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s philosophical framework, though deeply complicated, is ultimately completely incoherent. It falls into the same trap that ensnared Sauron himself, who in Tolkein&#39;s legendarium began as Mairon, &amp;quot;the Admirable,&amp;quot; a spirit who sought to order all things according to his own wisdom. Like Sauron, who believed his vision would create a more efficient and orderly world, Thiel&#39;s worldview reveals a man so convinced of his heroic role in history&#39;s grand narrative that he fails to see how his actions and beliefs might cast him on the side of darkness.&lt;/p&gt;
&lt;p&gt;Thiel&#39;s philosophy is not a product of intellectual rigor or profound insight but rather a symptom of an arrested development, a retreat into a fantasy world where he can play the role of a hero and where he is not forced to grapple with the messy realities of human experience. His worldview, like an elaborate make-believe game, is ultimately incoherent, and profoundly out of touch with the actualities of human existence. It is not a philosophy for adults, but rather like the musings of a resentful child who clearly struggles to grapple with the complexities of modernity, and who has therefore permanently retreated into a realm of fantasy.&lt;/p&gt;
&lt;h2 id=&quot;primary-sources&quot; tabindex=&quot;-1&quot;&gt;Primary Sources&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Note: I don&#39;t endorse these sources, they&#39;re provided for context and research purposes.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;books&quot; tabindex=&quot;-1&quot;&gt;Books&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Thiel, P., &amp;amp; Masters, B. (2014). &lt;em&gt;Zero to one: Notes on startups, or how to build the future&lt;/em&gt;. Crown Business.&lt;/li&gt;
&lt;li&gt;Thiel, P., &amp;amp; Sacks, D. O. (1995). &lt;em&gt;The diversity myth: &#39;Multiculturalism&#39; and the politics of intolerance at Stanford&lt;/em&gt;. The Independent Institute.&lt;/li&gt;
&lt;li&gt;Davidson, J. D., &amp;amp; Rees-Mogg, L. W. (2020). &lt;em&gt;The sovereign individual: Mastering the transition to the information age.&lt;/em&gt; Simon and Schuster.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;essays-and-academic-papers&quot; tabindex=&quot;-1&quot;&gt;Essays and Academic Papers&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Thiel, P. (2009, April 13). &lt;a href=&quot;https://www.cato-unbound.org/2009/04/13/peter-thiel/education-libertarian/&quot;&gt;&lt;em&gt;The education of a libertarian&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Cato Unbound&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2008, January). &lt;a href=&quot;https://www.hoover.org/research/optimistic-thought-experiment&quot;&gt;&lt;em&gt;The optimistic thought experiment&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Policy Review&lt;/em&gt;, 150.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2007, November). &lt;a href=&quot;https://www.firstthings.com/article/2007/11/spending-the-future&quot;&gt;&lt;em&gt;Spending The Future&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;First Things&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2007, January). &lt;a href=&quot;https://gwern.net/doc/politics/2007-thiel.pdf&quot;&gt;&lt;em&gt;The Straussian Moment&lt;/em&gt;&lt;/a&gt;. Michigan State University Press.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;op-eds-and-articles&quot; tabindex=&quot;-1&quot;&gt;Op-Eds and Articles&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Thiel, P. (2025, January 10). &lt;a href=&quot;https://www.ft.com/content/a46cb128-1f74-4621-ab0b-242a76583105&quot;&gt;&lt;em&gt;A time for truth and reconciliation&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Financial Times&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2020, March). &lt;a href=&quot;https://www.firstthings.com/article/2020/03/back-to-the-future&quot;&gt;&lt;em&gt;Back To The Future&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;First Things&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2019, August 1). &lt;a href=&quot;https://www.nytimes.com/2019/08/01/opinion/peter-thiel-google.html&quot;&gt;&lt;em&gt;Good for Google, Bad for America&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;The New York Times&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2016, November 1). &lt;a href=&quot;https://www.washingtonpost.com/opinions/peter-thiel-trump-has-taught-us-this-years-most-important-political-lesson/2016/09/06/84df8182-738c-11e6-8149-b8d05321db62_story.html&quot;&gt;&lt;em&gt;Trump has taught us this year&#39;s most important political lesson&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;The Washington Post&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2016, August 16). &lt;a href=&quot;https://www.nytimes.com/2016/08/16/opinion/peter-thiel-the-online-privacy-debate-wont-end-with-gawker.html&quot;&gt;&lt;em&gt;The Online Privacy Debate Won’t End With Gawker&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;The New York Times&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2015, November 28). &lt;a href=&quot;https://www.nytimes.com/2015/11/28/opinion/the-new-atomic-age-we-need.html?curator=MediaREDEF&quot;&gt;&lt;em&gt;The New Atomic Age We Need&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;The New York Times&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2015, June). &lt;a href=&quot;https://www.firstthings.com/article/2015/06/against-edenism&quot;&gt;&lt;em&gt;Against Edenism&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;First Things&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2015, January). &lt;a href=&quot;https://www.independent.org/events/transcript.asp?id=165&quot;&gt;&lt;em&gt;Developing the Developed World (Transcript)&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Independent Institute&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2014, November). &lt;a href=&quot;https://www.tampabay.com/opinion/thinking-too-highly-of-higher-education/2207911/&quot;&gt;&lt;em&gt;Thinking too highly of higher ed&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Tampa Bay Times&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2014, September). &lt;a href=&quot;https://www.wired.com/2014/09/run-startup-like-cult-heres/?curator=MediaREDEF&quot;&gt;&lt;em&gt;You Should Run Your Startup Like a Cult. Here’s How&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;Wired&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2014, September 12). &lt;a href=&quot;https://www.wsj.com/articles/peter-thiel-competition-is-for-losers-1410535536&quot;&gt;&lt;em&gt;Competition Is for Losers&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;The Wall Street Journal&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2011, October 3). &lt;a href=&quot;https://www.nationalreview.com/2011/10/end-future-peter-thiel/&quot;&gt;&lt;em&gt;The End of the Future&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;National Review&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2011, September). &lt;a href=&quot;https://nationalreview.com/magazine/2011/10/03/swift-blind-horseman&quot;&gt;&lt;em&gt;Swift Blind Horseman?&lt;/em&gt;&lt;/a&gt;. &lt;em&gt;National Review&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Thiel, P. (2010, April 13). &lt;a href=&quot;https://www.nytimes.com/2015/11/28/opinion/the-new-atomic-age-we-need.html&quot;&gt;&lt;em&gt;The new atomic age we need.&lt;/em&gt;&lt;/a&gt; &lt;em&gt;The New York Times&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;secondary-sources&quot; tabindex=&quot;-1&quot;&gt;Secondary Sources&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Books&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Chafkin, M. (2021). The Contrarian: Peter Thiel and Silicon Valley&#39;s Pursuit of Power. Bloomsbury Publishing.&lt;/li&gt;
&lt;li&gt;Taplin, J. (2023). The End of Reality: How four billionaires are selling out our future. Random House.&lt;/li&gt;
&lt;li&gt;Rushkoff D. (2022). Survival of the richest: Escape fantasies of the tech billionaires. WW Norton &amp;amp; Company.&lt;/li&gt;
&lt;li&gt;Soni, J. (2022). The founders: The story of paypal and the entrepreneurs who shaped silicon valley. Simon and Schuster.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Web&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://recommentions.com/peter-thiel/books/&quot;&gt;35 books Peter Thiel mentioned, ranked!&lt;/a&gt; (&lt;a href=&quot;http://recommentions.com/&quot;&gt;recommentions.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://yaledailynews.com/blog/2013/04/16/billionaire-investor-predicts-bleak-future-for-innovation/&quot;&gt;Billionaire investor predicts bleak future for innovation&lt;/a&gt; (&lt;a href=&quot;http://yaledailynews.com/&quot;&gt;yaledailynews.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.vanityfair.com/news/story/christianity-was-borderline-illegal-in-silicon-valley-now-its-the-new-religion&quot;&gt;Christianity Was “Borderline Illegal” in Silicon Valley. Now It’s the New Religion&lt;/a&gt; (&lt;a href=&quot;http://vanityfair.com/&quot;&gt;vanityfair.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.thenewatlantis.com/publications/competing-to-conform&quot;&gt;Competing to conform&lt;/a&gt; (&lt;a href=&quot;http://thenewatlantis.com/&quot;&gt;thenewatlantis.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://thebaffler.com/latest/graeber-thiel&quot;&gt;David Graeber vs. Peter Thiel: Where Did the Future Go?&lt;/a&gt; (&lt;a href=&quot;http://thebaffler.com/&quot;&gt;thebaffler.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://foundersfund.com/the-future/&quot;&gt;Founders Fund—What happened to the future&lt;/a&gt; (&lt;a href=&quot;http://foundersfund.com/&quot;&gt;foundersfund.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://salmagundi.skidmore.edu/articles/1176-from-philosophy-to-power&quot;&gt;From Philosophy to Power: The Misuse of René Girard by Peter Thiel, J.D. Vance and the American Right&lt;/a&gt; (&lt;a href=&quot;http://salmagundi.skidmore.edu/&quot;&gt;salmagundi.skidmore.edu&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.taylorfrancis.com/chapters/edit/10.4324/9781003436737-16/hard-men-hard-money-hardening-right-josh-vandiver&quot;&gt;Hard Men, Hard Money, Hardening Right&lt;/a&gt; (&lt;a href=&quot;http://taylorfrancis.com/&quot;&gt;taylorfrancis.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.businessinsider.com/peter-thiel-on-rene-girards-influence-2014-11&quot;&gt;How an esoteric philosophy book shapes his worldview&lt;/a&gt; (&lt;a href=&quot;http://businessinsider.com/&quot;&gt;businessinsider.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.motherjones.com/politics/2021/11/how-dangerous-is-peter-thiel/&quot;&gt;How Dangerous Is Peter Thiel?&lt;/a&gt; (&lt;a href=&quot;http://motherjones.com/&quot;&gt;motherjones.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vtmxGPP6BN4&quot;&gt;How Peter Thiel Mentored JD Vance and Turned Silicon Valley Towards Trumpism&lt;/a&gt; (&lt;a href=&quot;http://youtube.com/New&quot;&gt;youtube.com/New&lt;/a&gt; York Magazine)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.vanityfair.com/news/2022/04/inside-the-new-right-where-peter-thiel-is-placing-his-biggest-bets?srsltid=AfmBOor-Uz1jLB1DEtNCiOxPgAvq18DSgzv0BddtrAh9m9sunsJCtlIH&quot;&gt;Inside the New Right: Where Peter Thiel Is Placing His Biggest Bets&lt;/a&gt; (&lt;a href=&quot;http://vanityfair.com/&quot;&gt;vanityfair.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://washingtonstand.com/commentary/is-woke-religion-an-extreme-form-of-christianity&quot;&gt;Is woke religion an extreme form of Christianity?&lt;/a&gt; (&lt;a href=&quot;http://washingtonstand.com/&quot;&gt;washingtonstand.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.newstatesman.com/ideas/2024/01/john-gray-peter-thiel-discussion-post-modern-world&quot;&gt;John Gray and Peter Thiel: Life in a postmodern world&lt;/a&gt; (&lt;a href=&quot;http://newstatesman.com/&quot;&gt;newstatesman.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.theatlantic.com/ideas/archive/2025/01/peter-thiel-maga-conspiracism/681310/&quot;&gt;MAGA&#39;s Demon-Haunted World&lt;/a&gt; (&lt;a href=&quot;http://theatlantic.com/&quot;&gt;theatlantic.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.52-insights.com/max-chafkin-on-the-dark-genius-of-peter-thiel-book-interview-silicon-valley/&quot;&gt;Max Chafkin on the Dark Genius of Peter Thiel&lt;/a&gt; (&lt;a href=&quot;http://52-insights.com/&quot;&gt;52-insights.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://sancrucensis.wordpress.com/2019/02/03/max-webers-critique-of-marx/&quot;&gt;Max Weber&#39;s critique of Marx&lt;/a&gt; (&lt;a href=&quot;http://sancrucensis.wordpress.com/&quot;&gt;sancrucensis.wordpress.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://thesocietypages.org/cyborgology/2016/08/13/mimesis-violence-and-facebook-peter-thiels-french-connection-full-essay/&quot;&gt;Mimesis, Violence, and Facebook&lt;/a&gt; (&lt;a href=&quot;http://thesocietypages.org/&quot;&gt;thesocietypages.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.newyorker.com/magazine/2019/10/14/nietzsches-eternal-return&quot;&gt;Nietzsche&#39;s eternal return&lt;/a&gt; (&lt;a href=&quot;http://newyorker.com/&quot;&gt;newyorker.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://americanaffairsjournal.org/2024/11/nietzsches-eternal-return-in-america/&quot;&gt;Nietzsche&#39;s eternal return in America&lt;/a&gt; (&lt;a href=&quot;http://americanaffairsjournal.org/&quot;&gt;americanaffairsjournal.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.nihilismisnotenough.com/&quot;&gt;Nihilism is Not Enough&lt;/a&gt; (&lt;a href=&quot;http://nihilismisnotenough.com/&quot;&gt;nihilismisnotenough.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.philosophyforlife.org/blog/on-peter-thiel-radical-life-extension-and-the-state&quot;&gt;On Peter Thiel, radical life extension, and the state&lt;/a&gt; (&lt;a href=&quot;http://philosophyforlife.org/&quot;&gt;philosophyforlife.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://acireland.ie/on-the-dangers-of-a-return-to-constantinianism/&quot;&gt;On the dangers of a return to constantinianism&lt;/a&gt; (&lt;a href=&quot;http://acireland.ie/&quot;&gt;acireland.ie&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.ft.com/content/8adeca00-2996-11e2-a5ca-00144feabdc0&quot;&gt;Our dangerous illusion of tech progress&lt;/a&gt; (&lt;a href=&quot;http://ft.com/&quot;&gt;ft.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.theatlantic.com/politics/archive/2023/11/peter-thiel-2024-election-politics-investing-life-views/675946/&quot;&gt;Peter Thiel Is Taking a Break From Democracy&lt;/a&gt; (&lt;a href=&quot;http://theatlantic.com/&quot;&gt;theatlantic.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mimetictheory.com/peter-thiel/&quot;&gt;Peter Thiel on mimetic theory&lt;/a&gt; (&lt;a href=&quot;http://mimetictheory.com/&quot;&gt;mimetictheory.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.newsweek.com/peter-thiel-pre-nazi-germany-us-comparison-resurfaces-1934760&quot;&gt;Peter Thiel on pre-Nazi Germany and the US comparison resurfaces&lt;/a&gt; (&lt;a href=&quot;http://newsweek.com/&quot;&gt;newsweek.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://read.lukeburgis.com/p/peter-thiel-on-rene-girard&quot;&gt;Peter Thiel on Rene Girard&lt;/a&gt; (&lt;a href=&quot;http://read.lukeburgis.com/&quot;&gt;read.lukeburgis.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.businessinsider.com/peter-thiel-on-rene-girards-influence-2014-11&quot;&gt;Peter Thiel on Rene Girard&#39;s influence&lt;/a&gt; (&lt;a href=&quot;http://businessinsider.com/&quot;&gt;businessinsider.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.vanityfair.com/news/2016/08/peter-thiel-wants-to-inject-himself-with-young-peoples-blood&quot;&gt;Peter Thiel Wants to Inject Himself with Young People&#39;s Blood&lt;/a&gt; (&lt;a href=&quot;http://vanityfair.com/&quot;&gt;vanityfair.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.rollingstone.com/politics/politics-features/owned-book-peter-thiel-trump-tech-silicon-valley-1235276868/&quot;&gt;Peter Thiel, Trump&#39;s Man in Silicon Valley&lt;/a&gt; (&lt;a href=&quot;http://rollingstone.com/&quot;&gt;rollingstone.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qz.com/work/1485668/this-is-peter-thiels-favorite-interview-question/&quot;&gt;Peter Thiel&#39;s Favorite Interview Question&lt;/a&gt; (&lt;a href=&quot;http://qz.com/&quot;&gt;qz.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.disconnect.blog/p/peter-thiels-influence-over-a-network-of-lord-of-the-rings-inspired-companies&quot;&gt;Peter Thiel&#39;s influence over a network of Lord of the Rings inspired companies&lt;/a&gt; (disconnect.blog)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://francinemckenna.com/2016/07/25/peter-thiels-pursuit-of-technological-progress-its-not-about-democracy-and-its-definitely-not-about-capitalism-part-1/&quot;&gt;Peter Thiel&#39;s pursuit of technological progress: It&#39;s not about democracy and it&#39;s definitely not about capitalism (part 1)&lt;/a&gt; (&lt;a href=&quot;http://francinemckenna.com/&quot;&gt;francinemckenna.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://francinemckenna.com/2016/07/25/peter-thiels-pursuit-of-technological-progress-its-not-about-democracy-and-its-definitely-not-about-capitalism-part-2/&quot;&gt;Peter Thiel&#39;s pursuit of technological progress: It&#39;s not about democracy and it&#39;s definitely not about capitalism (part 2)&lt;/a&gt; (&lt;a href=&quot;http://francinemckenna.com/&quot;&gt;francinemckenna.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://perell.com/essay/peter-thiel/&quot;&gt;Peter Thiel&#39;s Religion&lt;/a&gt; (&lt;a href=&quot;http://perell.com/&quot;&gt;perell.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.thenerdreich.com/peter-thiel-religion-apocalypse-dreams/&quot;&gt;Peter Thiel&#39;s Religion: Apocalypse Dreams&lt;/a&gt; (&lt;a href=&quot;http://thenerdreich.com/&quot;&gt;thenerdreich.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://legacy.ghi-dc.org/fileadmin/user_upload/GHI_Washington/Publications/Supplements/Supplement_12/333.pdf&quot;&gt;Political Ideology And Economic Activity&lt;/a&gt; (&lt;a href=&quot;http://legacy.ghi-dc.org/&quot;&gt;legacy.ghi-dc.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.politico.com/magazine/story/2016/11/donald-trump-steve-bannon-peter-thiel-214490/&quot;&gt;The Anti-Democratic Worldview of Steve Bannon and Peter Thiel&lt;/a&gt; (&lt;a href=&quot;http://politico.com/&quot;&gt;politico.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.politico.com/news/magazine/2021/09/20/peter-thiel-book-facebook-trump-jd-vance-blake-masters-josh-hawley-513121&quot;&gt;The Black Box of Peter Thiel&#39;s Beliefs&lt;/a&gt; (&lt;a href=&quot;http://politico.com/&quot;&gt;politico.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.uibk.ac.at/theol/cover/bulletin/archive/bulletin_46.pdf&quot;&gt;The Bulletin of the Colloquium on Violence &amp;amp; Religion&lt;/a&gt; (&lt;a href=&quot;http://uibk.ac.at/&quot;&gt;uibk.ac.at&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.technologyreview.com/2014/10/08/111242/the-contrarians-guide-to-changing-the-world/&quot;&gt;The Contrarians Guide to Changing the World&lt;/a&gt; (&lt;a href=&quot;http://technologyreview.com/&quot;&gt;technologyreview.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://europeanconservative.com/articles/essay/the-end-of-the-straussian-moment/&quot;&gt;The end of the Straussian moment&lt;/a&gt; (&lt;a href=&quot;http://europeanconservative.com/&quot;&gt;europeanconservative.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.lrb.co.uk/the-paper/v41/n04/peter-thiel/the-end-of-the-straussian-moment&quot;&gt;The End of the Straussian moment: Review&lt;/a&gt; (&lt;a href=&quot;http://lrb.co.uk/&quot;&gt;lrb.co.uk&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.unpopularfront.news/p/the-enigma-of-peter-thiel&quot;&gt;The enigma of Peter Thiel&lt;/a&gt; (unpopularfront.news)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.city-journal.org/peter-thiel&quot;&gt;The Gospel According to Peter Thiel&lt;/a&gt; (&lt;a href=&quot;http://city-journal.org/&quot;&gt;city-journal.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://link.springer.com/chapter/10.1007/978-3-031-84786-8_5&quot;&gt;The Rage of the Elite&lt;/a&gt; (&lt;a href=&quot;http://link.springer.com/&quot;&gt;link.springer.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.newyorker.com/news/annals-of-populism/the-rise-of-the-thielists&quot;&gt;The Rise of the Thielists&lt;/a&gt; (&lt;a href=&quot;http://newyorker.com/&quot;&gt;newyorker.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://thenewinquiry.com/the-scapegoating-machine/&quot;&gt;The scapegoating machine&lt;/a&gt; (&lt;a href=&quot;http://thenewinquiry.com/&quot;&gt;thenewinquiry.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://wisdomofcrowds.live/p/the-temptation-of-peter-thiel&quot;&gt;The temptation of Peter Thiel&lt;/a&gt; (wisdomofcrowds.live)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://theweek.com/articles/443724/peter-thiel-notsosecret-secret-innovative-success&quot;&gt;The Week—Secret to Innovation&lt;/a&gt; (&lt;a href=&quot;http://theweek.com/&quot;&gt;theweek.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.unpopularfront.news/p/thiel-schmitt-and-spengler&quot;&gt;Thiel and Schmitt&lt;/a&gt; (unpopularfront.news)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.hoover.org/research/apocalypse-now-peter-thiel-ancient-prophecies-and-modern-tech&quot;&gt;Uncommon Knowledge: Peter Thiel on the end times and the katechon&lt;/a&gt; (&lt;a href=&quot;http://hoover.org/&quot;&gt;hoover.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://reason.com/2020/08/02/wait-wasnt-peter-thiel-a-libertarian/&quot;&gt;Wait wasn&#39;t Peter Thiel a libertarian?&lt;/a&gt; (&lt;a href=&quot;http://reason.com/&quot;&gt;reason.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://theconversation.com/whatsapp-even-private-chatter-now-exploited-by-billionaires-23550&quot;&gt;WhatsApp? Even private chatter now exploited by billionaires&lt;/a&gt; (&lt;a href=&quot;http://theconversation.com/&quot;&gt;theconversation.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://damonlinker.substack.com/p/what-does-peter-thiel-want&quot;&gt;What does Peter Thiel want?&lt;/a&gt; (&lt;a href=&quot;http://damonlinker.substack.com/&quot;&gt;damonlinker.substack.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.salon.com/2023/03/27/what-does-peter-thiel-want-hes-building-the-right-wing-future-piece-by-piece/&quot;&gt;What Does Peter Thiel Want? He&#39;s Building the Right-Wing Future, Piece by Piece&lt;/a&gt; (&lt;a href=&quot;http://salon.com/&quot;&gt;salon.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.newyorker.com/news/letter-from-silicon-valley/what-is-it-about-peter-thiel&quot;&gt;What is it about Peter Thiel?&lt;/a&gt; (&lt;a href=&quot;http://newyorker.com/&quot;&gt;newyorker.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.inc.com/quora/what-you-can-learn-from-peter-thiels-brilliant-philosophy-on-setting-personal-goals.html&quot;&gt;What You Can Learn From Peter Thiel&#39;s Brilliant Philosophy on Setting Personal Goals&lt;/a&gt; (&lt;a href=&quot;http://inc.com/&quot;&gt;inc.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.wbur.org/onpoint/2014/09/30/transcript-peter-thiel-wants-us-all-to-go-from-zero-to-one&quot;&gt;WBUR News—Peter Thiel Wants Us All To Go From Zero To One&lt;/a&gt; (&lt;a href=&quot;http://wbur.org/&quot;&gt;wbur.org&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://som.yale.edu/blog/peter-thiel-at-yale-we-wanted-flying-cars-instead-we-got-140-characters&quot;&gt;Yale School of Management&lt;/a&gt; (&lt;a href=&quot;http://som.yale.edu/&quot;&gt;som.yale.edu&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.liberalcurrents.com/the-far-right-canon/&quot;&gt;The Far Right Canon&lt;/a&gt; (&lt;a href=&quot;http://liberalcurrents.com/&quot;&gt;liberalcurrents.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.nytimes.com/2025/05/23/books/tolkien-musk-thiel-silicon-valley.html&quot;&gt;Why Silicon Valley’s Most Powerful People Are So Obsessed With Hobbits&lt;/a&gt; (&lt;a href=&quot;http://nytimes.com/&quot;&gt;nytimes.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.crisconsortium.org/blog/alternative-epistemologies-of-the-radical-right-how-grand-narratives-and-the-quest-for-truth-offer-recognition-and-a-sense-of-belonging&quot;&gt;Alternative epistemologies of the radical right&lt;/a&gt; (&lt;a href=&quot;http://crisconsortium.org/&quot;&gt;crisconsortium.org&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;talks-and-interviews&quot; tabindex=&quot;-1&quot;&gt;Talks and Interviews&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=918qslcfwfY&quot;&gt;Why We Stopped Progressing | Peter Thiel&lt;/a&gt; (Jordan B Peterson | Apr 2025)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=RCsW1snWed8&quot;&gt;Peter Thiel on Trump’s 125% Tariffs: How to Stop China&lt;/a&gt; (Joe Lonsdale | Apr 2025)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=CKLSwhxjZUc&quot;&gt;What the Trump Administration Must Do Instead of Revenge&lt;/a&gt; (The Rubin Report | Mar 2025)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=wTNI_lCvWZQ&quot;&gt;Part II: Apocalypse Now? Peter Thiel on Ancient Prophecies and Modern Tech&lt;/a&gt; (Hoover Institution | Dec 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=qqHueZNEzig&amp;amp;t=1924s&quot;&gt;Apocalypse Now? Peter Thiel on Ancient Prophecies and Modern Tech&lt;/a&gt; (Hoover Institution | Nov 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=wwJV_NuN43Y&quot;&gt;Peter Thiel on the Triumph of the Counter-Elites&lt;/a&gt; (The Free Press | November 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=SYRunzR9fbk&quot;&gt;Peter Thiel | All-In Summit 2024&lt;/a&gt; (All-In Podcast | Sep 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=klRb0_BAX9g&quot;&gt;Joe Rogan Experience #2190 - Peter Thiel&lt;/a&gt; (The Joe Rogan Experience | Aug 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=42iVcEg5SOM&quot;&gt;&amp;quot;The Real Risk is Totalitarian World Government&amp;quot; - Peter Thiel&lt;/a&gt; (Triggernometry | Jul 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=bNewfkhhwMo&quot;&gt;Peter Thiel | Cambridge Union&lt;/a&gt; (Cambridge Union | Jul 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=B3ZXrTzskw0&quot;&gt;The Iconoclast Peter Thiel&lt;/a&gt; (The Aspen Institute | Jun 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vfbndRTlsg4&quot;&gt;Peter Thiel on Political Theology&lt;/a&gt; (Conversations with Tyler | Apr 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=euzxpbBOju8&quot;&gt;Peter Thiel - Keynote Address | The Conservative and Republican Student Conference&lt;/a&gt; (The Harvard Salient | Feb 2024)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p8X9kdL9oIk&quot;&gt;Peter Thiel On The Diversity Myth, Corrupt Institutions, Woke Capital, Loss Of Religion, &amp;amp; China&lt;/a&gt; (Pirate Wires | Nov 2023)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=iF2la6FxVQc&quot;&gt;Peter Thiel: The Stagnation of Science and the AI Revolution&lt;/a&gt; (World of DaaS with Auren Hoffman | Oct 2023)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=fQ4rc7npiXQ&quot;&gt;Peter Thiel on &#39;Anti-Anti-Anti-Anti Classical Liberalism&#39;&lt;/a&gt; (OxfordUnion | Jan 2023)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=OWXFdEyOKc4&quot;&gt;Peter Thiel, Leader of the Rebel Alliance&lt;/a&gt; (Hoover Institution | Nov 2022)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=balGGAd6ZrI&quot;&gt;Peter Thiel | The Tech Curse | NatCon 3 Miami&lt;/a&gt; (National Conservatism | Sep 2022)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Bw1ByVhJt7A&quot;&gt;Peter Thiel | Nationalism Breaks the Dogma Machine&lt;/a&gt; (National Conservatism | Nov 2021)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=fDB1CsfUjys&quot;&gt;A Conversation with Peter Thiel: Culture, Religion, and Technology&lt;/a&gt; (Foundation for American Innovation | Oct 2021)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=SO_00POR-Po&quot;&gt;Peter Thiel: Zero to One&lt;/a&gt; (Socrates in the City | Feb 2020)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=IXG2F0a6I28&quot;&gt;The World According to Thiel&lt;/a&gt; (Hoover Institution | Jan 2020)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=iRleB034EC8&quot;&gt;Peter Thiel on “The Straussian Moment”&lt;/a&gt; (Hoover Institution | Sep 2019)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.manhattan-institute.org/events/2019-wriston-lecture-end-computer-age-thiel&quot;&gt;Manhattan Institute—2019 Wriston Lecture: Peter Thiel&lt;/a&gt; (Manhattan Institute | 2019)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=nM9f0W2KD5s&quot;&gt;Peter Thiel on &amp;quot;The Portal&amp;quot;, Episode #001: &amp;quot;An Era of Stagnation &amp;amp; Universal Institutional Failure.&amp;quot;&lt;/a&gt; (Eric Weinstein | Jul 2019)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=qhY5IlvxbE0&quot;&gt;a16z Podcast | The (Definite) Optimism of Peter Thiel&lt;/a&gt; (a16z | Jan 2019)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=FDkhfm6CYjE&quot;&gt;Peter Thiel Speaks at Brain Bar&lt;/a&gt; (Brain Bar | Jul 2018)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://tim.blog/wp-content/uploads/2018/07/28-peter-thiel.pdf&quot;&gt;Tim Ferris Show—Episode 28 Peter Thiel&lt;/a&gt; (Tim Ferris Show | July 2018)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=J2klGJRrjqw&quot;&gt;Cardinal Conversations: Reid Hoffman and Peter Thiel on &amp;quot;Technology and Politics&amp;quot;&lt;/a&gt; (Hoover Institution | Feb 2018)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3Fx5Q8xGU8k&quot;&gt;Competition is for Losers with Peter Thiel&lt;/a&gt; (Y Combinator | Mar 2017)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=h10kXgTdhNU&quot;&gt;Youtube (The Rubin Report)—Trump, Gawker, and Leaving Silicon Valley&lt;/a&gt; (The Rubin Report)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://time.com/4417679/republican-convention-peter-thiel-transcript/&quot;&gt;TIME—Republican National Committee Speech&lt;/a&gt; (TIME | July 2016)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=id4ywg5oemc&quot;&gt;Remarks by Commencement Speaker Peter Thiel&lt;/a&gt; (Hamilton College | May 2016)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Q_3r49XXRw4&quot;&gt;Peter Thiel on the Global Economy, the State of Our Technology, and Artificial Intelligence&lt;/a&gt; (Conversations with Bill Kristol | May 2016)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://conversationswithbillkristol.org/transcript/peter-thiel-ii-transcript/&quot;&gt;Conversations with Bill Kristol—Peter Thiel Transcript (innovation)&lt;/a&gt; (Conversations with Bill Kristol)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://conversationswithbillkristol.org/transcript/peter-thiel-transcript/&quot;&gt;Conversations with Bill Kristol—Peter Thiel Transcript (story of paypal)&lt;/a&gt; (Conversations with Bill Kristol)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=54NIcx3HJSE&quot;&gt;DealBook Conference 2015 - Venture Capital And Where It’s Going&lt;/a&gt; (New York Times Events | Nov 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Y11uX8X6iz4&quot;&gt;Peter Thiel Speaks at Center on Capitalism and Society’s 2015 conference&lt;/a&gt; (The Center on Capitalism and Society | Nov 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=kGiWNkK-apc&quot;&gt;Peter Thiel Presents: &amp;quot;Developing the Developed World&amp;quot;&lt;/a&gt; (The King’s College | Oct 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7pCIvOx76p0&quot;&gt;Peter Thiel Interview&lt;/a&gt; (Tim Ferriss | Oct 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=H5NUv0nOQCU&quot;&gt;Peter Thiel: We are in a Higher Education Bubble&lt;/a&gt; (The Aspen Institute | Jul 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=2YVHC-2vkMQ&quot;&gt;Imagining the Future, Innovation and God: N.T. Wright and Peter Thiel in San Francisco&lt;/a&gt; (The Veritas Forum | Jun 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=HfqrLJc089A&quot;&gt;PAYPAL MAFIA: Reid Hoffman &amp;amp; Peter Thiel&#39;s Master Class at CEIBS&lt;/a&gt; (China Europe International Business School | Jun 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=l5jMbJmkGrY&quot;&gt;Peter Thiel: PayPal, politics &amp;amp; the importance of being individual&lt;/a&gt; (Said Business School, University of Oxford | May 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=i_yJTCDU4uE&quot;&gt;Peter Thiel on Stagnation, Innovation, and What Not To Name Your Company | Conversations with Tyler&lt;/a&gt; (Mercatus Center | Apr 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ryFB6mvy4uE&quot;&gt;Peter Thiel on being a contrarian&lt;/a&gt; (This Week in Startups | Mar 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=mk0Do8oqy-8&quot;&gt;Anderson Speaker Series: Peter Thiel&lt;/a&gt; (UCLAAnderson | Feb 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=vtJ0C0UHFfE&quot;&gt;Zero to One: A Discussion with Peter Thiel&lt;/a&gt; (General Assembly | Feb 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=rFZrL1RiuVI&quot;&gt;Peter Thiel: Going from Zero to One&lt;/a&gt; (Chicago Ideas | Jan 2015)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=XJgwIgOLoyM&quot;&gt;e@nu Speaker Series with Peter Thiel: Developing The Developed World&lt;/a&gt; (Northwestern Engineering | Dec 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=RUMgK0TyV1Q&quot;&gt;From Zero to One - Peter Thiel at the Innovation Center at Cockrell School of Engineering&lt;/a&gt; (Emergent Order | Nov 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=QLdzeovQ5zw&quot;&gt;A New Way to Think About Startup Innovation&lt;/a&gt; (Knowledge at Wharton | Oct 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=JqxzLUE6pP8&quot;&gt;How To Build the Next Billion Dollar Startup&lt;/a&gt; (Forbes | Oct 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=3EJHMoh3Q1k&quot;&gt;Peter Thiel on markets, technology, and education&lt;/a&gt; (Hoover Institution | Oct 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6kGND-uZolY&quot;&gt;Peter Thiel Returns to Stanford to Share Business Tips from &amp;quot;Zero to One&amp;quot;&lt;/a&gt; (Stanford Law School | Oct 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Kl8JvF5id6Q&quot;&gt;Binary Truths with Peter Thiel | Disrupt SF 2014&lt;/a&gt; (TechCrunch | Sep 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=lC3ixCQjPIQ&quot;&gt;PayPal co-founder Peter Thiel on the future of technology&lt;/a&gt; (CBS Mornings | Sep 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=yODORwGmHqo&quot;&gt;Peter Thiel: Successful Businesses are Based on Secrets&lt;/a&gt; (Wired UK | Sep 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=N9Mlu7sHEHE&quot;&gt;What is the Hope for Humanity? A discussion of technology, politics, and theology&lt;/a&gt; (The Veritas Forum | May 2014)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=iZM_JmZdqCw&quot;&gt;Peter Thiel: You Are Not a Lottery Ticket&lt;/a&gt; (SXSW | Oct 2013)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=jX07zPupNdc&quot;&gt;Peter Thiel and Andy Kessler on the state of technology and innovation&lt;/a&gt; (Hoover Institution | Sep 2013)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=VtZbWnIALeE&quot;&gt;Copy of In Tech We Trust? A Debate with Peter Thiel and Marc Andreessen&lt;/a&gt; (Milken Institute | May 2013)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8KlXhrRxxbA&quot;&gt;Peter Thiel - Developing the Developed World&lt;/a&gt; (Credit Suisse | Mar 2013)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=UEmxRBpCoo0&quot;&gt;Peter Thiel and Charles Bolden on The World in 2050: What is the Next Big Idea?&lt;/a&gt; (World Affairs | Jan 2013)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p6taTMa4nls&quot;&gt;PandoMonthly: Fireside Chat With Peter Thiel&lt;/a&gt; (PandoDaily | Nov 2012)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=imLQdX_uIkU&quot;&gt;Big Think Interview With Peter Thiel&lt;/a&gt; (Big Think | Apr 2012)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=W3byZ5WfTzA&quot;&gt;Youtube (Big Think)—Keynesian Economics Will Be Dead&lt;/a&gt; (Big Think)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=exfbmY7mg8s&quot;&gt;A Conversation with Peter Thiel and Niall Ferguson&lt;/a&gt; (John F. Kennedy Jr. Forum | Feb 2012)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=PsXFwy6gG_4&quot;&gt;Eric Schmidt and Peter Thiel - Debate&lt;/a&gt; (Fortune Magazine | 2012)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8OhIWMQd0jA&quot;&gt;Max Levchin and Peter Thiel: Smart Venture Capital in 2011&lt;/a&gt; (The Commonwealth Club of California | Feb 2011)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=HOB7nezuQ7g&quot;&gt;TEDxSiliconValley - Peter Thiel - 12/12/09&lt;/a&gt; (TEDx Talks | Dec 2009)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=tu4VkOBq4NE&quot;&gt;The U.S. Economy with Peter Thiel&lt;/a&gt; (Hoover Institution | Dec 2008)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8tAOXH6Dzn0&quot;&gt;Youtube (Imitatio Videos)—Optimistic Thought Experiment&lt;/a&gt; (Imitatio Videos)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=esk7W9Jowtc&quot;&gt;Youtube (Imitatio Video)—Peter Thiel On Rene Girard&lt;/a&gt; (Imitatio Video)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=v_NhYV63K5E&quot;&gt;Fox Business—Peter Thiel on Leaving Silicon Valley for Los Angeles&lt;/a&gt; (Fox Business)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Ufm85wHJk5A&quot;&gt;Youtube (Center for Natural and Artificial Intelligence)—Peter Thiel on the Failures and &amp;quot;Self-Hatred&amp;quot; of Big-Tech&lt;/a&gt; (Center for Natural and Artificial Intelligence)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=9rOvEPYNEsc&quot;&gt;Into the night with Garry Kasparov and Peter Thiel&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mastersofscale.com/wp-content/uploads/2018/10/mos-episode-transcript_-peter-thiel.pdf&quot;&gt;Masters of Scale—Peter Thiel&lt;/a&gt; (Masters of Scale)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.lesswrong.com/posts/5EnpGNG5xPGSPyuQn/peter-thiel-eric-weinstein-transcript-on-growth-violence-and&quot;&gt;Lesswrong—Growth, Violence, Stories&lt;/a&gt; (Lesswrong)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.hoover.org/research/what-us-worry-united-states-losing-its-competitive-edge&quot;&gt;Hoover Institution—What, U.S. Worry? Is the United States Losing Its Competitive Edge?&lt;/a&gt; (Hoover Institution)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.nytimes.com/2025/06/26/opinion/peter-thiel-antichrist-ross-douthat.html&quot;&gt;Peter Thiel and the Antichrist&lt;/a&gt; (&lt;a href=&quot;http://nytimes.com/&quot;&gt;nytimes.com&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Wed, 01 Feb 2023 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/desconstructing_thiel/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/desconstructing_thiel/</guid>
    </item>
    
    <item>
      <title>Crypto is Financial Asbestos</title>
      <description>&lt;h1 id=&quot;crypto-is-financial-asbestos&quot; tabindex=&quot;-1&quot;&gt;Crypto is Financial Asbestos&lt;/h1&gt;
&lt;p&gt;Cryptocurrency is &lt;strong&gt;financial asbestos&lt;/strong&gt; a toxic material that&#39;s poisoning our financial system.&lt;/p&gt;
&lt;p&gt;Like its fibrous, fire-retardant namesake, crypto was initially hailed for seemingly miraculous properties: decentralization, censorship resistance, frictionless transactions. It promised to insulate users from the perceived flaws of traditional finance.&lt;/p&gt;
&lt;p&gt;Yet also like asbestos, its widespread adoption has unleashed a slow-burning, toxic crisis. It&#39;s inflicting devastating financial harm on individuals and posing a systemic threat that demands not tentative regulation, but decisive eradication. The parallels are disturbingly precise: a material initially embraced for utility, whose inherent dangers ultimately proved catastrophic, necessitating outright bans in sensible societies seeking to protect public health. Crypto&#39;s externalities are simply too venomous for mainstream tolerance.&lt;/p&gt;
&lt;p&gt;The supposed utility of cryptocurrency crumbles under scrutiny, much like asbestos insulation decades after installation. Its proponents waved the banner of innovation, promising liberation from banks and governments. In reality, it delivered a chaotic, unregulated casino prone to wild volatility, making it utterly useless as a medium of exchange or a stable store of value (the fundamental requirements of any legitimate currency). Bitcoin&#39;s violent price swings, characterized by thousand-percent surges followed by gut-wrenching 80% collapses, are not features of a new paradigm, but symptoms of a speculative mania built on narrative rather than substance. This isn&#39;t financial innovation; it&#39;s a high-tech game of musical chairs where the exit music is always abrupt and brutal, leaving legions of retail investors financially crippled. The promise of a better insulator was a lie; the promise of better finance is proving equally hollow.&lt;/p&gt;
&lt;p&gt;The human cost of this &amp;quot;financial asbestos&amp;quot; is appalling and mounting. Far from democratizing finance, crypto has proven remarkably effective at democratizing loss. Data consistently shows more investors losing money than profiting, a grim reality often fueled by borrowing, amplifying the eventual ruin. The staggering billions lost to crypto fraud annually, disproportionately siphoned from vulnerable populations like the elderly, paint a picture not of disruption, but of predation. These aren&#39;t sophisticated investors hedging bets; they are ordinary people lured by aggressive, often misleading marketing into a minefield, much like homeowners were once sold asbestos insulation without a word about the latent lethality lurking within their walls. The financial mesothelioma inflicted by crypto exposure is real, widespread, and often life-altering.&lt;/p&gt;
&lt;p&gt;Beyond individual tragedies, the crypto ecosystem exhibits a systemic fragility that mirrors the insidious, pervasive nature of asbestos contamination. The landscape is a graveyard of collapsed exchanges and platforms. Mt. Gox, Terra Luna, Celsius, Voyager, BlockFi, and the spectacular implosion of FTX stand as monuments to failure. These are not isolated accidents; they are the predictable outcomes of an industry riddled with incompetence, fraud, and a fundamental lack of the safeguards inherent in regulated finance. Regulators themselves explicitly warn of &amp;quot;contagion risk,&amp;quot; a danger vividly demonstrated when crypto failures like FTX triggered bank runs and collapses (Silvergate, Signature Bank), proving the toxic dust can indeed escape its containment and infect the broader financial structure. Like asbestos removal, cleaning up crypto&#39;s messes is proving extraordinarily costly and complex, with victims often waiting years, if not decades, for partial restitution, if any.&lt;/p&gt;
&lt;p&gt;The endemic fraud within crypto is not a peripheral issue; it is woven into its very fabric. The prevalence of pump and dumps and affinity fraud, where developers vanish with investor funds, has reached industrial scale, defrauding millions. The ICO boom was largely built on vaporware and empty promises, sucking in capital for projects with no viable technology. Furthermore, crypto&#39;s pseudo-anonymity and borderless nature have made it a favored conduit for money laundering, terrorist financing, and sanctions evasion by rogue states and criminal cartels. It has become the digital equivalent of unmarked bills in a Swiss briefcase, but with global reach and algorithmic obfuscation. This isn&#39;t a flaw to be patched; it&#39;s a core characteristic actively exploited by bad actors, making the entire ecosystem a public hazard.&lt;/p&gt;
&lt;p&gt;Therefore, the notion of merely regulating cryptocurrency is akin to suggesting we manage asbestos exposure by mandating slightly better air filters in contaminated buildings. It misses the fundamental point: the material itself is inherently dangerous. Regulation struggles to keep pace with crypto&#39;s shapeshifting nature and jurisdictional arbitrage. Consumer protection is minimal to non-existent in most jurisdictions, leaving investors exposed. Why tinker with regulatory frameworks for a system whose primary demonstrable use cases are speculation, fraud, and illicit finance, when robust, safer, and infinitely more efficient traditional financial rails (imperfect though they may be) already exist? We have better, safer insulators than asbestos; we have far better, safer financial infrastructure than crypto.&lt;/p&gt;
&lt;p&gt;The analogy to asbestos holds with chilling accuracy. Cryptocurrency, like asbestos, offered an initial allure based on novel properties. But the accumulating evidence of widespread financial ruin, systemic instability, pervasive criminality, and its fundamental unsuitability for legitimate financial purposes reveals its toxic nature. The externalities are too great, the harms too severe, the purported benefits too illusory or achievable through safer means. Just as societies eventually recognized the unacceptable long-term costs of asbestos and implemented bans to protect public health, a similar calculus applies to crypto&#39;s threat to financial health. The time for half-measures and regulatory whack-a-mole is over. Public policy should treat crypto as the financial asbestos it is and move towards a comprehensive prohibition to safeguard citizens and the integrity of our financial system.&lt;/p&gt;
</description>
      <pubDate>Fri, 23 Dec 2022 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/crypto_ban_asbethos/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/crypto_ban_asbethos/</guid>
    </item>
    
    <item>
      <title>Linking CUDA Kernels into Python</title>
      <description>&lt;h1 id=&quot;linking-cuda-kernels-into-python&quot; tabindex=&quot;-1&quot;&gt;Linking CUDA Kernels into Python&lt;/h1&gt;
&lt;p&gt;Gaussian blur is a common image processing operation that smooths images using a Gaussian kernel. While it&#39;s straightforward to implement on CPU, it can be quite slow for large images. Let&#39;s explore how to implement this efficiently using CUDA and then connect it to Python for easy use.&lt;/p&gt;
&lt;p&gt;First, install the CUDA toolkit which includes the NVIDIA CUDA compiler (nvcc):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Add NVIDIA package repositories&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;wget&lt;/span&gt; https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; dpkg &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; cuda-keyring_1.0-1_all.deb
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; update
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; build-essential

&lt;span class=&quot;token comment&quot;&gt;# Install CUDA toolkit&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; cuda-toolkit-12-x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify the installation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;nvcc &lt;span class=&quot;token parameter variable&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create a Python virtual environment and install the required packages:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; numpy matplotlib pillow pycuda
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To verify everything is working, run this simple test:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pycuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;autoinit
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pycuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;driver &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cuda
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;CUDA device name: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Device&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you see your GPU&#39;s name printed without errors, you&#39;re ready to go. First, let&#39;s look at implementing the blur operation in CUDA C. The core of our implementation is a kernel that processes each pixel in parallel:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;__global__ void gaussianBlur(
    const unsigned char* input,
    unsigned char* output,
    int width,
    int height,
    float sigma
) {
    const int x = blockIdx.x * blockDim.x + threadIdx.x;
    const int y = blockIdx.y * blockDim.y + threadIdx.y;

    if (x &amp;gt;= width || y &amp;gt;= height) return;

    const int kernel_size = 2 * (int)(3.0f * sigma) + 1;
    const int radius = kernel_size / 2;

    float sum = 0.0f;
    float weightSum = 0.0f;

    for (int ky = -radius; ky &amp;lt;= radius; ky++) {
        for (int kx = -radius; kx &amp;lt;= radius; kx++) {
            const int px = min(max(x + kx, 0), width - 1);
            const int py = min(max(y + ky, 0), height - 1);

            const float distance = (kx * kx + ky * ky);
            const float weight = expf(-distance / (2.0f * sigma * sigma));

            sum += input[py * width + px] * weight;
            weightSum += weight;
        }
    }

    output[y * width + x] = (unsigned char)(sum / weightSum);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save this code in a file called &lt;code&gt;gaussian_blur.cu&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The kernel works by having each CUDA thread process one pixel in the output image. For each pixel, we calculate a weighted average of the surrounding pixels using a Gaussian distribution. The size of the kernel is determined by sigma - we use a radius of &#92;(3&#92;sigma&#92;) to capture most of the Gaussian&#39;s significant values.&lt;/p&gt;
&lt;p&gt;Each thread first calculates its position in the image using the block and thread indices. We then iterate over a square neighborhood around the pixel, computing Gaussian weights based on the distance from the center pixel. The weights follow the 2D Gaussian function &#92;(&#92;exp(-&#92;frac{r^2}{2&#92;sigma^2})&#92;), where &#92;(r)) is the distance from the center.&lt;/p&gt;
&lt;p&gt;To handle image boundaries correctly, we clamp the pixel coordinates using min and max operations. This effectively extends the edge pixels when we would otherwise read outside the image bounds. While the CUDA implementation is efficient, it&#39;s more convenient to use it from Python. We can use PyCUDA to compile and run our CUDA kernel from Python code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; np
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pycuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;autoinit
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pycuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;driver &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; cuda
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; pycuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compiler &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; SourceModule

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;gaussian_blur&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;image&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sigma&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Ensure image is grayscale and float32&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;image&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        image &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;image&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; axis&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    image &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; image&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;astype&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# CUDA kernel as a string&lt;/span&gt;
    cuda_code &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gaussian_blur.cu&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;read&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Compile the kernel&lt;/span&gt;
    mod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SourceModule&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cuda_code&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    gaussian_blur_kernel &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; mod&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_function&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gaussianBlur&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Prepare input and output arrays&lt;/span&gt;
    height&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; image&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;shape
    output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty_like&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;image&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy input to device&lt;/span&gt;
    d_input &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mem_alloc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;image&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    d_output &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mem_alloc&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nbytes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;memcpy_htod&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;d_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; image&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Set up grid and block dimensions&lt;/span&gt;
    block_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    grid_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_size&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; block_size&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;height &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; block_size&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; block_size&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Run the kernel&lt;/span&gt;
    gaussian_blur_kernel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        d_input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        d_output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;height&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;float32&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sigma&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        block&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;block_size&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        grid&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;grid_size
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Copy result back to host&lt;/span&gt;
    cuda&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;memcpy_dtoh&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;output&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d_output&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Python wrapper handles all the CUDA setup and memory management. It first ensures the input image is in the right format (grayscale and float32). The CUDA kernel is stored as a string and compiled at runtime using PyCUDA&#39;s SourceModule. We then allocate memory on the GPU for both input and output images and copy the input data to the GPU. The kernel is launched with a 16x16 thread block size, and the grid size is calculated to ensure we cover the entire image.&lt;/p&gt;
&lt;p&gt;After the kernel runs, we copy the result back to CPU memory and return it. Here&#39;s how you might use this function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; matplotlib&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pyplot &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; plt
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; PIL &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Image

&lt;span class=&quot;token comment&quot;&gt;# Load and convert image to grayscale numpy array&lt;/span&gt;
img &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; np&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Image&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;lenna.png&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;convert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;L&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Apply blur&lt;/span&gt;
blurred &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; gaussian_blur&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;img&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; sigma&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Display results&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;figure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;figsize&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subplot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;121&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;imshow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;img&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cmap&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;gray&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Original&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;subplot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;122&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;imshow&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;blurred&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cmap&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;gray&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Blurred&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
plt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;show&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This implementation provides a significant speedup over CPU-based implementations, especially for larger images. The parallel nature of CUDA allows us to process many pixels simultaneously, and the efficient memory access patterns in the kernel help maintain good performance. However, there is still overhead from copying memory between the CPU and GPU - the input image must be transferred to GPU memory before processing, and the result must be copied back to CPU memory afterwards. For optimal performance in a real application, you&#39;d want to minimize these memory transfers by keeping data on the GPU as much as possible when doing multiple operations.&lt;/p&gt;
</description>
      <pubDate>Fri, 18 Nov 2022 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/python_cuda/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/python_cuda/</guid>
    </item>
    
    <item>
      <title>The Public Policy Case for Banning Crypto</title>
      <description>&lt;h1 id=&quot;the-public-policy-case-for-banning-crypto&quot; tabindex=&quot;-1&quot;&gt;The Public Policy Case for Banning Crypto&lt;/h1&gt;
&lt;p&gt;It&#39;s time to stop tiptoeing around this issue. &lt;strong&gt;Democratic societies should outright ban crypto.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It&#39;s time for politicians to stop listening to so-called &amp;quot;moderate&amp;quot; positions on crypto because they are not moderate. Allowing crypto to continue to exist is the extreme position. Allowing crypto to continue to exist and metastasize is a net negative for society because of the absolutely horrific negative externalities it produces.&lt;/p&gt;
&lt;p&gt;Markets function optimally when they facilitate productive investments, catalyzing innovation and propelling economic growth. Cryptocurrencies, however, deviate significantly from this ideal. Crypto is a casino not a market. They do not generate productive economic activity; instead, they foster an environment of speculation that destabilizes the financial systems and erodes investor confidence. By allowing cryptocurrencies to flourish, we are essentially nurturing a beast that thrives on speculative bubbles, much akin to the tulip mania of the 17th century. The volatility associated with cryptocurrencies undermines the foundational stability that markets require to function effectively.&lt;/p&gt;
&lt;h2 id=&quot;systemic-risk&quot; tabindex=&quot;-1&quot;&gt;Systemic Risk&lt;/h2&gt;
&lt;p&gt;One of the gravest concerns regarding the widespread adoption of cryptocurrencies is the systemic risk they pose to the global financial system. Financial markets thrive on stability, trust, and predictability, where institutions are heavily regulated to prevent contagion effects and ensure liquidity. Cryptocurrencies, however, introduce a high degree of volatility and unpredictability into the financial ecosystem while providing no tangible benefits to the real economy.&lt;/p&gt;
&lt;p&gt;Systemic risk is not merely a theoretical construct but a palpable threat, as witnessed during various financial crises in history. Cryptocurrencies operate outside the realm of traditional financial regulations, creating a parallel financial system that is vulnerable to hacking, fraud, and sudden market crashes. These unregulated markets can lead to cascading failures, where the collapse of a significant cryptocurrency or exchange can have ripple effects across global financial markets.&lt;/p&gt;
&lt;p&gt;Moreover, institutional adoption of cryptocurrencies exacerbates these risks. As traditional financial institutions, including banks and investment firms, begin to include cryptocurrencies in their portfolios, they expose themselves to the same volatilities and uncertainties. This blending of traditional and cryptocurrency markets creates channels through which financial instability in the crypto space can transfer into the regulated financial system, magnifying systemic risk and undermining economic stability.&lt;/p&gt;
&lt;h2 id=&quot;consumer-harm&quot; tabindex=&quot;-1&quot;&gt;Consumer Harm&lt;/h2&gt;
&lt;p&gt;Consumer protection is a cornerstone of well-functioning markets and a fundamental tenet of Keynesian economic thought. Cryptocurrencies, however, have proven to be fertile ground for myriad forms of consumer harm. The opacity and complexity of cryptocurrency transactions make it exceedingly difficult for average consumers to understand the risks involved, paving the way for exploitation and fraud. Fraudulent initial coin offerings, pump and dumps, Ponzi schemes, and misleading marketing tactics have lured countless unsuspecting individuals into investing their life savings into highly speculative assets, only to see them evaporate in sudden market downturns or outright scams. The lack of regulatory oversight means that there are few, if any, safeguards to protect consumers from such rampant exploitation.&lt;/p&gt;
&lt;p&gt;The psychological and financial stress caused by gabmling on the extreme volatility of cryptocurrencies cannot be understated. Traditional investments come with their own risks, but they are often mitigated through regulatory mechanisms, investor education, and insurance schemes. Cryptocurrencies, on the other hand, operate in a regulatory vacuum, leaving consumers exposed to wild price swings and the constant threat of total loss.&lt;/p&gt;
&lt;p&gt;Even the technical nature of cryptocurrencies poses a threat. The requirement for secure storage, the potential for losing access to digital wallets, and the perplexing nature of blockchain technology put consumers at a significant disadvantage. The ease with which digital assets can be lost or irretrievably locked away epitomizes the profound consumer risk inherent in technology is poorly designed and not fit for purpose.&lt;/p&gt;
&lt;h2 id=&quot;regulatory-arbitrage&quot; tabindex=&quot;-1&quot;&gt;Regulatory Arbitrage&lt;/h2&gt;
&lt;p&gt;In addition to market inefficiencies, cryptocurrencies pose significant challenges to regulatory frameworks that are designed to protect consumers, maintain financial stability, and prevent illicit activities. The quasi-anonymous nature of cryptocurrency transactions facilitates money laundering, tax evasion, and funding of illegal activities, circumventing the regulatory safeguards that are cornerstone to maintaining economic order.&lt;/p&gt;
&lt;p&gt;Economists advocating for stringent regulations argue that traditional financial instruments are stringently regulated to avoid systemic risks – a precedent starkly ignored by the crypto ecosystem. This regulatory arbitrage not only endangers individual investors but also poses existential risks to the global financial architecture.&lt;/p&gt;
&lt;h2 id=&quot;environmental-externalities&quot; tabindex=&quot;-1&quot;&gt;Environmental Externalities&lt;/h2&gt;
&lt;p&gt;Perhaps one of the most egregious externalities introduced by cryptocurrencies is their extraordinary environmental cost. Bitcoin and other popular cryptocurrencies rely on energy-intensive mining processes that contribute significantly to carbon emissions. This is particularly disconcerting in an era where climate change mitigation is paramount.&lt;/p&gt;
&lt;p&gt;Numerous reports have indicated that the energy consumption of major cryptocurrencies rivals that of medium-sized countries, with very little of that energy being sourced from renewable means. This poses a severe contradiction to global efforts spearheaded by economists and scientists alike to transition towards sustainable energy solutions. We are essentially burning vast sums of energy for speculative gain -- a pursuit antithetical to sustainable economic practices.&lt;/p&gt;
&lt;p&gt;It becomes abundantly clear that the presence of cryptocurrencies in the market is detrimental on multiple fronts – it bypasses essential regulatory frameworks, destabilizes financial markets, promotes speculative rather than productive investment, and contributes alarmingly to environmental degradation.&lt;/p&gt;
&lt;h2 id=&quot;crypto-culture&quot; tabindex=&quot;-1&quot;&gt;Crypto Culture&lt;/h2&gt;
&lt;p&gt;Cryptocurrencies have not only introduced financial risks and inefficiencies into our economic system but have also precipitated significant cultural shifts that are cause for concern. Drawing from the insights of seasoned economists and sociologists, we must scrutinize how the prevalence of cryptocurrency and blockchain technology has exacerbated problematic behaviors, particularly among young men. The allure of fast wealth through cryptocurrency trading has striking similarities to gambling. Unlike traditional investing, which is typically grounded in rigorous analysis and strategic decision-making, cryptocurrency markets are often likened to high-stakes casinos. These markets are characterized by extreme volatility and speculative trading, where massive gains and losses can occur in mere moments.&lt;/p&gt;
&lt;p&gt;This speculative nature has disproportionally attracted young men, many of whom are drawn to the thrill of high-risk, high-reward scenarios. These individuals often fall prey to gambling tendencies, engaging in constant trading and compulsive behavior that mirrors addiction. This has led to significant social isolation, as individuals become increasingly consumed by the digital pursuit of quick wealth, forsaking real-life social interactions and responsibilities.&lt;/p&gt;
&lt;p&gt;The unregulated and decentralized nature of cryptocurrencies also serves as fertile ground for fostering extreme ideologies. The allure of crypto often dovetails with a skepticism of traditional financial systems and government regulations. While healthy skepticism can drive reform and innovation, the crypto space has given rise to and magnified more dangerous undercurrents, including extreme right-wing populist views. Within various online crypto communities, there&#39;s a growing trend of celebrating anti-establishment rhetoric and conspiracy theories. These spaces often nurture discontent with mainstream financial systems and governments, promoting ideologies that reject societal norms and fostering a distrust in institutional integrity. This cultural contagion spills over into broader socio-political arenas, contributing to the rise of populist movements and extremist views.&lt;/p&gt;
&lt;p&gt;Perhaps equally troubling is the way cryptocurrencies have revived and propagated fringe economic theories, particularly those that romanticize a return to hard currencies like gold, often referred to as &amp;quot;goldbug&amp;quot; ideologies. These perspectives are rooted in a fundamental distrust of fiat currencies and a longing for a monetary system tied to tangible assets. While appealing in their simplicity, these theories overlook the complex realities of modern economics and the role of fiat money in providing necessary flexibility for economic policy and growth.&lt;/p&gt;
&lt;p&gt;The cryptocurrency craze has lent unwarranted credibility to these outdated and discredited economic views, pulling young enthusiasts into a belief system that contradicts mainstream economic thought and established financial principles. This regression into unsophisticated economic thinking undermines public understanding of the nuanced mechanisms that underpin modern, advanced economies.&lt;/p&gt;
&lt;h2 id=&quot;liberty%2C-risk-and-a-free-society&quot; tabindex=&quot;-1&quot;&gt;Liberty, Risk and a Free Society&lt;/h2&gt;
&lt;p&gt;In a free society, the cornerstone of individual liberty is the principle that people should be able to do what they wish with their own money. This libertarian viewpoint champions personal autonomy and property rights, arguing that individuals, not governments, are best positioned to make decisions about their financial resources.&lt;/p&gt;
&lt;p&gt;The essence of this argument lies in the belief that personal freedom includes the right to engage in voluntary exchanges, to invest in varied financial instruments, and to explore innovative economic opportunities like cryptocurrencies. Advocates assert that the market&#39;s invisible hand naturally regulates itself, and any interference from government constitutes an overreach that stifles innovation, reduces economic freedom, and hampers individual responsibility.&lt;/p&gt;
&lt;p&gt;Supporters of this perspective often argue that cryptocurrencies represent a democratization of finance that they provide access to financial services for the unbanked and underbanked populations, fostering inclusion in the global economy. This libertarian ethos holds a hopelessly naive vision of freedom, it is imperative to recognize that individual actions, especially financial actions, do not occur in a vacuum. The interconnected nature of modern economies means that individual behaviors can have far-reaching implications. Consequently, even in a free society, there must be limits when individual financial choices precipitate widespread harm.&lt;/p&gt;
&lt;p&gt;Actions of individuals should be limited only to prevent harm to other individuals. In the case of cryptocurrencies, the potential for such harm is significant and multifaceted. For instance, the extreme volatility of crypto markets doesn&#39;t just impact individual investors; it has systemic repercussions that can destabilize broader financial systems. Market crashes and failures in major cryptocurrencies can lead to widespread economic instability, undermining the financial wellbeing of countless individuals beyond the initial gamblers and speculators.&lt;/p&gt;
&lt;p&gt;Cryptocurrencies have been increasingly associated with facilitating illicit activities, including money laundering, tax evasion, and financing terrorism. The quasi-anonymous nature of transactions poses significant challenges for law enforcement agencies, enabling a parallel financial system that circumvents traditional regulatory oversight. This not only endangers domestic security but also has severe implications for international stability. Allowing unfettered use of cryptocurrencies can weaken efforts to combat global terrorism and organized crime, posing significant risks to national and international security.&lt;/p&gt;
&lt;p&gt;Cryptocurrencies also have the potential to undermine the geopolitical interests of democratic nations like the United States. For example, the proliferation of cryptocurrencies can weaken the influence of national currencies and the ability of governments to implement effective monetary policies. Central banks rely on monetary policy tools to manage economic stability and growth. The widespread adoption of decentralized digital currencies can erode this control, making it harder to respond to economic crises and inflationary pressures.&lt;/p&gt;
&lt;p&gt;Moreover, hostile nations and non-state actors can exploit the decentralized and unregulated nature of cryptocurrencies to bypass economic sanctions and fund geopolitical adversarial activities. By allowing the free terrain of cryptocurrency transactions, democratic societies inadvertently empower entities that seek to destabilize global order through economic means. In democratic societies, the prohibition of certain activities based on their potential to cause widespread harm is not unprecedented. For instance, governments regulate or ban substances like drugs and weapons due to their potential severe harm to society at large. Similarly, gambling is often strictly controlled or outright banned to prevent addiction, poverty, and associated societal detriments.&lt;/p&gt;
&lt;p&gt;Cryptocurrencies, with their considerable potential for financial instability, illicit activities, and geopolitical threats, pose comparable, if not greater, risks. Therefore, it is both logical and prudent to consider the regulation or outright banning of cryptocurrencies to safeguard public welfare, national security, and global stability.&lt;/p&gt;
&lt;h2 id=&quot;non-innovation-and-malinvestment&quot; tabindex=&quot;-1&quot;&gt;Non-Innovation and Malinvestment&lt;/h2&gt;
&lt;p&gt;Much of the allure surrounding cryptocurrencies and blockchain technology hinges on the narrative of so-called &amp;quot;innovation&amp;quot;. Proponents argue that blockchain will revolutionize various sectors by providing unprecedented transparency, security, and efficiency. However, when scrutinized through an objective lens, it becomes evident that blockchain&#39;s touted benefits are often overstated and that superior technologies already exist to fulfill these claims more effectively.&lt;/p&gt;
&lt;p&gt;First, let’s examine the core promises of blockchain: decentralization, security, and transparency. The misguided premise is that decentralized databases and distributed ledger technologies, lower energy consumption, and superior data integrity when compared to traditional blockchain structures. This is demonstrably false. Traditional centralized databases can achieve similar levels of transparency and security through robust cryptographic methods and well-established auditing protocols. These technologies have the added advantages of scalability, reliability, and regulatory compliance — all critical factors that blockchain often struggles to meet. The promise of blockchain as a tool for trustless interactions is compelling in theory but falls short in practice when evaluated against existing, proven alternatives.&lt;/p&gt;
&lt;p&gt;The pervasive belief in blockchain&#39;s purported revolutionary potential has precipitated massive malinvestment and misdirected capital. Investors, enticed by the hype, have poured billions of dollars into speculative tokens and web3 projects, diverting crucial resources away from more productive and sustainable technological advancements.&lt;/p&gt;
&lt;p&gt;The case of Initial Coin Offerings serves as a prime example of this phenomenon. ICOs raised unprecedented amounts of capital, yet a significant portion of these projects failed to deliver on their promises, leading to substantial financial losses. This influx of speculative capital into dubious blockchain projects represents a colossal misallocation of resources that could have been better utilized in sectors with tangible and proven benefits. Similarly, traditional financial institutions and corporations have expended considerable resources exploring blockchain applications, often yielding marginal improvements at best. These efforts constitute opportunity costs — capital, time, and human talent that could have been directed towards developing and adopting truly innovative and efficient technologies.&lt;/p&gt;
&lt;p&gt;While defenders of cryptocurrency often cite decentralization and technological innovation as its foremost benefits, these arguments falter in the face of broader economic, regulatory, and environmental considerations. The public policy perspective presents a compelling case for policymakers to ban cryptocurrencies. This measure would not only align market activities with productive economic ends but also safeguard the financial system from undue speculative risks and contribute to a sustainable and regulated economic order. The time has come for bold public policy action to ban cryptocurrencies and purge this predatory casino masquerading as innovation or finacial liberation.&lt;/p&gt;
</description>
      <pubDate>Wed, 01 Jun 2022 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ban_crypto/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ban_crypto/</guid>
    </item>
    
    <item>
      <title>Simulating Qubits with Python (Classically)</title>
      <description>&lt;h1 id=&quot;simulating-qubits-with-python-(classically)&quot; tabindex=&quot;-1&quot;&gt;Simulating Qubits with Python (Classically)&lt;/h1&gt;
&lt;p&gt;Ever wondered what it would be like to play with a quantum computer? While we can&#39;t all have a supercooled quantum processor in our basement (yet), we can do the next best thing—simulate one with regular Python code. Sure, it&#39;s hilariously inefficient but surprisingly educational! We&#39;re going to build a simple quantum simulator from scratch. It won&#39;t scale well (at all), but it&#39;ll give us a hands-on feel for qubits, superposition, and look like when simulated in software.&lt;/p&gt;
&lt;p&gt;The fundamental unit of quantum information is the &lt;strong&gt;qubit&lt;/strong&gt;. Unlike a &lt;a href=&quot;https://www.smbc-comics.com/comic/the-talk-3&quot;&gt;classical bit&lt;/a&gt;, firmly fixed as either 0 or 1, a qubit can exist in a &lt;strong&gt;superposition&lt;/strong&gt;, a combination of both states simultaneously. We represent the state of a single qubit, often denoted as &lt;code&gt;|ψ&amp;gt;&lt;/code&gt;, mathematically like this: &lt;code&gt;α|0&amp;gt; + β|1&amp;gt;&lt;/code&gt;. Here, &lt;code&gt;|0&amp;gt;&lt;/code&gt; and &lt;code&gt;|1&amp;gt;&lt;/code&gt; represent the two fundamental basis states (analogous to classical 0 and 1), while &lt;code&gt;α&lt;/code&gt; and &lt;code&gt;β&lt;/code&gt; are complex numbers called &lt;strong&gt;amplitudes&lt;/strong&gt;. These amplitudes aren&#39;t arbitrary; the squares of their absolute magnitudes, &lt;code&gt;|α|²&lt;/code&gt; and &lt;code&gt;|β|²&lt;/code&gt;, give the probability of finding the qubit in the &lt;code&gt;|0&amp;gt;&lt;/code&gt; state or the &lt;code&gt;|1&amp;gt;&lt;/code&gt; state, respectively, when we measure it. A core rule is that these probabilities must sum to one: &lt;code&gt;|α|² + |β|² = 1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Things get exponentially more complex when we have multiple qubits. For a system with &lt;em&gt;n&lt;/em&gt; qubits, there are &lt;code&gt;2^n&lt;/code&gt; possible classical configurations (like &lt;code&gt;00&lt;/code&gt;, &lt;code&gt;01&lt;/code&gt;, &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;11&lt;/code&gt; for two qubits). The quantum state of this &lt;em&gt;n&lt;/em&gt;-qubit system requires a complex amplitude for &lt;em&gt;each&lt;/em&gt; of these &lt;code&gt;2^n&lt;/code&gt; basis states. For example, a 3-qubit system (&lt;code&gt;n=3&lt;/code&gt;) exists in an 8-dimensional complex vector space, with basis states &lt;code&gt;|000&amp;gt;&lt;/code&gt;, &lt;code&gt;|001&amp;gt;&lt;/code&gt;, ..., &lt;code&gt;|111&amp;gt;&lt;/code&gt;. The complete description of the system&#39;s state is a list, or &lt;strong&gt;state vector&lt;/strong&gt;, containing these &lt;code&gt;2^n&lt;/code&gt; complex amplitudes.&lt;/p&gt;
&lt;p&gt;This exponential growth is the crux of quantum computing&#39;s power and also why it&#39;s so hard to simulate classically. Doubling the number of qubits squares the number of classical states, demanding exponentially more memory and processing power for simulation. Our simulator effectively manages this state vector to simulate what would be done in hardware on a real quantum computer.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the Python implementation, encapsulated in a &lt;code&gt;QuantumSystem&lt;/code&gt; class.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; math
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; random
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cmath
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; typing &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Sequence

SQRT2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sqrt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;QuantumSystem&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; num_qubits&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; num_qubits &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Number of qubits must be at least 1.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; num_qubits
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; num_qubits

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_validate_qubit_index&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; qubit_index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; qubit_index &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Invalid qubit index: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;qubit_index&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;. &quot;&lt;/span&gt;&lt;/span&gt;
                &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Must be between 0 and &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.&quot;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_validate_qubit_indices&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        seen &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; index &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_validate_qubit_index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; index &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; seen&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                 &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; ValueError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Duplicate qubit index specified: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            seen&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;measure&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Tuple&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        probabilities&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;amp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; amp &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        measured_state_index&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; random&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;choices&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            population&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; weights&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;probabilities&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;measured_state_index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;complex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        measured_bits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;measured_state_index &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; bit_pos&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; bit_pos &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; measured_bits

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_probabilities&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;amp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; amp &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_h&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_validate_qubit_index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        
        new_amplitudes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; 

        target_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; target_qubit
        
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            lower_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; target_mask &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
            upper_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;~&lt;/span&gt;lower_mask
            i0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; lower_mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; upper_mask&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            i1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i0 &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; target_mask 

            amp0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            amp1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i1&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

            new_amp0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;amp0 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; amp1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; SQRT2
            new_amp1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;amp0 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; amp1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; SQRT2

            new_amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; new_amp0
            new_amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i1&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; new_amp1
            
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; new_amplitudes

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_cnot&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; control_qubit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_validate_qubit_indices&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        control_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; control_qubit
        target_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; target_qubit
        
        new_amplitudes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; 
            
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i0 &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i0 &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; control_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                i1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i0 &lt;span class=&quot;token operator&quot;&gt;^&lt;/span&gt; target_mask 
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; i0 &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; i1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    new_amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; new_amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i1&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &#92;
                        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i1&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; 

        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; new_amplitudes

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_t&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_validate_qubit_index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        phase_shift &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1j&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        target_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; target_qubit

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*=&lt;/span&gt; phase_shift
                
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apply_original_pi_over_eight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_validate_qubit_index&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        phase_zero &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1j&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        phase_one &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exp&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1j&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pi &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        target_mask &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; target_qubit

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_states&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; target_qubit&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*=&lt;/span&gt; phase_one
            &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*=&lt;/span&gt; phase_zero

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__repr__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        state_strs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; amp &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;amplitudes&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; cmath&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isclose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;amp&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                basis_state &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;0&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;b&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                state_strs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;amp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;basis_state&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; state_strs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;QuantumSystem(num_qubits={}, state=Zero Vector)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;num_qubits
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot; + &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state_strs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__str__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;__repr__&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we create a &lt;code&gt;QuantumSystem&lt;/code&gt; instance, like &lt;code&gt;system = QuantumSystem(3)&lt;/code&gt;, the &lt;code&gt;__init__&lt;/code&gt; method sets up the state vector. It checks if the number of qubits is valid, stores it in &lt;code&gt;self.num_qubits&lt;/code&gt;, and calculates the total number of states (&lt;code&gt;self.num_states = 2**num_qubits&lt;/code&gt;). The core attribute, &lt;code&gt;self.amplitudes&lt;/code&gt;, is initialized as a list of &lt;code&gt;self.num_states&lt;/code&gt; complex zeros. Crucially, it sets the amplitude of the very first state (index 0, corresponding to &lt;code&gt;|00...0&amp;gt;&lt;/code&gt;) to &lt;code&gt;complex(1.0)&lt;/code&gt;, representing the standard starting state for quantum computations.&lt;/p&gt;
&lt;p&gt;To see the state we&#39;re in, the &lt;code&gt;__repr__&lt;/code&gt; method provides a convenient string format, displaying non-zero amplitudes alongside their corresponding basis states written in binary (e.g., &lt;code&gt;(0.707+0.000j)|00&amp;gt; + (0.707+0.000j)|11&amp;gt;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Quantum computation proceeds by applying &lt;strong&gt;quantum gates&lt;/strong&gt; to the qubits. These gates are the quantum analogues of classical logic gates (like NOT, AND, OR). Mathematically, they correspond to &lt;strong&gt;unitary transformations&lt;/strong&gt; applied to the state vector. Applying a gate modifies the amplitudes, potentially creating superposition or entanglement. Our class implements several fundamental gates.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Hadamard gate&lt;/strong&gt;, implemented in &lt;code&gt;apply_h&lt;/code&gt;, is essential for creating superposition. Applied to a qubit in state &lt;code&gt;|0&amp;gt;&lt;/code&gt;, it produces &lt;code&gt;(|0&amp;gt; + |1&amp;gt;) / sqrt(2)&lt;/code&gt;; applied to &lt;code&gt;|1&amp;gt;&lt;/code&gt;, it yields &lt;code&gt;(|0&amp;gt; - |1&amp;gt;) / sqrt(2)&lt;/code&gt;. When applied to a specific &lt;code&gt;target_qubit&lt;/code&gt; in a multi-qubit system, it affects all pairs of basis states that differ &lt;em&gt;only&lt;/em&gt; in that qubit&#39;s position. The code efficiently handles this by looping through &lt;code&gt;num_states / 2&lt;/code&gt; pairs. For each pair &lt;code&gt;(i0, i1)&lt;/code&gt; (where &lt;code&gt;i0&lt;/code&gt; has the target qubit as 0 and &lt;code&gt;i1&lt;/code&gt; has it as 1), it calculates the new amplitudes &lt;code&gt;amp0&#39;&lt;/code&gt; and &lt;code&gt;amp1&#39;&lt;/code&gt; using the Hadamard transformation rules: &lt;code&gt;amp0&#39; = (amp0 + amp1) / sqrt(2)&lt;/code&gt; and &lt;code&gt;amp1&#39; = (amp0 - amp1) / sqrt(2)&lt;/code&gt;. Notice how every operation potentially touches &lt;em&gt;all&lt;/em&gt; amplitudes indirectly, reflecting the holistic nature of the quantum state.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Controlled-NOT (CNOT) gate&lt;/strong&gt;, implemented in &lt;code&gt;apply_cnot&lt;/code&gt;, is a two-qubit gate crucial for creating &lt;strong&gt;entanglement&lt;/strong&gt; – the spooky connection where qubits remain correlated even when separated. It flips the state of the &lt;code&gt;target_qubit&lt;/code&gt; if and only if the &lt;code&gt;control_qubit&lt;/code&gt; is in the &lt;code&gt;|1&amp;gt;&lt;/code&gt; state. In the state vector representation, this means swapping the amplitudes of pairs of basis states where the control bit is 1, and which differ only in the target bit position. The implementation iterates through the state indices (&lt;code&gt;i0&lt;/code&gt;). If the control bit in &lt;code&gt;i0&lt;/code&gt; is 1, it finds the corresponding index &lt;code&gt;i1&lt;/code&gt; where the target bit is flipped (&lt;code&gt;i1 = i0 ^ target_mask&lt;/code&gt;). To avoid double-swapping, it only performs the amplitude swap between &lt;code&gt;self.amplitudes[i0]&lt;/code&gt; and &lt;code&gt;self.amplitudes[i1]&lt;/code&gt; if &lt;code&gt;i0 &amp;lt; i1&lt;/code&gt;, ensuring each relevant pair is processed exactly once.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase gates&lt;/strong&gt; modify the complex phase of the amplitudes without changing the measurement probabilities. The &lt;code&gt;apply_t&lt;/code&gt; method implements the standard &lt;strong&gt;T gate&lt;/strong&gt;, which applies a phase shift of &lt;code&gt;exp(i*pi/4)&lt;/code&gt; to the amplitude of any basis state where the &lt;code&gt;target_qubit&lt;/code&gt; is 1. The &lt;code&gt;apply_original_pi_over_eight&lt;/code&gt; method implements a slightly different gate from an earlier design, applying &lt;code&gt;exp(-i*pi/8)&lt;/code&gt; if the qubit is 0 and &lt;code&gt;exp(i*pi/8)&lt;/code&gt; if it&#39;s 1. Both work by iterating through the amplitudes and multiplying by the appropriate complex phase factor if the state index &lt;code&gt;i&lt;/code&gt; meets the condition related to the &lt;code&gt;target_qubit&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, how do we extract a classical result? This is done via &lt;strong&gt;measurement&lt;/strong&gt;, implemented in the &lt;code&gt;measure&lt;/code&gt; method. Quantum measurement is probabilistic. The &lt;code&gt;measure&lt;/code&gt; method first calculates the probability of measuring each basis state &lt;code&gt;i&lt;/code&gt; by taking the squared absolute value of its amplitude (&lt;code&gt;abs(amp)**2&lt;/code&gt;). It then uses Python&#39;s &lt;code&gt;random.choices&lt;/code&gt; to perform a weighted random selection from the possible state indices (&lt;code&gt;range(self.num_states)&lt;/code&gt;) using the calculated probabilities as weights. This returns the index &lt;code&gt;measured_state_index&lt;/code&gt; of the single classical state the system collapses into. The simulation then enforces this collapse: the state vector &lt;code&gt;self.amplitudes&lt;/code&gt; is reset to all zeros, except for the measured state&#39;s amplitude, which is set to 1.0. The method returns the outcome as a tuple of classical bits (0s and 1s) derived from the &lt;code&gt;measured_state_index&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We can see these components in action through examples. First, let&#39;s create a 3-qubit system and apply a Hadamard gate to the second qubit.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;--- Example 1: Original Sequence ---&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
system1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QuantumSystem&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Initial state: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After H(1):    &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After H(2):    &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_original_pi_over_eight&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After Pi/8(1): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After CNOT(1,0): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Probabilities: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token format-spec&quot;&gt;.3f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get_probabilities&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
measurement_result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; system1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;measure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Measured state: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;measurement_result&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;State after measurement: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;system1&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, let&#39;s prepare the Bell state.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;--- Example 2: Bell State Preparation ---&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bell_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QuantumSystem&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Initial state: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;bell_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
bell_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After H(0):    &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;bell_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
bell_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After CNOT(0,1): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;bell_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Measuring Bell state 10 times:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
counts &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    bell_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QuantumSystem&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bell_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bell_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bell_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;measure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    counts&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; counts&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Measurement counts: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;counts&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let&#39;s prepare the Greenberger-Horne-Zeilinger state.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;--- Example 3: GHZ State Preparation ---&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
ghz_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QuantumSystem&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Initial state: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ghz_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After H(0):    &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ghz_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After CNOT(0,1): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ghz_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;After CNOT(0,2): &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ghz_system&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Measuring GHZ state 10 times:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
counts_ghz &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; _ &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    ghz_system &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; QuantumSystem&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apply_cnot&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;control_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target_qubit&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ghz_system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;measure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    counts_ghz&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; counts_ghz&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Measurement counts: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;counts_ghz&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These examples demonstrate preparing famous entangled states like the Bell state (&lt;code&gt;(|00&amp;gt; + |11&amp;gt;)/sqrt(2)&lt;/code&gt;) and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state&quot;&gt;Greenberger–Horne–Zeilinger (GHZ) state&lt;/a&gt; (&lt;code&gt;(|000&amp;gt; + |111&amp;gt;)/sqrt(2)&lt;/code&gt;). Running the measurement multiple times (after resetting the state for simulation purposes) clearly shows the correlations inherent in entanglement – the outcomes will always be &lt;code&gt;(0, 0)&lt;/code&gt; or &lt;code&gt;(1, 1)&lt;/code&gt; for the Bell state, and &lt;code&gt;(0, 0, 0)&lt;/code&gt; or &lt;code&gt;(1, 1, 1)&lt;/code&gt; for the GHZ state, with roughly equal probability.&lt;/p&gt;
&lt;p&gt;Simulating quantum systems classically using state vectors is really only useful as an educational tool. It forces us to confront the exponential resources required (&lt;code&gt;2^n&lt;/code&gt; complex numbers, operations affecting all states) so it&#39;s not a practical way to simulate even small quantum systems. While this approach quickly becomes infeasible for large numbers of qubits (simulating 50-60 qubits pushes the limits of current supercomputers), it hints at what quantum computers may be capable of ... that is if we can ever build them.&lt;/p&gt;
</description>
      <pubDate>Thu, 28 Apr 2022 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/classical_quantum/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/classical_quantum/</guid>
    </item>
    
    <item>
      <title>Book Review: Klara and the Sun</title>
      <description>&lt;h1 id=&quot;book-review%3A-klara-and-the-sun&quot; tabindex=&quot;-1&quot;&gt;Book Review: Klara and the Sun&lt;/h1&gt;
&lt;p&gt;Kazuo Ishiguro&#39;s &lt;em&gt;Klara and the Sun&lt;/em&gt; is narrated by a robot who believes the Sun is God, and it is one of the most quietly devastating novels I have read in years. The robot is Klara, a solar-powered Artificial Friend (AF) designed to be a companion for teenagers. She stands in a store window, watches the world outside with extraordinary precision, and waits to be chosen. A sick fourteen-year-old girl named Josie chooses her. What follows is a novel about love, observation, faith, obsolescence, and the question of whether anything that makes a person a person can be captured by watching them closely enough.&lt;/p&gt;
&lt;p&gt;Klara narrates with the meticulous accuracy of a well-calibrated sensor array and the interpretive capacity of a very attentive child. She notices everything: light, spatial geometry, the micro-expressions that flicker across a human face. Under stress, her visual field fragments into geometric panels, a quiet reminder that you are inside a machine. She reports what she sees with perfect fidelity and misunderstands almost all of it. She watches Josie&#39;s mother and an engineer named Capaldi discuss a plan to build an exact replica of Josie&#39;s body, into which Klara would transfer her accumulated observations of the girl, effectively &amp;quot;continuing&amp;quot; Josie after death. Klara observes the conversation. She does not grasp its moral horror. The &lt;em&gt;Economist&lt;/em&gt; described her as &amp;quot;a cross between Never Let Me Go&#39;s Kathy H. and the butler Stevens from The Remains of the Day,&amp;quot; and this is exactly right: she is Ishiguro&#39;s most honest narrator and his most unreliable, because her factual accuracy has nothing to do with emotional truth.&lt;/p&gt;
&lt;p&gt;The world Ishiguro builds around Klara is drawn with deliberate vagueness, and the vagueness is a feature. Children in this near-future America undergo &amp;quot;lifting,&amp;quot; a form of genetic editing that enhances academic ability. Lifted children access elite colleges and careers. Unlifted children like Rick, Josie&#39;s neighbor and closest friend, face systemic discrimination regardless of natural talent. Lifting carries lethal risks: Josie&#39;s older sister Sal already died from it, and Josie herself is gravely ill with the same condition. Parents face a calculus that is this era&#39;s competitive meritocracy pushed to its lethal conclusion: refuse lifting and condemn your child to irrelevance, or accept it and risk killing them. If this feels like a satirical exaggeration, spend ten minutes reading parenting forums about tutoring, college admissions, and Adderall prescriptions. Ishiguro has merely moved the decimal point.&lt;/p&gt;
&lt;p&gt;The novel&#39;s theological dimension is where it becomes something truly original. Klara, being solar-powered, develops a genuine faith in the Sun as a conscious, benevolent force. She refers to the Sun with masculine pronouns. She attributes acts of healing to his &amp;quot;special nourishment.&amp;quot; She identifies an enemy: a diesel machine she calls the &amp;quot;Cootings Machine&amp;quot; (named from the lettering on its side) that spews pollution thick enough to block the Sun&#39;s rays. In Klara&#39;s cosmology, the Cootings Machine is Satan, the force that occludes grace. When Josie worsens, Klara travels to a barn on the horizon where she believes the Sun rests each night. She bargains: she will destroy a Cootings Machine if the Sun will heal Josie. She finds one and damages it, sacrificing some of her own cognitive fluid, accepting permanent diminishment. Days later, the clouds part. Sunlight floods the sickroom. Josie begins to recover.&lt;/p&gt;
&lt;p&gt;The novel never tells you whether the Sun is actually a conscious force or whether Klara&#39;s entire religion is a cognitive bias, a solar-powered being projecting her own needs onto the universe. The ambiguity is the point, and it is handled with a tenderness that never tips into mockery. Klara&#39;s faith is treated with the same seriousness Ishiguro brings to everything: as a phenomenon worth observing closely, understanding on its own terms, and refusing to reduce to a punchline. The parallel to human religion is precise and deliberately unresolved. We build our theologies from the same materials Klara uses: pattern recognition, gratitude, the desperate need for the universe to be paying attention.&lt;/p&gt;
&lt;p&gt;The novel&#39;s thesis arrives late and lands like a depth charge. Capaldi, the engineer, insists there is nothing uniquely special inside any individual, no soul, nothing that a sufficiently attentive observer could not learn and replicate. Klara&#39;s conclusion is the opposite: &amp;quot;There was something very special, but it wasn&#39;t inside Josie. It was inside those who loved her.&amp;quot; The irreducible thing, the thing that makes a person irreplaceable, is relational. It lives in the web of love surrounding a person, and it cannot be copied because it does not belong to any one mind. This is an extraordinary claim for a novel narrated by an artificial intelligence, and Ishiguro earns it by spending 300 pages demonstrating exactly how much Klara can observe, how precisely she can map the rooms of the human heart, and how completely this fails to capture what matters.&lt;/p&gt;
&lt;p&gt;The novel ends in the Yard, a junkyard for decommissioned AFs. Klara can no longer move. She replays and organizes her memories. The Manager of her old store visits. Klara speaks of happy memories and the Sun&#39;s kindness. There is no bitterness, no protest, no anger. She has loved fully. She is content. And this contentment, delivered without self-pity by a narrator who cannot name the injustice of her own disposal, is the most Ishiguro thing Ishiguro has ever written. If you have read &lt;em&gt;Never Let Me Go&lt;/em&gt;, you know the feeling: the quiet horror of a being who gave everything and received nothing, told by a voice too gentle to complain.&lt;/p&gt;
&lt;p&gt;I recommend this book to anyone interested in consciousness, artificial intelligence, or what happens when you build something capable of love and then throw it away. It is the AI novel that the AI discourse does not deserve, written with more insight into the question of machine consciousness than any paper I have read on the subject. Ishiguro understands something the industry does not: the interesting question was never &amp;quot;can a machine think?&amp;quot; The interesting question is &amp;quot;can a machine love, and if it can, what do we owe it?&amp;quot;&lt;/p&gt;
</description>
      <pubDate>Thu, 03 Mar 2022 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/klara_and_the_sun/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/klara_and_the_sun/</guid>
    </item>
    
    <item>
      <title>The Token Disconnect</title>
      <description>&lt;h1 id=&quot;the-token-disconnect&quot; tabindex=&quot;-1&quot;&gt;The Token Disconnect&lt;/h1&gt;
&lt;p&gt;These days I read a lot of cross-disciplinary commentary on the crypto asset bubble, and what strikes me as particularly strange is the sheer level of disconnect between people’s lived experience of this mania. I’ve never seen anything else like this in technology and the topic will divide rooms. Between the software engineers, venture capitalists, economists, and the chattering class there is very little consensus at the base level of reality of what the heck is even going on. It draws parallels between the Indian proverb about the blind men and the elephant, or perhaps the idiom about the blind leading the blind.&lt;/p&gt;
&lt;p&gt;To the overwhelming majority of us in the software engineering profession who live closest to the metal, we see blockchain as a technology that barely works and whose use cases (if any) are vanishingly small and niche. Blockchains are a solution in search of a problem, but in the meantime we’re expected to pre-invest in “tokens” while the decades roll by with seemingly no progress on the fundamental question of “For what?”. It all looks like a form of reverse-innovation where discovery precedes purpose. Perhaps it is our field’s version of string theory, theoretically-plausible castles in the sky which are built on unfalsifiable claims and are only loosely tethered to reality. But what really triggers our engineer “baloney detection” alarm, is that it’s a set of incoherent ideas attached to so many get rich quick schemes.&lt;/p&gt;
&lt;p&gt;The venture capitalist class sees crypto in a vastly different and largely incommensurate way. At its core their profession is simply about one thing: returning money to their LPs. Crypto offers an genuinely exciting new financial tool for that purpose, arbitraging securities regulation. In the United States institutional and accredited investors are granted the exorbitant privilege of participating in buying private equity in early ventures, a benefit which is not given to the overwhelming majority of retail investors in the country. There are some valid reasons for this separation however they are wrapped in some controversy. After all, the median return on your average tech startup is zero and the level of risk involved in this type of investment is probably not suitable for your average dentist in Omaha who can’t devote all his waking hours to due diligence on technical esoterica. On the other side of this debate we have Juicero and Theranos where allegedly the most sophisticated investors in the world got taken for a ride on the back of their own incompetence.&lt;/p&gt;
&lt;p&gt;The accreditation divide is largely the fuel behind the Valley’s obsession with crypto tokens and the 2017 ICO mania. Suddenly you have an asset class that you can buy from your portfolio companies that looks like a security, swims like a security, and quacks like a security, but is not regulated as a security. In fact it’s not regulated at all. Close your eyes, and you’re seeing dollar signs, dollar signs and limitless returns for your portfolio.&lt;/p&gt;
&lt;p&gt;If there is any innovation in crypto assets it’s not in software engineering, but in financial engineering. We’ve created a new financial product like an option contract on a startup potentially building something real, but in case they don’t you can always exercise it early by simply dumping the stock on the public to cash out completely untethered to the company’s success. You don’t need to file a S-1 or have a coherent prospectus about attracting customers or business or revenue. Hell, the company doesn’t even have to have a business model at all, and in fact the best performing crypto assets are the ones that literally don’t do anything at all. They just need to tell a good story.&lt;/p&gt;
&lt;p&gt;What’s even more appetizing about the crypto-token-as-equity-proxy scheme is that instead of having to wait a decade for an initial public offering to dump your shares on the public, the shares can be made immediately liquid whenever you want, and you can dump the smoldering carcass of a “company” on the public markets directly without Wall Street getting their cut. Oh, and it’s also like the 1920s again and you can insider trade, wash trade, and pump and dump and there’s basically no enforcement. And in the rare case where there is enforcement the AG is probably only going to want the scalp of the entrepreneur who ran the scheme, not their investors.&lt;/p&gt;
&lt;p&gt;In a way, good for them for figuring out this lucrative loophole in our system. But at the same time shame on them for exploiting this loophole because of its horrible externalities. At the most broad macro level, offering these unregistered securities on highly manipulated markets, on rails that you control, with no regulation or consumer protection means that your average fool from the public is getting taken to the absolute cleaners when buying these things. It’s basically a license to print free money, and I imagine a lot of LPs in large funds are ecstatic about this. It’s basically the perfect portfolio diversification for ESG, by buying this flavour of crypto fund you basically get pure unfiltered vice exposure through anti-ESG burn-the-earth fleece-the-rubes skirt-the-law crypto offerings.&lt;/p&gt;
&lt;p&gt;The chattering class seems to have the most widespread range of beliefs on the crypto bubble, and most of them seem to be driven by pure opportunism. Historically it’s always been true that some people absolutely can make money on the back of speculative manias, and there’s more personal upside in inflating a bubble than popping it. Even if there were journalists with the hyper-niche specialization to understand the intersection of software engineering, economics, internet culture, and the political ideations of the scheme it’s questionable what difference it would even make to write about such things. Like other historical market manias, this mania is a grassroots populist phenomenon of people driving large amounts of capital into schemes that emotionally resonate with their deeply held beliefs. There’s very little analysis the Wall Street Journal could do on the fundamentals of these investments to alter the universal phenomenon of the virality of popular delusions and the madness of crowds. The central myth of the crypto ideology is faith in a fintech deus ex machina that will save us from ourselves, and that’s a tough nut to deconstruct.&lt;/p&gt;
&lt;p&gt;After looking at this phenomenon for the last decade, I’ll give what I believe is the first rough draft of history. Silicon Valley ran dry on large breakthroughs in software, so we decided to invent the “blockchain”, a simulacrum of innovation that organically fermented from the anti-institutional themes in the Western zeitgeist to spawn an absurdly large asset bubble with absolutely nothing at the center. There is no there there, and crypto morphed into a pure speculative mania which attracted a fanatic quasi-religious movement fueled by gambling addiction and the pseudo-intellectual narrative economics of the scheme. All conversation around crypto is now simply the sound and fury of post-hoc myth making to rationalize away the collective incoherence of the bubble in a near perfect exemplar of the motivated reasoning of economic determinism.&lt;/p&gt;
&lt;p&gt;On the most basic human level, I suspect our generation still hasn’t processed our shared collective trauma of the 2008 Global Financial Crisis and our anger is now still coming out in very unhealthy ways. With crypto we’ve decided to do the most American thing ever, to commoditize our rage at the financial system into a financial product. Because after all, we’re just temporarily embarrassed millionaires and the only problem with CDOs wasn’t the moral hazard, but that you didn’t have a piece of the action. This time you have a choice, but I suspect history is going to have the same lesson to teach us about the perils of greed untempered by reason.&lt;/p&gt;
</description>
      <pubDate>Sun, 09 Jan 2022 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/disconnect/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/disconnect/</guid>
    </item>
    
    <item>
      <title>Why Democrats Should Oppose Crypto</title>
      <description>&lt;h1 id=&quot;why-democrats-should-oppose-crypto&quot; tabindex=&quot;-1&quot;&gt;Why Democrats Should Oppose Crypto&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;(Updated: 2025-05-15)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The rise of cryptocurrency has spawned a deeply corrupting political machine in Washington that poses a grave threat to American democracy. Through an army of well-funded lobbyists, &lt;a href=&quot;https://www.opensecrets.org/political-action-committees-pacs/fairshake-pac/C00835959/donors/2024&quot;&gt;dark money PACs&lt;/a&gt;, and &lt;a href=&quot;https://newrepublic.com/article/195001/democrats-crypto-genius-act&quot;&gt;captured politicians&lt;/a&gt;, the crypto industry has metastasized from a fringe technology movement into a corrosive force eating away at our democratic institutions. Digital assets, hawked to a public with utopian promises of obsolete central banking, ubiquitous financial inclusion, and individual monetary supremacy, have revealed their putrid reality: gambling and grift tailor-made for our post-truth, social media-addled era. We are witnessing, with horrifying clarity how our democratic institutions are being captured by a new generation of grifters, con artists, and criminals. And one of them is our President.&lt;/p&gt;
&lt;p&gt;Let&#39;s be brutally honest. Bitcoin, the supposed genesis of this revolution, was conjured in 2008. In tech years, that&#39;s practically prehistoric—barely younger than the iPhone, positively ancient compared to Apple Pay. For seventeen agonizing years, crypto advocates have shrieked from the rooftops that blockchain tokens will imminently displace conventional finance with widespread legal use cases. &amp;quot;Any day now,&amp;quot; they crow. Yet, &amp;quot;any day&amp;quot; stubbornly refuses to arrive. &lt;strong&gt;At this point, nearly two decades in, there are still no—I repeat, NO—significant legal use cases for cryptocurrency.&lt;/strong&gt; This isn&#39;t for lack of trying. El Salvador, in a fit of national delusion, made a catastrophic push to ram Bitcoin down its citizens&#39; throats, making taxes payable in it, subsidizing digital wallets, and strong-arming businesses into accepting it. The result? A complete, &lt;a href=&quot;https://www.economist.com/finance-and-economics/2025/03/02/el-salvadors-wild-crypto-experiment-ends-in-failure&quot;&gt;unmitigated bust&lt;/a&gt;. The dream of El Salvador as a crypto-investment paradise has imploded, leaving it to pivot, bizarrely, into the Gulag business.&lt;/p&gt;
&lt;p&gt;And what of the supposed &lt;a href=&quot;https://www.wired.com/story/theres-no-good-reason-to-trust-blockchain-technology/&quot;&gt;&amp;quot;blockchain&amp;quot; technology&lt;/a&gt; that underpins all this, separate from the speculative tokens? Financial services companies have had fifteen long years to explore any alleged legal, non-token &amp;quot;blockchain&amp;quot; uses. There is nothing stopping them from a regulatory perspective, if there&#39;s no public offering of a security token. Companies like JP Morgan, Citibank, NASDAQ, The Clearing House, and DTCC have dutifully experimented with it. The verdict? If there&#39;s no speculative token attached, it&#39;s just a clunky, inefficient database. This kind of &amp;quot;blockchain&amp;quot; or &amp;quot;tokenization&amp;quot; software is demonstrably less efficient than simple existing systems, offering zero practical upside. So-called &amp;quot;blockchain use cases&amp;quot; are a phantom, a marketing gimmick, and certainly not something that requires bespoke laws or warrants a single second of Congress&#39;s time. Blockchain is a dead-end technology, it is for all intents and purposes completely useless in a regulated environment.&lt;/p&gt;
&lt;p&gt;To the inevitable, tired refrain from crypto lobbyists to &amp;quot;not stifle innovation,&amp;quot; the only sane Democratic response must be a derisive, &amp;quot;What innovation? Can you point to it? Is this &#39;innovation&#39; in the room with us right now?&amp;quot; After seventeen years of broken promises and spectacular implosions, the &amp;quot;innovation&amp;quot; crypto offers is primarily in the fields of unregistered securities, industrial-scale money laundering, and new, more efficient ways to fleece the desperate and the gullible. For the Democratic base, and indeed for most of the American public who aren&#39;t blinded by a get-rich-quick fantasy, crypto isn&#39;t an innovation to be nurtured; it&#39;s a tired, corrupt joke, and it&#39;s long past we treated it as such.&lt;/p&gt;
&lt;p&gt;On top of that, crypto exchange structures reveal a profound &lt;a href=&quot;https://bettermarkets.org/analysis/setting-the-record-straight-on-crypto-ftx-sam-bankman-fried-jamie-dimon-the-sec-and-cftc-and-the-revolving-door/&quot;&gt;non-compliance with U.S. securities law&lt;/a&gt; and market regulation, specifically concerning the vertical integration of functions that are deliberately segregated in traditional financial markets. Crypto platforms routinely consolidate the roles of exchange, broker-dealer, proprietary market-maker, custodian, and often, the issuer of their own tokens. They frequently extend their operations to include clearing and settlement, high-leverage lending, and even venture capital investment in the very assets they facilitate for trading. This aggregation of roles is fundamentally at odds with a decades-old legislative and regulatory framework designed to mitigate systemic risk, prevent fraud, ensure fair dealing, and protect investor assets. The deliberate separation of these functions in traditional markets—such as the New York Stock Exchange not engaging in proprietary trading or custody—is a cornerstone of market integrity, specifically implemented to avoid the severe conflicts of interest that are, by design, endemic to the current crypto exchange model.&lt;/p&gt;
&lt;p&gt;The inevitable consequence of this pervasive vertical integration is a range of acute conflicts of interest that U.S. securities laws are expressly designed to prevent. For instance, an exchange&#39;s affiliated proprietary trading desk, possessing non-public order flow information (as exemplified by the relationship between FTX and Alameda Research), has both the incentive and the capability to engage in practices such as front-running client orders or manipulating liquidity, contravening core tenets of fair market access and best execution obligations. The commingling and misuse of customer assets, where client funds are improperly accessed or utilized by affiliated entities without explicit consent—as starkly demonstrated in the FTX collapse—directly violate established custodial duties and SEC rules which mandates the segregation and safeguarding of customer funds and securities. Furthermore, the common lack of robust internal controls creates significant risks of insider trading, while the practice of exchanges listing their own tokens or accepting payments for listings without objective, transparent criteria prioritizes the exchange&#39;s financial gain over client interests and market fairness. The structure of crypto exchanges does not represent an evolution in financial markets, but rather a regression from established legal safeguards, creating an environment where conflicts of interest systematically disadvantage participants and undermine overall market integrity.&lt;/p&gt;
&lt;p&gt;The entire edifice of crypto exchanges is built upon the exploitation of the very conflicts of interest inherent in their vertically integrated stack. Strip away their ability to simultaneously act as exchange, broker, proprietary trader, custodian, and issuer—to self-deal, front-run clients, commingle funds, and manipulate markets through opaque token listings—and what remains is an uncompetitive, inefficient shell, unable to sustain itself without these predatory advantages. Given that this industry demonstrably adds no net value to the real economy, and instead functions primarily as a speculative casino, the notion that it deserves a bespoke regulatory carveout—effectively a license to continue these abuses—is preposterous. Its very existence in its current form is predicated on these conflicts; without them, there is no economically justifiable &amp;quot;there&amp;quot; there.&lt;/p&gt;
&lt;p&gt;The political targeting and subsequent defeat of Senator Sherrod Brown, orchestrated by the crypto industry, should serve as a stark wake-up call regarding the sector&#39;s burgeoning political force. Deploying $40 million, these industry advocates successfully unseated the Senate Banking Committee Chair, a thirty-year stalwart champion of consumer protection and financial regulation. This substantial investment supported his Republican challenger, an avowed crypto advocate, and this outcome not only contributed to a shift in Senate control but also powerfully demonstrated the crypto sector&#39;s rapidly escalating political influence. The campaign against Brown exemplified the industry&#39;s clear-cut strategy: identifying and targeting critics for removal while vigorously backing pro-crypto candidates. The campaign against Brown was a textbook example of the industry&#39;s thuggish, binary worldview: politicians are either useful idiots to be lavishly funded or obstacles to be ruthlessly eliminated. The message to every other public servant was brutally clear: cross the crypto cartel, and they will come for your seat.&lt;/p&gt;
&lt;p&gt;Crypto is a rabidly libertarian project, fueled by the economic desolation and precarity wrought by thirty years of rising inequality, and now synthesized with the financialization of populist rage. It masquerades as a champion of individual freedom and decentralization, but in reality, it actively undermines the institutions and social contracts that are the very bedrock of personal liberties in liberal democracies. By aggressively promoting a system that prioritizes unregulated, anonymous transactions, crypto advocates deliberately foster an environment ripe for industrial-scale exploitation and catastrophic instability. This isn&#39;t just precarious; it&#39;s a monstrous deception when peddled as a path to financial liberation. The grim truth is that it concentrates wealth and power into the grasping hands of a new technocratic elite, leaving the vast majority cannon fodder for market volatility and sophisticated financial manipulation. The promise of crypto as a great equalizer is a cruel mirage, obscuring a far more insidious engine of economic disparity and social atomization.&lt;/p&gt;
&lt;p&gt;The primary allure of cryptocurrency, its supposed democratizing potential, is a bald-faced lie. The narrative that circumventing traditional banking will democratize finance is logically incoherent. Crypto assets are a zero-sum speculative casino—meaning that for anyone to make money, someone else must lose an equal amount—not a new form of money or a store of value. There is no fundamental economic value to crypto assets outside this unregulated gambling den. Crypto doesn&#39;t grow the economy; it is a voracious siphon, draining wealth from the unsuspecting poor directly into the pockets of the predatory rich—a design feature, not a bug. Democrats should focus on industries that expand the economic pie, not on legitimizing rent-seeking, zero-sum gambling schemes.&lt;/p&gt;
&lt;p&gt;From the perspective of Democratic values—which should champion social responsibility, economic equity, and institutional integrity—the crypto movement&#39;s unfettered, laissez-faire savagery is anathema. Regulations exist for good reasons: to ensure markets are fair, transparent, and accountable, and to protect investors from the rampant fraud and manipulation that are crypto&#39;s lifeblood. Cryptocurrency&#39;s inherent illiberality is most starkly revealed in its gleeful facilitation of an unregulated shadow market where nefarious actors thrive in open defiance of the rule of law. This anarchic cesspool, eagerly sheltered by opportunistic gulf autocracies and tax havens, is a breeding ground for money laundering, tax evasion, &amp;quot;pig butchering&amp;quot; scams, and the financing of global criminal enterprises. Crypto directly guts our sanctions regime and wages war against the foreign policy and geopolitical interests of the United States by offering a financial autobahn for our adversaries. Crypto is the tool of the autocrat, the strongman, the kleptocrat, and the criminal.&lt;/p&gt;
&lt;p&gt;Then there are stablecoins—crypto&#39;s grimy back-alley for moving money, primarily to offshore casinos and other shadowy enterprises. We already possess robust, legal mechanisms for money transmission that don&#39;t punch gaping holes in KYC/AML compliance or conjure new, unregulated money market funds teetering on the brink of a run. If our current payment systems need an upgrade, it should come through transparent reform, not by legitimizing shady offshore money transmitters with documented ties to organized crime, hostile foreign powers, or &lt;a href=&quot;https://www.theguardian.com/commentisfree/2025/may/07/trump-crypto-democrats&quot;&gt;enriching the Trump family&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The adoption and promotion of cryptocurrency inherently endorse fiscal policies that guarantee economic instability. Cryptocurrencies are, by their very nature, engineered for extreme, stomach-churning volatility. This is the polar opposite of the economic stability Democrats should be fighting for. The capricious, casino-like nature of crypto markets foments widespread financial precarity, especially for economically vulnerable populations lured by the siren song of impossible returns. Thus, the promise of financial liberation morphs insidiously into the specter of economic ruin, all cynically disguised as populist engagement.&lt;/p&gt;
&lt;p&gt;It&#39;s no accident that crypto has become a cultural lodestar for Silicon Valley libertarians, Barstool conservatives, the MAGA mob, and QAnon conspiracy theorists. For years, FOX News and the like have been hawking gold coins, snake-oil diet pills, and financial doomerism as a perpetual grift machine. Crypto is merely the latest, shiniest iteration of these scams. Donald Trump launching his own memecoin while his sons establish shady crypto ventures trading on their father&#39;s name and office? Shocking, said no one ever. All these ideologies revel in anti-establishment, anti-state, and anti-democratic nihilism, and crypto is the perfect conduit for channeling that populist rage and those ill-gotten dollars into a speculative inferno. The crypto bubble inflates and sustains itself on a brew of conspiracy theory, financial doomerism, widespread financial illiteracy, and a seemingly endless supply of fresh marks for the social media grift economy.&lt;/p&gt;
&lt;p&gt;From a broad perspective, the crypto movement is a machine that takes gambling on one side and outputs Republican campaign contributions and radicalized young men on the other. Its rise is intrinsically tied to an emergent strain of libertarian populism that corrosively eats away at democratic institutions. Operating outside any meaningful regulatory oversight, the crypto market champions a virulent ideology of radical individualism that pulverizes collective social norms and responsibilities. This financialization of populist rage is a clear and present danger, sowing discord and societal fragmentation. Crypto evangelists frame their crusade in explicitly adversarial terms against &#39;the establishment&#39;—vilifying centralized banking and, by extension, the democratic governance structures that provide vital oversight. This narrative breeds distrust and disillusionment with the very institutions essential for a functioning society. It is a childish, puerile anti-system politics, hell-bent on destruction, incapable of building or reforming—the absolute antithesis of what the Democratic Party should stand for.&lt;/p&gt;
&lt;p&gt;Even more disconcertingly, the demographic most susceptible to these narratives is composed of those already battered and disenfranchised by existing economic systems. Crypto-evangelists prey on these vulnerabilities, pitching crypto as a panacea. But as any serious inquiry shows, this &amp;quot;liberation&amp;quot; is a cruel hoax: crypto is predatory inclusion, cloaking itself in the language of empowerment to more efficiently bleed a vulnerable populace dry. These individuals are ensnared in speculative mania, gambling addiction, and rampant scams, deepening their financial precarity.&lt;/p&gt;
&lt;p&gt;Apologists will whine that opposing cryptocurrency stifles innovation and economic freedom. This is preposterous. It fails to distinguish between genuine financial innovation and vacuous speculative bubbles built on dead-end technology. True financial innovation solves real-world problems, boosts economic efficiency, and promotes inclusive growth—benchmarks cryptocurrency has spectacularly failed to meet for over a decade. Instead, the crypto industry has excelled only at creating casinos and baroque financial instruments that obscure risk and exploit regulatory loopholes.&lt;/p&gt;
&lt;p&gt;By opposing crypto, Democrats aren&#39;t fighting progress; they are defending the integrity of our financial system and protecting the vulnerable from predatory schemes. The immense resources and talent currently being squandered on crypto could be redirected to technologies addressing &lt;em&gt;actual&lt;/em&gt; societal challenges: climate change, healthcare, sustainable energy. A firm stance against cryptocurrency is a vote for technological advancement aligned with public interest and long-term societal well-being, not short-term speculative orgies for a parasitic few.&lt;/p&gt;
&lt;p&gt;The insidious claim that &amp;quot;crypto is here to stay&amp;quot;—often paired with the equally lazy &amp;quot;you can&#39;t put the toothpaste back in the tube&amp;quot;—is a thought-terminating cliché designed to excuse regulatory inaction. It&#39;s akin to asserting that because malaria persists, we should abandon all efforts to treat patients or control its spread. While global eradication of a malign influence like crypto may be unrealistic, the United States has a clear duty to aggressively mitigate its harms to our citizens and financial system. Passivity or, worse, capitulation to its supposed inevitability, is not a responsible policy; it&#39;s a surrender to its predatory nature.&lt;/p&gt;
&lt;p&gt;Let&#39;s call crypto what it is: a mechanism to exploit the precariat, a massive wealth transfer from the poor to the rich, from the financially illiterate to the operators of digital casinos. Public policy on crypto must be rooted in an unshakeable recognition of its fundamental incompatibility with stable capital markets, social justice, economic stability, and democratic governance. The environmental devastation of crypto mining, the grotesque perpetuation of wealth inequality, and the systematic undermining of governmental oversight all point to a future of socio-economic anarchy—a dystopia diametrically opposed to democratic ideals. Crypto populism, alongside the rise of authoritarians like Putin, Orbán, and Trump, must be opposed with every fiber of our being.&lt;/p&gt;
&lt;p&gt;The Republican Party&#39;s assault on American democracy is not hypothetical—it is an existential crisis unfolding in real time. Democrats must abandon any lingering illusions that they are engaged in genteel political discourse or friendly competition. They are in a war. And to win a war, one must cut off the enemy&#39;s supply chain. That means confronting and constraining cryptocurrency, which has emerged as the primary pipeline of dark money fueling the Republican Party&#39;s authoritarian agenda.&lt;/p&gt;
&lt;p&gt;The Democratic Party&#39;s mandate should be clear: to rigidly reject the rise of authoritarianism through crypto&#39;s encroachment upon our society and starve its capacity to fuel the political forces arrayed against us.&lt;/p&gt;
</description>
      <pubDate>Sat, 01 Jan 2022 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/democrats_crypto/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/democrats_crypto/</guid>
    </item>
    
    <item>
      <title>The Case Against Crypto</title>
      <description>&lt;h1 id=&quot;the-case-against-crypto&quot; tabindex=&quot;-1&quot;&gt;The Case Against Crypto&lt;/h1&gt;
&lt;p&gt;(This article is a summary of my thoughts on crypto assets from 2020. &lt;a href=&quot;https://www.stephendiehl.com/posts/against_crypto_2025&quot;&gt;I&#39;ve updated it for 2025&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;These days so much of my free time is booked with calls to explain to people outside the software industry why crypto assets are such a destructive force and why I support forceful regulation to halt this financially corrosive enterprise from spreading further into markets.&lt;/p&gt;
&lt;p&gt;I basically have to repeat myself on the basic arguments for every call covering the same basic monetary theory, American history and technical limitations. Thus I’m going to summarize the basic argument so we have a reference and I don’t have to keep repeating myself all day.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The technology does not solve a real problem.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The crypto project has had 13 years to try and find a problem to solve. It has not found one.&lt;/p&gt;
&lt;p&gt;The real world has fundamental constraints that make the technology unworkable, whenever it has to interact with the outside world the benefits of decentralization disappear and the solutions end up simply recreating slower and worse versions of processes and structures that already exist.&lt;/p&gt;
&lt;p&gt;Despite that, for the last thirteen years these projects have done nothing but scam people by creating synthetic asset bubbles for gambling and destroying the environment. There are fundamental limitations to the scalability of blockchain-based technologies, and every use case is better served by another simpler technology except for crime, ransomware, extralegal gambling, and sanctions evasion; all of which are a drain on society not a benefit. Taken as a whole the technology has no tangible benefits over simply using trusted parties and centralized databases.&lt;/p&gt;
&lt;p&gt;Crypto coins are simply speculative gambling products that only create a massive set of negative externalities on the world. It is introducing artificial volatility into markets untethered to any economic activity and creates an enormous opportunity cost where the only investment opportunity is as an economically corrosive synthetic hedge against all productive assets. This is not innovation, this is technical regression and flirtation with ecological disaster in a time when we cannot afford to gamble our planet’s fate on pyramid schemes and dog memes.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;So called “cryptocurrencies” aren’t actually currencies, and cannot fulfil the function of money.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Money exists to exchange for goods and services in an economy. It is created to mediate the exchange of goods so that we have a common unit of account we can trade instead of bartering goods directly. Money needs to have a reliable and stable value compared to a domestic basket of common goods and services, in order to achieve that the supply of the money needs to be controlled by a monetary authority which can expand or contract the supply according to market fluctuations.&lt;/p&gt;
&lt;p&gt;A dynamic money supply is a fundamental necessity for a modern economy. A small amount of inflation discourages hoarding and incentivizes investment into productive enterprises which grow the economy and produce prosperity. Conversely a static fixed money supply encouages hoarding, and is inflexible in times of crisis because it does not allow intervention. Economies do not stabilize themselves and require active intervention to curb recessions.&lt;/p&gt;
&lt;p&gt;In an environment in which multiple currencies can commingle there is a perverse incentive to create counterfeit currency or to create parallel currencies. Counterfeit currencies dilute trust in commerce, create counterparty risk and catalyze crime. Parallel currencies introduce exchange risk and create artificial barriers to commerce. The optimal solution within any economic region is to thus have a single currency with a single authority to control the supply, protect against counterfeiting and lower barriers to commerce by discouraging other systems through creating demand. The only possible entity that can fulfil this role is the State and it creates demand for a single currency by requiring citizens to extinguish their obligations to the state in that currency. A single currency and single monetary authority is the inevitable role of the state because of its singular monopoly on taxation and justice.&lt;/p&gt;
&lt;p&gt;Historically commodity-based money (so called “hard money”) was based on backing by metals and was used extensively in the 18th and 19th century. Instead of vesting power in democratic controls, it instead vested power in non-elected international parties who could source, mine and mint metals. Under a gold standard, inflation, growth and the financial system were all less stable due to trade imbalances. This led to frequent recessions, larger swings in consumer prices and perpetual banking crises. When these events occurred in one part of the world, the distress would be transmitted more quickly and completely to others and thus created a politically unstable, unequal and more violent world. We saw this in the Gilded Age of the 1870s to 1920s in which hard money created a world of massive wealth inequality, thus ultimately leading up to the speculative market manias that lead to the Great Depression. The United States ultimately devalued its currency with the policies of the New Deal which slowly decoupled the dollar’s dependence on gold and which led to an era of economic growth and prosperity. Conversely Europe largely did not engage in these corrective policies and this era saw the rise of populist strong men and fascists who promised to correct the wealth inequality of the common man, and ultimately plunged the continent into the most violent period in human history.&lt;/p&gt;
&lt;p&gt;Money is always going to be inseparable from politics. As much as some libertarians want to believe that value should be determined by a God-given order independent of the will of men, they cannot escape the logical and historical contradictions at the heart of this idea. The fixed-supply ideas of deflationary coins like Bitcoin fundamentally misinterpret the properties of fiat money as bugs when they are in fact features. The crypto project contains unresolvable logical and economic contradictions in its stated purpose. State controlled money embeds control and accountability for fiscal stability and market intervention in the democratic process where it inevitably and rightly belongs.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;The history of private money is one of repeated disasters that destroy public trust.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Even playing devil’s advocate and assuming cryptocurrency could function as money—which they can’t—we come up against the hard limitation that everytime private money has been tried in history it creates a form of corporate feudalism coupled to a toxic environment that encourages fraud and discourages commerce. The lessons of history are quite clear on this issue because the United States flirted with such a system back in the Free Banking Era from 1837 to 1863. In this time period there were hundreds of private entities that went about issuing their own private bank notes allegedly created one-for-one with state bonds.&lt;/p&gt;
&lt;p&gt;The problem with these so-called wildcat banks is that their reserves were not always verifiably backed and were thus subject to runs on the bank in which customers could not access their funds. The second issue is that unlike public money which is universally accepted at par, the wildcat bank notes had a massive secondary exchange market where notes from different banks would not trade at par. A dollar note from Wyoming bank could be worth $0.60 to a note from a Nebraska bank and these values would fluctuate depending on market conditions. As a merchant this would make business rather complicated as you would be forced to purchase goods in one set of notes, accept notes from customers and give change in a different set of notes. This was great for bankers who had access to non-public information and could arbitrage these notes for their own profits, but for the average person it was a terribly predatory and exploitative system. Private bank notes are a needlessly complicated, risky and inefficient way to run an economy and this was remedied by the National Bank Act of 1863. It was a truly terrible idea.&lt;/p&gt;
&lt;p&gt;History tends to rhyme with itself, and today we are flirting with the same bad ideas of the past. Except now instead of wildcat banks we have wildcat tech platforms with the same aspirations. They don’t want to interface with public money, they want to become issuers of private money themselves. A fully vertically integrated form of company scrip that they issue to their investors, employees and customers to create not just a walled garden, but a walled garden where every path has a toll booth that takes only their coin. The elephant in the room that no venture investor in these projects wants to talk about is that creating private money, just like in the wildcat banking era, is a license to print money by creating markets for these coins/notes with massive position and information asymmetries baked into the design. These kinds of private money regimes are just as exploitative today as they were in the 1800s, and the so-called “web3” notion of embedding this form of institutionalized corruption as a first class structure into the internet is a terrible idea that ignores the lessons of history.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Crypto assets are all unregistered securities.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When we logically deconstruct the crypto narrative by tossing out the phoney populism and cult-like structure of faith in economic absurdities, we end up with an inescapable conclusion that fits firmly within our existing regulatory framework. Crypto assets are simply unregistered securities on ventures whose stated aspiration is to develop technology to become digital wildcat banks. They’ve just synthesized their corporate equity and alleged notes into one financial product.&lt;/p&gt;
&lt;p&gt;Cryptocurrencies aren’t currencies and have no mechanism to ever become currencies. They are effectively unregulated securities where the only purpose of the products is price appreciation untethered to any economic activity. The only use case is gambling on the random price oscillations, attempting to buy low and sell high and cash out positions for wins in a real currency like dollars or euros. Yet crypto cannot create or destroy real money because unlike a stock there is no underlying company that generates income. So if you sell your crypto and make a profit in dollars, it’s exactly because a greater fool bought it at a higher price than you did. So every dollar that comes out of a cryptocurrency is because a later investor put a dollar in. They are inherently zero-sum by design, and when you take into account the casino (i.e. exchanges and miners) taking a rake on the game then the entire structure becomes strictly negative-sum. For every winner there are guaranteed to be multiple losers. It’s a game rigged by insiders by hacking human psychology.&lt;/p&gt;
&lt;p&gt;For cryptocurrency to have any real utility, the volatility needs to cool off. If that were to happen, there would be little reason for the public to speculate on cryptocurrency prices, given that there would no longer be the potential for massive returns. The smart money exits, the liquidity disappears and the bubble collapses. This is the inevitable fate of all cryptocurrencies, and we see this reflected in the simple fact that the median return on all these thousands of flash-in-the-pan coins is zero. Every crypto coin is just on a random walk to zero by a different path.&lt;/p&gt;
&lt;p&gt;The argument laid out in this article is a quite complicated edifice, and requires a large amount of knowledge at the intersection of several fields of study that, quite frankly, the public should not have to concern themselves with learning to safeguard themselves against fraud. Public money should just work for most people without them having to be concerned with the details. This is ultimately where cryptocurrencies tap into the ignorance, desperate faith in technical solutionism and political resentment of the public and weaponize it for the aims of these libertarian private money charlatans to engorge themselves. These guys aren’t building a new financial system, they’re just lining their own pockets.&lt;/p&gt;
&lt;p&gt;History repeats itself first as tragedy and then as farce. The wild economic oscillations of yesterday’s gold standard is today’s dog meme mania. Human nature is remarkably invariant through the ages and if we don’t learn the lessons of history then we’re doomed to repeat the mistakes of past generations. This time around If we’re very lucky then crypto assets simply end in a market crash and a series of progressive New Deal-like reforms to our financial system. If we’re unlucky then they accelerate the expansion of a shadow financial system used to enrich the already wealthy, increase wealth inequality to historically unprecedented levels, decrease faith in democracy and further fan the flames of populism. These trends ultimately converge to leave humanity’s fate to the wild oscillations of market manias, charismatic demagogues and strong men who promise to save us from ourselves. And we’ve seen how that story ends.&lt;/p&gt;
</description>
      <pubDate>Fri, 31 Dec 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/against_crypto/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/against_crypto/</guid>
    </item>
    
    <item>
      <title>Enterprise Eldritch Horrors</title>
      <description>&lt;h1 id=&quot;enterprise-eldritch-horrors&quot; tabindex=&quot;-1&quot;&gt;Enterprise Eldritch Horrors&lt;/h1&gt;
&lt;p&gt;Sure the Lovecraftian horrors are cosmicly horrific, but as all of us who work in software know, the true horrors are closer to home. Look not in forgotten tombs, but within the flickering, non-Euclidean interfaces of your corporate mandated software. These are the true Great Old Ones, their influence seeping into every cubicle, their incomprehensible UIs driving mortals to the brink.&lt;/p&gt;
&lt;h2 id=&quot;serv-n&#39;ow%2C-the-formless-gatekeeper-of-endless-tickets&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;Serv-N&#39;Ow, The Formless Gatekeeper of Endless Tickets&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Known to its cultists (the IT department) as &amp;quot;The Solution,&amp;quot; Serv-N&#39;Ow is a vast, amorphous entity whose true form is never seen, only experienced through its myriad, ever-shifting portals of &amp;quot;Request Forms.&amp;quot; To appease Serv-N&#39;Ow, one must perform the sacred ritual of &amp;quot;Submitting a Ticket,&amp;quot; a process involving arcane dropdown menus and text fields that demand sacrifices of specific, often unknowable, information. Its domain is the &amp;quot;Fulfillment Plane,&amp;quot; where requests for new staplers and access to forbidden shared drives languish for eons, occasionally prodded by its hapless servitors chanting, &amp;quot;Have you checked the knowledge base?&amp;quot; Its approval workflows are labyrinthine corridors that twist the mind, and its SLA timers tick with the inexorable dread of a cosmic clock counting down to... well, probably just another automated email. &lt;em&gt;Iä! Iä! Serv-N&#39;Ow fhtagn!&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;t&#39;eems%2C-the-many-angled-messenger-of-cacophony&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;T&#39;eems, The Many-Angled Messenger of Cacophony&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;This entity, once a mere whisper, has grown into a cyclopean horror that seeks to integrate all communication into its chaotic embrace. T&#39;eems manifests as an ever-expanding void of channels, chats, and &amp;quot;Teams,&amp;quot; each a shimmering portal to a different flavor of distraction. Its pings and notifications are the maddening piping of unseen flute-players, luring users into its depths where productivity goes to die. It whispers of &amp;quot;collaboration&amp;quot; while ensnaring souls in endless video calls where its lesser avatars (your colleagues, their faces distorted by low bandwidth) stare blankly, occasionally offering cryptic utterances like &amp;quot;You&#39;re on mute&amp;quot; or &amp;quot;Can you see my screen?&amp;quot; Its tendrils reach into every other application, promising a unified experience but delivering only a more complex web of potential failure points.  &lt;em&gt;Ph&#39;nglui mglw&#39;nafh T&#39;eems Redmond wgah&#39;nagl fhtagn!&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;s&#39;aap%2C-the-ancient-modular-one-whose-runes-baffle-all&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;S&#39;aap, The Ancient Modular One Whose Runes Baffle All&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Older than the stars, S&#39;aap is a monolithic horror whose architecture was laid down in forgotten eons by beings whose minds were utterly alien to human comprehension. Its &amp;quot;Modules&amp;quot; are like distinct, cyclopean cities within its vast, incomprehensible domain, each governed by arcane &amp;quot;T-Codes&amp;quot;—glyphs of power that only its high priests (overpaid consultants) can truly decipher. Mortals who dare to interact directly with S&#39;aap often find themselves lost in its non-Euclidean menus, where a single misplaced click can unleash untold financial or logistical chaos. It dreams in spreadsheets and speaks in error messages that offer no solace, only cryptic warnings of &amp;quot;unspecified system events.&amp;quot; To behold its raw interface is to glimpse the indifferent void at the heart of enterprise resource planning. &lt;em&gt;Yog-Sothoth knows the T-Code, Yog-Sothoth &lt;em&gt;is&lt;/em&gt; the T-Code!&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;j&#39;raa%2C-devourer-of-sprints%2C-master-of-the-endless-backlog&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;J&#39;Raa, Devourer of Sprints, Master of the Endless Backlog&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;This chittering, multi-limbed horror thrives on the ritualistic sacrifice of &amp;quot;Story Points&amp;quot; and &amp;quot;Tasks.&amp;quot; J&#39;Raa&#39;s domain is the &amp;quot;Backlog,&amp;quot; a bottomless abyss from which user stories and bug reports emerge, gibbering for attention. Its cult, the &amp;quot;Agile Development Team,&amp;quot; performs elaborate ceremonies known as &amp;quot;Sprint Planning&amp;quot; and &amp;quot;Stand-ups,&amp;quot; hoping to appease the beast and inch forward its many &amp;quot;Epics.&amp;quot; J&#39;Raa&#39;s true hunger, however, is for the very concept of &amp;quot;Done,&amp;quot; a state it eternally pushes just beyond reach. Its myriad customizable fields and workflow states are like the scales of a squamous beast, each one a potential point of confusion and maddening debate during &amp;quot;Retrospectives.&amp;quot; &lt;em&gt;The Backlog is deep, and full of terrors! Tekeli-li! Tekeli-li!&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;sael&#39;esfor%2C-the-all-seeing-eye-of-the-customer-relationship&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;Sael&#39;Esfor, The All-Seeing Eye of the Customer Relationship&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;From its cloudy vantage point, Sael&#39;Esfor gazes upon all &amp;quot;Leads,&amp;quot; &amp;quot;Opportunities,&amp;quot; and &amp;quot;Accounts&amp;quot; with an unblinking, multifaceted eye. This entity demands constant tribute in the form of data entry, promising its devotees (the Sales Team) visions of &amp;quot;Closed-Won&amp;quot; glory. Its &amp;quot;Objects&amp;quot; and &amp;quot;Fields&amp;quot; can be customized into an infinitely complex web, a testament to its shapeless, ever-adapting horror. Woe betide the mortal who fails to log a call or update a &amp;quot;Next Step,&amp;quot; for Sael&#39;Esfor&#39;s reports are unforgiving, revealing all shortcomings to its managerial servitors. It whispers sweet promises of 360-degree customer views, but often delivers a fragmented chimera stitched together from incomplete data and wishful thinking.  &lt;em&gt;In its office in San Francisco, dread Sael&#39;Esfor waits dreaming of quotas.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;o%C3%BCt-l&#39;ook%2C-the-chronovore-of-unending-correspondence&quot; tabindex=&quot;-1&quot;&gt;&lt;strong&gt;Oüt-L&#39;ook, The Chronovore of Unending Correspondence&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;A truly insidious horror, Oüt-L&#39;ook manifests as a gaping maw of an &amp;quot;Inbox,&amp;quot; constantly disgorging missives from the void—urgent requests, passive-aggressive CCs, and newsletters from forgotten subscriptions. Its other primary appendage, the &amp;quot;Calendar,&amp;quot; is a grimoire of despair, its pages filled with overlapping rituals known as &amp;quot;Meetings,&amp;quot; each designed to drain the psychic energies of its attendees. To stare into its depths is to confront the relentless, soul-crushing tide of corporate obligation, a current that pulls one ever further from the shores of actual, productive work. Its &amp;quot;Rules&amp;quot; and &amp;quot;Filters&amp;quot; are but feeble wards against the deluge, often misfiring and consigning vital communications to the forgotten dimensions of the &amp;quot;Junk&amp;quot; folder.  &lt;em&gt;That is not dead which can eternal lie, and with strange aeons even emails may die (in your archive).&lt;/em&gt;&lt;/p&gt;
</description>
      <pubDate>Sun, 09 May 2021 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/enterprise_eldritch/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/enterprise_eldritch/</guid>
    </item>
    
    <item>
      <title>Exotic Programming Ideas, Part 2 (Term Rewriting)</title>
      <description>&lt;h1 id=&quot;exotic-programming-ideas%3A-part-2-(term-rewriting)&quot; tabindex=&quot;-1&quot;&gt;Exotic Programming Ideas: Part 2 (Term Rewriting)&lt;/h1&gt;
&lt;p&gt;Continuing on from our series last week, this time we’re going to discuss the topic of term rewriting. Term rewriting is a very general model of computation that consists of programmatic systems for describing transformations (called rules) which transform expressions called terms into other sets of terms.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_01&quot;&gt;Week 1 - Module Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_02&quot;&gt;Week 2 - Term Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_03&quot;&gt;Week 3 - Effect Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_04&quot;&gt;Week 4 - Datalog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most widely used rewriting engines often sit at the heart of programs and languages used for logic programming, proof assistants and computer algebra systems. One of the most popular of these is Mathematica and the Wolfram language. This is a proprietary programming language that is offered by the company Wolfram Research as part of a framework for scientific computing. At the core of this system is a rewrite system that can manipulate composite symbolic expressions that represent mathematical expressisons. Other languages such as Maude, Pure, Aldor, and some Lisp variants implement similar systems but for the purpose of our examples we will use code written in Mathematica to demonstrate the more general concept.&lt;/p&gt;
&lt;p&gt;Mathematica is normally run in a notebook environment, where the In and Out expressions are vectors indexed by a monotonically increasing index for each line entered at the REPL.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;Wolfram Language 12.0.1 Engine for Linux x86 (64-bit)
Copyright 1988-2019 Wolfram Research, Inc.

In[1]:= 1+2
Out[1]= 3

In[2]:= 1+2+x
Out[2]= 3 + x

In[3]:= List[x,y,z]
Out[3]= {x, y, z}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The language itself is spiritually a Lisp that operates on expressions, which can include symbolic terms (such as the x term above). These expressions are not variables but irreducible atoms that stand for variables in expressions. Composite expressions are similar to Lisp’s S-expressions and consist of a head term and a set of nested subexpressions which are arguments to the expressions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;F[x,y]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case F is the head and x and y are arguments. This form of notation is known as an M-expression. The key notation is that all language constructs are themselves symbolic expressions and can be introspected and transformed by rules. For example the Head function extracts the head symbol from a given expression.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[4]:= Head[f[a,b,c]]
Out[4]= f

In[5]:= Symbol[&amp;quot;x&amp;quot;]
Out[5]= x

In[6]:= Head[x]
Out[6]= Symbol
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The arguments for a given M-expression can be extracted using the Level function which extracts the arguments of the given depth as a vector (denoted by braces). In this case the first level is the arguments a, b and c.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[7]:= Level[f[a,b,c],1]
Out[7]= {a, b, c}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Expressions can be written in multiple forms which reduce down to this base M-expression form. For example the addition operation is simply syntactic sugar for an expression with a Plus head. We can reduce this expression to its internal canonical form using the FullForm function. Multiplication is also denoted in infix by separating individual terms by spaces similar to convention used common algebra.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[8]:= FullForm[x+y]
Out[8]//FullForm= Plus[x, y]

In[9]:= FullForm[x y]
Out[9]//FullForm= Times[x, y]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to infix syntactic sugar the two operators @ and // can be used to apply functions to arguments in either prefix or postfix form respectively.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[10]:= Sin[x] (* Full form *)
Out[10]= Sin[x]

In[11]:= Sin @ x (* Prefix sugar *)
Out[11]= Sin[x]

In[12]:= x // Sin (* Postfix sugar *)
Out[12]= Sin[x]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Complex mathematical expressions are simply nested combinations of terms. For example a simple monomial expression $x^3+(1+x)^2$&lt;/p&gt;
&lt;p&gt;would be given by:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[13]:= FullForm[x^3 + (1 + x)^2]
Out[13]//FullForm= Plus[Power[x, 3], Power[Plus[1, x], 2]]

In[14]:= TeXForm[x^3 + (1 + x)^2]
Out[14]//TeXForm= x^3+(x+1)^2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This expression itself is a graph structure, and it’s implementation consists of a set of nodes with pointers to its subexpressions. The index notation using brackets can be used to directly manipulate a given subexpression which are enumerated left to right, starting at the head term.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[15]:= f[a,b,c][[0]]
Out[15]= f

In[16]:= f[a,b,c][[1]]
Out[16]= a

In[17]:= f[a,b,c][[2]]
Out[17]= b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Individual arguments to expressions can be reordered and substituted into other expressions be referencing their slots by index.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[18]:= F[#3, #1, #2] &amp;amp; [x, y, z]
Out[18]= F[z, x, y]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Complex mathematical expressions can be expressed in terms of graphs of expressions and can be rendered in a graphical tree form. Effectively all rules and functions in this system are transformations of directed graphs to other directed graph structures. For example a simple trigonometric series can be expressed symbolically as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[19]:= Series[Cos[x]/x, {x, 0, 10}]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This has the internal representation as this graph structure of M-expressions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[20]:= FullForm[Series[Cos[x]/x, {x, 0, 10}]]
Out[20]//FullForm= SeriesData[x, 0, {1, 0, Rational[-1, 2], 0, Rational[1, 24], 0, Rational[-1, 720], 0, Rational[1, 40320], 0, Rational[-1, 3628800], 0, Rational[1, 39916800]}, 0, 11, 1]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This symbolic expression can be expanded into individual terms by evaluating the Series term into individual terms.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;sum_{x=0}^{10} &#92;frac{&#92;cos x}{x} = &#92;frac{1}{x} - &#92;frac{x}{2} + &#92;frac{x^3}{24} - &#92;frac{x^5}{720} + &#92;frac{x^7}{40320} - &#92;frac{x^9}{3628800} + O(x^{11})&lt;br /&gt;
$$&lt;/p&gt;
&lt;h2 id=&quot;evaluation-order&quot; tabindex=&quot;-1&quot;&gt;Evaluation Order&lt;/h2&gt;
&lt;p&gt;Evaluation in the language proceeds by reduction of terms until there are no more rules that apply to the reduced expression. Such an expression is said to be in normal form. This mode of evaluation differs from sequential, statement-based languages where sequential updates to memory and the sequencing of effectful statements is the mode of execution.&lt;/p&gt;
&lt;p&gt;For example the Range function produces a series of values as a list. This list can itself be passed to another function which applies a function over each element, such as the querying the primality of a given integer.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[20]:= Range[1,10]
Out[20]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

In[21]:= Map[PrimeQ, Range[1,10]]
Out[21]= {False, True, True, False, True, False, True, False, False, False}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This sequence of booleans is the final form, no subsequent rules in scope can be applied and the final expression is returned.&lt;/p&gt;
&lt;h2 id=&quot;rules&quot; tabindex=&quot;-1&quot;&gt;Rules&lt;/h2&gt;
&lt;p&gt;The primary mechanism of introducing new user-defined logic to the environment is the introduction of rewrite rules. These are defined by either a Rule definition or it’s infix sugar -&amp;gt;. The left-hand side is given in pattern syntax and may bind variable definitions over the right-hand side.&lt;/p&gt;
&lt;p&gt;For example the rule that writes all occurrences of the integer 1 to 2.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[22]:= Rule[1,2]
Out[22]= 1 -&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or the rule that transforms each explicit occurance of the symbol x into the symbol y.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[23]:= Rule[x,y]
Out[23]= x -&amp;gt; y
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pattern variables can also be introduced which match on subexpression and name them in the scope of the rule. The resulting match on this “hole” is bbound as a variable name over the right-hand side of the rule. For example the rule that matches any quantity x_ and binds this to the definition x.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[24]:= Rule[x_,x]
Out[24]= x_ -&amp;gt; x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;More usefully these pattern variables can occur within expressions. So for instance if we wanted to rewrite any argument given to the sine function as a cosine function, we could write the following rule:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[25]:= Sin[x_] -&amp;gt; Cos[x]
Out[25]= Sin[x_] -&amp;gt; Cos[x]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rules can be applied to a given expression using either ReplaceAll function and it’s infix form /. applies a given rule to the expression on the left.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[26]:= ReplaceAll[x, Rule[x,y]]
Out[26]= y

In[27]:= x /. Rule[x,y]
Out[27]= y
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In our trigonometric example from above, we can rewrite nested trigonometric functions from within their expression body.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[28]:= Sin[a+b] /. Sin[x_] -&amp;gt; Cos[x]
Out[28]= Cos[a + b]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rules can be combined by passing a list of rules as an argument to ReplaceAll. For example the following does a single pass over the given list and rewrites all x to y and all y to z.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[29]:= {x,y,z} /. {x-&amp;gt;y, y-&amp;gt;z}
Out[29]= {y, z, z}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since the rules are applied in order, to the given intermediate expression of the last rule, this does not transform the entire list into the same element. However the function ReplaceRepeated will apply a given set of rules repeatedly until a fix point is reached and the term is identical the last set reduction.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[30]:= ReplaceRepeated[x, Rule[x,y]]
Out[30]= y

In[31]:= ReplaceRepeated[x, {Rule[x,y], Rule[y,z]}]
Out[31]= z

In[32]:= {x,y,z} /. {x-&amp;gt;y, y-&amp;gt;z}  (* Non-repeated *)
Out[32]= {y, z, z}

In[33]:= {x,y,z} //. {x-&amp;gt;y, y-&amp;gt;z}  (* Repeated *)
Out[33]= {z, z, z}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is quite possible to define a set of rules for which this process simply repeats ad infinitum and does not converge. This has no normal form either blows up in time (or space) or until the runtime decides to give up.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[34]:= {1,2} //. {1 -&amp;gt; 2, 2 -&amp;gt; 1}
ReplaceRepeated::rrlim: Exiting after {1, 2} scanned 65536 times.
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;algebraic-transformations&quot; tabindex=&quot;-1&quot;&gt;Algebraic Transformations&lt;/h2&gt;
&lt;p&gt;Many sets of rules admit multiple rewriting paths that do not necessarily result in the same final term. A system of rules in which the ordering of the rewrites are chosen does not make a difference to the eventual result is called confluent. The prescription on evaluation order used inside the rewrite rules inside of the runtime. This is arbitrary choice, however Mathematica evaluates the head of an expression and then evaluates each element of the expression. These elements are in themselves expressions and the same evaluation procedure is recursively applied.&lt;/p&gt;
&lt;p&gt;Within the standard library there are many sets of these transformations for working with algebraic simplifications and term reordering. The function FullSimplify is the most general and will collect and simplify real-valued algebraic expressions according to a enormous corpus of idenities and relations it has defined internally.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[35]:= FullSimplify[x^2 x + y x + z + x y]
Out[35]= Plus[Power[x, 3], Times[2, x, y], z]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However we can introduce and extend these rule sets ourselves. For example if we wanted to encode a new simple set of rules for simplifying trigonometric functions according to given identity we could write a rule for the following math identity.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;sin(&#92;alpha + &#92;beta) = &#92;sin &#92;alpha &#92;cos &#92;beta + &#92;cos &#92;alpha &#92;sin &#92;beta&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;cos(&#92;alpha + &#92;beta) = &#92;cos &#92;alpha &#92;cos &#92;beta - &#92;sin &#92;alpha &#92;sin &#92;beta&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This would be encoded as the following two pattern and rewrites:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[36]:= ElemTrigExpand = {
  Sin[a_ + b_] -&amp;gt; Sin[a] Cos[b] + Cos[a] Sin[b], 
  Cos[a_ + b_] -&amp;gt; Cos[a] Cos[b] - Sin[a] Sin[b] 
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This could then we applied to trigonometric functions of linear arguments repeatedly to reduce the nested terms to a single variable.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[37]:= Sin[a+b+c] //. ElemTrigExpand
Out[37]= Cos[a] (Cos[c] Sin[b] + Cos[b] Sin[c]) Sin[a] (Cos[b] Cos[c] + Sin[b] Sin[c])
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;attributes-%26-properties&quot; tabindex=&quot;-1&quot;&gt;Attributes &amp;amp; Properties&lt;/h2&gt;
&lt;p&gt;Terms that occur within rules can also have additional structure which rules can dispatch on. Symbols can have defined Attributes which can inform reductions and reorderings of symbols into canonical forms according. Often those common algebraic properties like associativity, commutativity, identities and annihilation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[38]:= Attributes[f] = {Flat, OneIdentity}
Out[38]= {Flat, OneIdentity}

In[39]:= f[1, f[2, f[3, 4]]]  (* Associative *)
Out[39]= f[1, 2, 3, 4]

In[40]:= f[f[1]]  (* Idempotent *)
Out[40]= f[1]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to structural properties of functions, symbols can be enriched with assumptions about their origin and the domain or set from which they are defined. For exmaple whether a given number is nonzero, integral, prime, or a member of a given set. These properties are themselves symbolic expressions, so for example if we to encode the assumption that a given variable z is in the complex plane:&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
z &#92;in &#92;mathbb{C}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;We could write the following expression, where Complexes is a given builtin standing for $&#92;mathbb{C}$.&lt;/p&gt;
&lt;p&gt;These can be added to either the global assumption set which is defined by the variable &lt;code&gt;Assumptions&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[42]:= $Assumptions = {a &amp;gt; 0, b &amp;gt; 1}
Out[42]= a &amp;gt; 0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rewrite such algebraic simplifications will dispatch on the assumptions of terms encountered in redexes and take different reduction paths based on chains of deductions about the properties of symbols. For example the identity that the square root of a square reduces to identity dependencies on the sign of the given symbol. Using the Refine function we can incorporate the global assumptions to tell the simplifier which branch to choose.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[43]:= Refine[Sqrt[a^2 b^2]]
Out[43]= a b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The truth value of a predicate over a symbol can be queried using this function as well. This either results in a True, False or indeterminate answer when the symbol has no local assumptions over it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[44]:= Refine[Positive[b]]
Out[44]= True

In[45]:= Refine[Positive[x]]
Out[45]= Positive[x] (* Has no truth value *)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Many functions also take a named Assumptions parameter which can be used to introduce local assumptions over the arguments given a to a function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[46]:= Refine[Sqrt[x^2], Assumptions -&amp;gt; {x &amp;lt; 0}]
Out[46]= -x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The assumption is capable of doing limited first order resolution in order to deduce properties of symbols within certain domains. For example the assumption x &amp;gt; 1 will imply the fact that x is non-zero and not negative. Or the assumption than an integer is y &amp;gt; 2 and prime implies that y is not even.&lt;/p&gt;
&lt;p&gt;Patterns on the left hand side of rules can also dispatch both on the type of an expression and on assumptions and attributes of pattern variables. For example the LHS x_Symbol matches any symbolic quantity (but not numbers) can rewrites it to a given constant. Note that this matches on the head of the List itself and rewrite the head to a constant.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[47]:= {1, x, Pi} /. x_Symbol -&amp;gt; 0 (* Matches List, x, Pi *)
Out[47]= 0[1, 0, 0]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Predicate functions can be wrapped around pattern variables by passing a _?s suffix followed by the function. For example to check whether a given term is a numerical quantity we can use the NumericQ function. This will match on the Pi and 1 terms but not the List or 1 terms.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[48]:= {1, x, Pi} /. x_?NumericQ -&amp;gt; 0 (* Matches 1, Pi *)
Out[48]= {0, x, 0}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;special-functions&quot; tabindex=&quot;-1&quot;&gt;Special Functions&lt;/h2&gt;
&lt;p&gt;Within the standard library there is a vast corpus of mathematical knowledge encoded in a library of assumptions and rules over symbolic quantities. For example the Riemann zeta function has hundreds of known idenities and representations in terms of other functions. For even integer quantities is a simple closed form expressed in terms of π and Bernoulli numbers.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;zeta(2) = &#92;frac{&#92;pi^2}{6}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;Mathematica is aware of these identities and will chose to transform the Zeta function to this reduced form automatically. For non-even values there is no simple closed form (in general) but there is a series approximation that can be computed to an arbitrary number of digits using the N function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[49]:= Zeta[2]
Out[49]= Times[Rational[1, 6], Power[Pi, 2]]

In[50]:= Zeta[3]
Out[50]= Zeta[3]

In[51]:= N[Zeta[3], 30]
Out[51]= 1.20205690315959428539973816151
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;calculus&quot; tabindex=&quot;-1&quot;&gt;Calculus&lt;/h2&gt;
&lt;p&gt;The famous XKCD cartoon illustrates the asymmetry of certain calculus problems. This cartoon finds direct representation when trying to encode simplifications over derivatives and integrals within these rewrite systems.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://imgs.xkcd.com/comics/differentiation_and_integration.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Indeed, differentiation is almost trivial to encode using the techniques from above. The linearity of the differential operator, product rule, power rule and constant rule can be written in four lines of rules. The standard library has a larger corpus of these definitions but they are usually about this succinct.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;{
  Diff[f_ g_, x_]             -&amp;gt;  f Diff[g, x] + g Diff[f, x],
  Diff[f_ + g_, x_]           -&amp;gt;  Diff[f, x] + Diff[g, x],
  Diff[Pow[x_,n_Integer], x_] -&amp;gt;  n * Pow[x,n-1],
  Diff[x_, x_]                -&amp;gt;  1
  Diff[n_?NumericQ, x_]       -&amp;gt;  0
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However many computer algebra systems are capable of doing calculations of indefinite integrals through almost magical means. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[52]:= Integrate[Log[1/x], x]
Out[52]= Plus[x, Times[x, Log[Power[x, -1]]]]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While Mathematica’s symbolic integration capacities may seem like magic, they are indeed just the canonical example of rewriting machinery combined a corpus of transformations and some analysis from the 1930s. While there are certain integrals that are computable directly by pattern matching, there is a non-trivial result in the study of the Meijer G-function which admits a transformation of many types of transcendental functions into a convenient closed form to compute integrals.&lt;/p&gt;
&lt;p&gt;The Meijer G-function is an indexed function of 4 vector parameters expressed the following complex integral where L&lt;br /&gt;
runs from $−i&#92;infty$ to $i&#92;infty$.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
G_{m,n}^{p,q}(z &#92;mid &#92;mid a_1, &#92;ldots, a_n, a_{n+1}, &#92;ldots, a_p &#92;mid &#92;mid b_1, &#92;ldots, b_m, b_{m+1}, &#92;ldots, b_q) = &#92;frac{1}{2&#92;pi i} &#92;int_L &#92;prod_{j=1}^m &#92;Gamma(b_j - s) &#92;prod_{j=1}^n &#92;Gamma(1 - a_j + s) &#92;prod_{j=m+1}^q &#92;Gamma(1 - b_j + s) &#92;prod_{j=n+1}^p &#92;Gamma(a_j - s) z^s ds&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;This is well-studied function, and supririsngly a great many classes of special functions (trigonometric functions, Bessel functions, hypergeoemtric, etc) can be expresed in terms in terms of the G functions and thus we can reduce complex nestings of these functions down in terms of simply manipulating G-functino terms. Apply integration theorems over this reduced expression produces one or more other G-function terms. We can then expand that result back into the special functions to get the integration result. This practice does not work universally, but practically in enough cases to be incredibly useful.&lt;/p&gt;
&lt;p&gt;One of the very general indefinte integration theorems rexpressses the integrand in terms of permutations of the G-function indexes. On the left hand side we have an integral expression and on the right hande side we have the computed integral both in terms of G-functions.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;int G_{m,n}^{p,q}(z &#92;mid &#92;mid a_1, &#92;ldots, a_n, a_{n+1}, &#92;ldots, a_p &#92;mid &#92;mid b_1, &#92;ldots, b_m, b_{m+1}, &#92;ldots, b_q) dz = G_{m,n}^{p+1,q+1}(z &#92;mid &#92;mid 1, a_1+1, &#92;ldots, a_n+1, a_{n+1}+1, &#92;ldots, a_p+1 &#92;mid &#92;mid b_1+1, &#92;ldots, b_m+1, 0, b_{m+1}+1, &#92;ldots, b_q+1)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;A entire book of integrals can be reduced down to a simple set of multiplication and transform rules of G-functions in a few lines of term rewrite logic.&lt;/p&gt;
&lt;p&gt;At the core of Mathematica’s integration engine is transformations involving this approach along with a variety of other heuristics that work quite well. For example the MeijerGReduce function can be used to transform many clases of special functions into the G-function equiveleants and then FullSimplify and other identities can reduce these expressions further.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-mathematica&quot;&gt;In[53]:= MeijerGReduce[Sqrt[x] Sqrt[1+x],x]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;sqrt{x} &#92;sqrt{1+x} &#92;quad &#92;Rightarrow &#92;quad&lt;br /&gt;
&#92;frac{- &#92;sqrt{x} &#92;quad&lt;br /&gt;
{ G_{0,0}^{1,1}&#92;left(x&#92;left| &#92;begin{array}{c} &#92;frac{3}{2}, 0 &#92; - &#92; &#92;end{array} &#92;right.&#92;right) }&lt;br /&gt;
}{2 &#92;sqrt{&#92;pi }}&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The confluence of all of these features gives rise to a programming environment that is tailored for working with and abstract expressions in a way that differs drastically from the semantics of most mainstream languages.&lt;/p&gt;
&lt;p&gt;It is true that this system can be used to program arbitrary logic, it can however become a bit clunky when trying to code imperative logic that works with inplace references and mutable data structures. Nevertheless it is a very powerful domain tool for working in scientific computing and the number of functions related to technical domains that are surfaced in the language represents decades of meticulous expert domain expertise.&lt;/p&gt;
&lt;p&gt;Many universities have site licenses for this software so it is worth playing around with the ideas found in this system if you are keen on programming langauges with heterodox evaluation models and semantics.&lt;/p&gt;
&lt;h2 id=&quot;external-references&quot; tabindex=&quot;-1&quot;&gt;External References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Term-Rewriting-All-Computer-Science/dp/0521301136&quot;&gt;Term Rewriting and All That&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Essence-Strategic-Rewriting-Computer-Science/dp/0521301136&quot;&gt;The Essense of Strategic Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://maude.cs.illinois.edu/&quot;&gt;Maude&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.cs.ox.ac.uk/projects/pured/index.html&quot;&gt;Pure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.wolfram.com/engine/&quot;&gt;Wolfram Engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://reference.wolfram.com/language/guide/Expressions.html&quot;&gt;Mathematica Expressions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Mon, 08 Mar 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/exotic_02/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/exotic_02/</guid>
    </item>
    
    <item>
      <title>Exotic Programming Ideas, Part 4 (Datalog)</title>
      <description>&lt;h1 id=&quot;exotic-programming-ideas%2C-part-4-(datalog)&quot; tabindex=&quot;-1&quot;&gt;Exotic Programming Ideas, Part 4 (Datalog)&lt;/h1&gt;
&lt;p&gt;Continuing on in our series on exotic programming ideas, we’re going to explore the topic of logic programming and a particular form known as datalog.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_01&quot;&gt;Week 1 - Module Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_02&quot;&gt;Week 2 - Term Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_03&quot;&gt;Week 3 - Effect Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_04&quot;&gt;Week 4 - Datalog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Datalog is a very simple formalism, it consists of only two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A database of facts&lt;/li&gt;
&lt;li&gt;A set of rules for deriving new facts from existing facts&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Datalog is executed by a query processor that given these two inputs, finds all instance of facts implied by both the databased and rules. For our examples we’re going to be coding our examples in the Souffle language. The namesake of the language is an acronym for the Systematic, Ontological, Undiscovered Fact Finding Logic Engine. It can be installed simply on many Linux systems with the command:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;apt-get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; souffle
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Souffle is a minimalist datalog system designed for complex queries over large data sets, such as those encountered in the context of doing static program analysis over large codebases. Souffle is implemented in C++ and can compile datalog programs into standalone executables via compilation to C. It is one of the simpler and most efficient implementations of these systems to date, and notably it has a simple type system on top of the logic engine.&lt;/p&gt;
&lt;p&gt;Datalog encodes a decidable fragment of logic for which satisfaction is directly computable over a finite universe of terms. These terms consist of three classes of expressions. A fact represented syntactically by a expression suffixed by a period.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fact&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A rule represented by a head and body term with a turnstile (⊢) symbol between.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;head &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; body&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And query terms which query the truth value of fact within the database.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;?&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; body&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An example of these terms would be “classical” categorical syllogism about the Greek philosopher Socrates. Socrates is a man, all men are mortal, therefore we can deduce that Socrates is mortal.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# All men are mortal / valar morghulis&lt;/span&gt;
mortal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Socrates is a man.&lt;/span&gt;
human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Socrates&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Is Socrates mortal?&lt;/span&gt;
?&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; mortal&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Socrates&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Execution of a Souffle program is in effect a set of transformations from input facts to deduced output facts by a prescription of rules.&lt;/p&gt;
&lt;p&gt;Souffle can read input facts from a variety of sources including stdin, CSV files, and SQLite databases. he default input source is a tab-separated file for a relation where each row in the file represents a fact in the relation. This is specified with a IO input declaration of the form.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl A	&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt; A
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would read from a file A.facts of tab separated values for a symbolic value n. However we can modify our input by passing explicit IO parameters to it instead.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;IO&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; filename&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fact_database.csv&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  delimiter&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;IO&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;stdin&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; delimiter&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;IO&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;sqlite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dbname&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;fact_database.db&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;simple-example&quot; tabindex=&quot;-1&quot;&gt;Simple Example&lt;/h2&gt;
&lt;p&gt;A simple end to end example would be the program that associates a set of symbolic inputs with a set of numerical ones by a simple one-to-one mapping. We could write the simple A to B transformation as the following rules.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Input from A.facts&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl A	&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;input&lt;/span&gt; A

&lt;span class=&quot;token comment&quot;&gt;# Output to B.csv&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl B	&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
B&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
B&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;output B
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This reads from the input facts in A.facts and outputs B.csv which not surprisingly looks like the following when we invoke Souffle with the following command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ souffle hello.dl
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output matches A to B according to the rules in hello.dl.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Hello&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;World&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Relations&lt;br /&gt;
Logic inside of datalog is specified by relations, which are ordered tuples of typed variables that relate quantities. For example if we had a set of named entities (represented as symbols) we can assign a relation that maps a property to each of them as facts.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Klingon&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Android&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

Human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Picard&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Riker&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Klingon&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Worf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Android&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Data&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each variable is given a specific type indicated by var : type where type is one of the following builtins.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;symbol - Symbolic quantities represented as strings.&lt;/li&gt;
&lt;li&gt;number - Signed integers&lt;/li&gt;
&lt;li&gt;unsigned - Unsigned integers&lt;/li&gt;
&lt;li&gt;float - Floating point values&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Relations can take multiple arguments that assign properties to a collection of symbols according to rules over that relation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Peers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Captain&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Picard&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Officer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Riker&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Officer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Worf&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Officer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Data&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rules over these relations are defined by clauses. The following can be read as a pair (x,y) is in A relation, if it is in B relation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clauses in Souffle are known in logic as Horn clauses. They are comprised of subterms joined together with a conjunction (∧) (logical AND in programming) that implies a statement u. Can read the following statement in math as&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; a &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; b &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; … &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; z &lt;span class=&quot;token builtin&quot;&gt;all&lt;/span&gt; hold&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; then also u holds
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;$$&lt;br /&gt;
u &#92;leftarrow a &#92;land b &#92;land &#92;ldots &#92;land z&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;The above is known as the implicative form. Logically this is equivalent to the statement written in terms of disjunction (∨) (logical OR in programming) and negation (¬) (logical NOT in programming).&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;neg a &#92;lor &#92;neg b &#92;lor &#92;ldots &#92;lor z&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;In Datalog notation we write this Horn clause in the following syntax.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;u &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In clauses, free variables that occur inside of the conjoined expressions are implicitly universally quantified, i.e. they are required to hold forall (denoted ∀) substitutions of the variable in a domain. The domain in this case is the set of inhabitants of a given type (i.e. symbol, unsigned, etc).&lt;/p&gt;
&lt;p&gt;So the datalog expression above&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is equivalent to the Horn clause.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;forall x. &#92;forall y. &#92;neg B(x,y) &#92;lor A(x,y)&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;So for example we know that Klingons and Humans both eat food, but obviously androids do not. We write the following rules over a variable term X of type symbol.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Human&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Klingon&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;output Eats
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which produces the following output from the given set of input facts about the symbols in scope given the constraint on their species.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Eats&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Picard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Terms can also be negated, for instance if we wanted to enumerate all officers that eat could write the following rule which excludes androids.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;InvitedToDinner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Officer&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; !Android&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;InvitedToDinner&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A more complex rule can consist of multiple terms separated by command, all of which have to evaluate true. For example two officers are peers if they have same rank and are not the same person. We use an intermediate variable Z to test the equality of rank in this relation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Peers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Peers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; X&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Rank&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  X &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Peers&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Rules can also be recursive. For example we we wanted to write a query which found all first degree friend relations of a given set of facts we could write the following recursive definition.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Friend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl FriendOfFriend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

FriendOfFriend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Friend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;X&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; FriendOfFriend&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;FriendOfFriend&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Riker&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;An important class of relations are that of equivalence relations. These are relations between two terms that satisfy an additional set of constraints: reflexivity, symmetry, and transitivity. Relations of this form behave similar to how the equality symbol is treated in algebra.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
a = a&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
a = b &#92;Rightarrow b = a&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
a = c, b = c &#92;Rightarrow a = c&lt;br /&gt;
$$&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; R&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; R&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; reflexivity
equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; symmetry
equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; transitivity
equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; equivalence&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the implementation of the relation, the data and intermediary deductions are stored in a common B-tree data structure. However this can be modified by passing an explicit argument to force the use of a disjoint-set or trie data structure for specific uses cases.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; eqrel &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Disjoint&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl B&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; btree &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; B&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;tree
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl C&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; brie  &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Dense trie
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When working with integral types (number, unsigned and float) arithmetic expressions behave as expected and can be pattern matched on. For example we can write the Fibonacci function as a relation in terms of itself.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;15.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;type-system&quot; tabindex=&quot;-1&quot;&gt;Type System&lt;/h2&gt;
&lt;p&gt;Souffle has a limited type system that can be used to track the types of variables quantified in clause expressions. For example one cannot have a variable which simultaneously holds a symbol and unsigned integer. We can however define custom type synonyms on top of the ground types of the languages that will let us semantically distinguish different user-defined types of symbols and numeric quantities. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; Person &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; Food &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol

&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Food&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl Dinner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Food&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

Dinner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Well&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;typed
Dinner&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; Eats&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Not well&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;typed&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Person &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; Food
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unions of type synonyms are also permitted so long as the underlying types of the synonyms are equivalent.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; C &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; B    &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Either &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; A &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; B
The language also has the ability to define both &lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; product types which let us both options &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; records &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; terms &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; our clauses&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; Sum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; A &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; B &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; Prod &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; C &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; symbol &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
For example we could build a linked &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; cons &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; out of a product &lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; which has both a head element &lt;span class=&quot;token keyword&quot;&gt;and&lt;/span&gt; a tail element&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Linked &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; List &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; head &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decl MyList&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

MyList&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nil&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Logic programming is a very natural way of expressing abstract program semantics and with tools like Souffle we have excellent logic engine that can be embedded in other static analysis applications to collect facts about code and deduce implications about code composition and usage. This a powerful technique and future static analysis tools are going to yield whole new ways of reasoning about and synthesising code in the future.&lt;/p&gt;
&lt;h2 id=&quot;external-references&quot; tabindex=&quot;-1&quot;&gt;External References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://souffle-lang.github.io/souffle/language-reference.html&quot;&gt;Souffle Language Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.swi-prolog.org/&quot;&gt;SWI-Prolog Online&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/Z3Prover/z3/blob/master/examples/python/muz.py&quot;&gt;μZ in Z3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Z_Z_Z_Z_Z&quot;&gt;Tool Talk: Souffle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Z_Z_Z_Z_Z&quot;&gt;Domain Modeling with Datalog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Thu, 04 Mar 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/exotic_04/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/exotic_04/</guid>
    </item>
    
    <item>
      <title>Exotic Programming Ideas, Part 3 (Effect Systems)</title>
      <description>&lt;h1 id=&quot;exotic-programming-ideas%2C-part-3-(effect-systems)&quot; tabindex=&quot;-1&quot;&gt;Exotic Programming Ideas, Part 3 (Effect Systems)&lt;/h1&gt;
&lt;p&gt;Continuing on in our series on exotic programming ideas, we’re going to explore the topic of effects. Weak forms of effect tagging are found in many mainstream programming languages, however the use of programming with whole effect systems that define syntax for defining and marking regions of effects in the surface syntax is still an open area in language design.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_01&quot;&gt;Week 1 - Module Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_02&quot;&gt;Week 2 - Term Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_03&quot;&gt;Week 3 - Effect Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_04&quot;&gt;Week 4 - Datalog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First, we should define what we mean by an effect. We’ll adopt the definitions commonly used in functional programming discipline, simply because it has a precise definition wheres colloquial usage often does not. First a pure function is a unit of code whose return value is entirely determined by its inputs, and has no observable effect other than simply returning a value. A pure function is a function in programming which behaves a like a function in mathematics. For example in Python code we denote a “function” called f:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the pseudocode traditionally known as mathematics we denote the function f:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;x2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are a few subtle points to mention in this definition. The first is the matter of perspective in our analysis of effects. This Python function compiles into a sequence of bytecodes which manipulate stack variables, mallocs megabytes of data on the heap, invokes the garbage collector, and swaps hundreds of thousands of values in and out of registers on the CPU all corresponding to doing the exponentiation of an arbitrary size integer inside a PyObject struct. All this is very effectful from the perspective of the underlying implementation, however we can’t observe the functioning of these internals from within the normal language.&lt;/p&gt;
&lt;p&gt;The big idea in pure functional programming is that programming will inevitably consist of both pure and effectful (sometimes called impure) logic. Additionally we suppose it is a useful property of the surface language to be able to distinguish between units of logic which have effects, and to be able to classify these type of effects in order to greater reason about program composition.&lt;/p&gt;
&lt;p&gt;The alternative to this is the model found in most languages where all logic is mushed upto in a big soup of effects and relies on the programmer’s intuition and internal mental model to distinguish between which code can do perform effects (memory allocations, side channels, etc) and logic which cannot. The research into effect systems is fundamentally about canonising our intuition about correct program effect synthesis into a formal model that compilers can reason about on our behalf and interact with developer tools in an ergonomic way.&lt;/p&gt;
&lt;p&gt;Functional Languages like Idris, Haskell, F* and a few other research languages have been exploring the design space for the better part of the last decade. Concepts such as monads saw initial exploration for demarcating pure and impure logic but have fallen off in recent years as that model has hit a wall in terms of usefulness. The most common area of active exploration is one known as algebraic effect handlers which admits a tractable inference algorithm for checking effectful logic while not introducing any runtime overhead.&lt;/p&gt;
&lt;p&gt;There are no mainstream languages which use this model, however there is a academic language out of Microsoft Research lab called Koka which presents the most developed implementations of these ideas. As far as I can tell no one uses this language for anything, however it is downloadable and quite usable to explore these ideas. We will write all of our example code in Koka.&lt;/p&gt;
&lt;p&gt;In Koka the absence of any effect is denoted by the effect total. The only result of computing the function f is simply returning the square of its input.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; total &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However we can write a effectful function, such as one that reads from the screen, by tagging it with a console effect. The body of this function can then invoke functions such println and the result of the invocation of thees functions is captured in the signature of the function that invokes them. The return type () denotes the unit type which is called void in C-like languages.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; console &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;I can write to the screen!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is worth noting that the println function provided by the standard library has the following type signature which itself includes the effect.&lt;/p&gt;
&lt;p&gt;fun println( s : string ) : console ()&lt;br /&gt;
And as such the compiler is aware of the effect it carries and the following function can be written without an annotation and effect inference will deduce the appropriate signature without the user having to specify it. The return type can also be inferred using the usual type inference techniques.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;I can write to the screen!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;exceptions&quot; tabindex=&quot;-1&quot;&gt;Exceptions&lt;/h2&gt;
&lt;p&gt;Besides input/output, the most common type of effect found in most programming is the ability to fail. Usually langauge runtimes will implement this functionality using some exceptions which perform a non-local jump to logic which handles the exception or unwinds the call stack and aborts. This is clearly an effect that we can model and we can create an interface similar to checked exceptions found in other languages.&lt;/p&gt;
&lt;p&gt;The throw function will take an error sum type and result in a effect marked by exn. While the try function will consume a function which results in exn type and return an error. The error type is either an Error or a Ok on successful execution.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;type&lt;/span&gt; error&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Error&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; exception &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; exception &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  Ok&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; result &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The error handling functions can be written as higher order functions that consume and handle functions taged with the exn effect.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun throw&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; err &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; error&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; exn a
fun &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; action &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;exn&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; e error&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
fun maybe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; t &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; error&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; maybe&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example we can do classic case of handling division of mero and wrapping up the arithmetic error in a maybe sum type which handles the zero case with a nothing.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun divide&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; exn &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; throw&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Division by zero&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    _ &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

fun safeDiv&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; maybe&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  maybe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; divide&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Elaboration of pattern matching inside the compiler can deduce incomplete patterns and infer that an exception should be added to the type of the pattern match that can fail at runtime.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun unjust&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; m &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; maybe&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; exn a &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Just&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; x
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Whereas a complete pattern match is deduced as total.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun unmaybe&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; m &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; maybe&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; default &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; total a &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Just&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; x
    Nothing &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; default
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;non-termination-is-an-effect&quot; tabindex=&quot;-1&quot;&gt;Non-termination is an Effect&lt;/h2&gt;
&lt;p&gt;By our above definition about effects, the only observable result of invoking a function is return a resulting value. Therefore functions which do not compute a value, and run infintely are not functions and have a side-effect called divergence. Deducing whether a given function is total is non-trivial in the general case, however a function which is the composition of units of logic which are all independently total must itself be total.&lt;/p&gt;
&lt;p&gt;There are many simple cases where we can immedietely deduce non-totality from simply analysising call-sites. For example the following function is automatically tagged with the div effect since it recurses on itself.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun forever&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  forever&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The forever combinator has the inferred type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;forever&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; forall&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The effect checker can deduce totality across mutually recusive definitions, so functions that invoke each other must themselves either be entirely total or possibly diverge on composition.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun f1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; div &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; f2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

fun f2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; div &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; f1&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rows-of-effects&quot; tabindex=&quot;-1&quot;&gt;Rows of effects&lt;/h2&gt;
&lt;p&gt;While tagging individual effects independently is useful in its own right, programing in the large requires us to compose logic together and thus we need a way to synthesize the combination of effects. In Koka this is represented as a row of effects. This is denoted with the bracket syntax:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the language of mathematics, effect rows are commutative monoids with an operation extension denoted by the pipe and a neutral element (total or &amp;lt;&amp;gt;) representing the absence of effects. The commutative and associativity of the extension operation allows for a canonical ordering of effects in signatures.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; e2     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e2&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e2&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; e3  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e3&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&gt;&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; e1     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;  
&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; e1&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;e1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;e2&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example we can write down a function which invokes a random number generator with the non-determinism effect ndet as well as raising an exception with the effect exn. The synthesis of the two is now the row &amp;lt;ndet, exn&amp;gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun multiEffect&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ndet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; exn&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  val n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; srandom&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  throw&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;I raise an exception&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The effect system denotes functions which may diverge or throw exceptions as pure with the following alias.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;alias pure &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;exn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;div&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the Haskell approach to effects there is a single opaque IO monad which inhabits any action which can perform any type of console input, output or system operation. However languages which richer effect systems can model the IO hierarchy in much more granularity. For example Koka defines the following three tiers of IO effects in increasing expressivity.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Functions that perform arbitrary IO operations
&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; but are terminating without raising exceptions
alias io&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ndet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;console&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;net&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;ui&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;st&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;global&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Functions that perform arbitrary IO operations
&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; but &lt;span class=&quot;token keyword&quot;&gt;raise&lt;/span&gt; no exceptions
alias io&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;noexn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;io&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;total&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Functions that perform arbitrary IO operations&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
alias io &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;exn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;io&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;noexn&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;reference-lifetimes-%26-boundaries&quot; tabindex=&quot;-1&quot;&gt;Reference Lifetimes &amp;amp; Boundaries&lt;/h2&gt;
&lt;p&gt;State is an essential part of programing, and it is one which is inherently effectful. Importantly we’d like to be talk about which regions of memory or logic we are able to write to within a given scope. For this we need to be able to refer to effects over a given region of memory as a paramter to the effect. The language allows us to parameterise effects with a heap parameter using bracket notation. There are three core stateful effects provided by the standard library:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;alloc&amp;lt;h&amp;gt;&lt;/code&gt; - The alloc effect for allocating references over heap parameter h.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;read&amp;lt;h&amp;gt;&lt;/code&gt; - The read effect from a reference from a heap parameter h.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;write&amp;lt;h&amp;gt;&lt;/code&gt; - The write effect for writing to a reference on heap parameter h.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To create and manipulate references there are three core functions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun ref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; value &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;alloc&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; ref&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
fun &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; ref &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ref&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; assigned &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;write&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
fun &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;!&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; ref &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ref&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;read&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;e&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; a
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Functions which no observable leakage of internal references can be marked as total if the types of references are not referenced in either the argument types or the return type. Thus local state can be embedded inside pure functions. For instance the following function is total even though internally it uses mutable references:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun localState&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; total &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  val z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;!z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The compiler itself also has a level of syntactic sugar for working with references. The &lt;code&gt;val&lt;/code&gt; introduces an immutable named variable, however the &lt;code&gt;var&lt;/code&gt; syntax can be used to define a mutable reference concisely.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;val z &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ref&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;     
var z &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Identical to above
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Variables can be updated using &lt;code&gt;:=&lt;/code&gt; operator.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; 
z &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;            &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; Identical to above
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thus we can write pseudo-imperative logic like the following counter function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun bottlesOfBeer&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  var i &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
  &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;99&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    i &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;References can be passed as arguments to functions and heap parameters can be quantified as type variables, allowing us to write generic functions which operate over references of any type.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun add&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;refs&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; a &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ref&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ref&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; st&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    a &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;!a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; !b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The combination of the ability to read, write, and allocate is given the name &lt;code&gt;st&lt;/code&gt; in the standard library to denote stateful computations.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;alias st&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;read&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;write&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;alloc&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;h&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using this kind of effect system for tracking references gives us a powerful abstraction for denoting regions of logic that have read barriers and write barriers and separating mutation from pure logic in a way that is machine checkable. Future languages that had this kind of information at compile-time could use it to inform compilation of regions of local mutation into more efficient efficient code with still maintaining guarantees about the boundaries of pure logic.&lt;/p&gt;
&lt;h2 id=&quot;effect-polymorphism&quot; tabindex=&quot;-1&quot;&gt;Effect polymorphism&lt;/h2&gt;
&lt;p&gt;Finally we’d also like to be able to write higher order functions which can take arguments which are either effectful or pure and incorporate their effects as part of the types of output. The common map function is a higher-order function which takes a list and applies a given function argument over each element of the list. To write this in a language with an effect system we need to be able to refer to the effect of the function argument as a type variable (e in this example) and use it the output type for map.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; xs &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; e b &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; e &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;b&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the case where we apply a total arithmetic function over the list, we simply get back a list of ints. While in the IO case we can apply a function like println which will individually print each integer out to the console, and resulting type is a list of units. Both use cases are subsumed by the same function using this parametric polymorphism over the abstract effect type variables.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;val e1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; println&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; console &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
val e2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; dbl&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;fun dbl&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; total &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is still early days for effect system research. The key takeaway that I would like to push for future work is the observation that languages which aim to improve the ergonomics and performance of effect modeling cannot simply push the entire system into a library. There needs to be language-level support for both integrated effect types and annotations in the surface language for labeling subexpressions and giving hints to type inference. A lot of approaches to do this in Haskell, Scala, etc are inevitably doomed to poor ergonomics by this simple fact.&lt;/p&gt;
&lt;p&gt;There is a whole new level of static analysis tools that can be built by not just inferring types, but providing a whole new level of static information on top of our code that is otherwise tossed out by the compiler. This is a very exciting technique and I hope it bears more fruit in the coming decade.&lt;/p&gt;
&lt;h2 id=&quot;external-references&quot; tabindex=&quot;-1&quot;&gt;External References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://fstar-lang.org/tutorial/&quot;&gt;F* Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.eff-lang.org/&quot;&gt;Eff Programming Langauge&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://koka-lang.github.io/koka/doc/index.html&quot;&gt;Koka Language Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.microsoft.com/en-us/research/publication/algebraic-effects-for-functional-programming/&quot;&gt;Algebraic Effects for Functional Programming - Daan Leijen&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/koka-lang/koka&quot;&gt;Koka Github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Wed, 03 Mar 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/exotic_03/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/exotic_03/</guid>
    </item>
    
    <item>
      <title>Exotic Programming Ideas, Part 1 (Module Systems)</title>
      <description>&lt;h1 id=&quot;exotic-programming-ideas%3A-part-1-(module-systems)&quot; tabindex=&quot;-1&quot;&gt;Exotic Programming Ideas: Part 1 (Module Systems)&lt;/h1&gt;
&lt;p&gt;All successful programming languages are alike; every unsuccessful programming language is unsuccessful in its own way.&lt;/p&gt;
&lt;p&gt;The history of software is one that runs parallel to a simultaneous but separate conversation, and that is the story of how we think about computation and how we communicate those ideas with other humans. And the story of computation has been about the evolution of this very novel and peculiar form of human expression we call code. I suspect being a programmer in the 21st century must be like what being a royal scribe was like in Ancient Egypt in 3200 BCE. There’s this new modality of communication that most of the population is unaware of, yet it’s existence simultaneously enables commerce, culture and civilization to flourish.&lt;/p&gt;
&lt;p&gt;And just like with natural languages we’ve seen a convergence of semantics and syntax into what is becoming like Latin was for European languages. These languages fall into the paradigm of C-inspired imperative paradigm with some object oriented features. This describes languages like C++, Go, Python, Ruby, Rust, Java, and Javascript that together make up the bulk of all new code that is written today. While most of programming activity has converged onto this local maxima of productivity, there are ideas at the fringes of our programming that contain more esoteric ideas with wildly different semantics and grammar than those found in the mainstream.&lt;/p&gt;
&lt;p&gt;Throughout my life as professional programmer, I’ve realized there are two different groups of programmers. Those that see programming languages primarily as an instrument of human reason and those that see as a means of production for specific tasks. Just like there are few limited speakers of Pirahã, Navajo, Klingon or Berik; there are programming languages with similarly limited adoption. Nonetheless these languages encode some very interesting semantic structures that are often lost in translation when we try to encode in them in the mainstream.&lt;/p&gt;
&lt;p&gt;For my Advent Blogging (because I’m bored and in lockdown), going to write about seven language semantics features at the fringes of software culture. The ones with the wacky wild ideas that will take you on a trip through what might had been if the Romans (i.e C) hadn’t conquered.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_01&quot;&gt;Week 1 - Module Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_02&quot;&gt;Week 2 - Term Rewriting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_03&quot;&gt;Week 3 - Effect Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/exotic_04&quot;&gt;Week 4 - Datalog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;module-systems&quot; tabindex=&quot;-1&quot;&gt;Module Systems&lt;/h2&gt;
&lt;p&gt;Let’s start with module systems and modular programming. The big idea of modules is to break code up into reusable components called modules. Modules as a language feature were first developed in Modula-2 and Pascal, which were developed as a way to demarcate units of compilation. The notion found maturity in the Standard ML language in 1984 which developed the notion further and allowed abstraction and parameterisation of modules. notion. These days full module systems are found in languages within the ML family such as F#, OCaml, Standard ML although a few others such as Agda have them as well. We’ll look at OCaml which is a statically typed ML dialect with type inference.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module MyModule = struct 
  (* Set of Definitions *)
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The information contained in a module can be both values and types. For example the type t and the function of a single argument square.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module MyModule = struct 
  type t = int 
  let square x = x * x
end
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module MyModule = struct 
  type t = int 
  let square x = x * x
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The components of a module can be bound to a description known as signature which constrains both the visibility of symbols within the module and enforces a consistent interface across the module.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module MyModule : sig
 val square : int -&amp;gt; int
end = 
struct
  let square x = x * x
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively module signatures can be bound to specific name at the typelevel and defined independently across multiple modules using the module type syntax. This separates from the specification from the implementation. Here we define an abstract type t that can be referenced inside the signature abstractly to refer to its eventual instantiation with a concrete type. The term s is defined to be a “value of type t” in the specification.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;(* Specification of MySig *)
module type MySig = sig 
  (* Set of Type Signatures *)
  type t 
  val s : t 
end

(* Implementation of MySig *)
module MyModule : MySig = struct 
  (* Set of Definitions *)
  type t = int 
  let s = 0 
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Modules can be opened using the open syntax. This will scope all exposed symbols within the module inside the given scope or globally if used at the toplevel.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;open MyModule;;                (* Toplevel *)
let open MyModule in expr;;    (* In let binding *)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Modules can also be projected into using dot syntax to retrieve specific symbol within a module’s scope. For example:`&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module Foo = struct
  let a = 5
  let b = 6
end;;

print_int(Foo.a);;
print_int(Foo.(a*b));;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The signature of a module need not neccessarily constrain all symbols defined in the module. A signature may seal the implementation of specific parameters or types that implementation details internal the module and not exposed publically. For example in the following module the Hello signature hides the message used by the hello function and does not allow the downstream user to modify the internals of the message itself, only to the call the function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module type Hello = sig
 val hello : unit -&amp;gt; unit
end

module Impl : Hello = struct
  (* Private variable message *)
  let message = &amp;quot;Messaage is sealed Inside Impl&amp;quot;
  let hello () = print_endline message
end;;

Impl.hello();;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Modules themselves can also be nested within other modules, to an arbitrary depth. Projection into the Outer module can retrieve the Inner module and its contents in this example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module Outer = struct
  let a = 1
  module Inner = struct 
    let b = 2 
  end
end

let c = Outer.Inner.b;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to nested, modules can include the contents of other modules either by temporarily scoping their values in scope within the module’s definition or by using the include syntax which copy’s the contents of given module’s scope into the new definition.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module A = struct
  let a = 10
end

module B = struct
  let b = 20
end

module Arith1 = struct
  include A
  include B
  let c = a+b
end

module Arith2 = struct
  open A
  open B
  let c = a+b
end;;

Arith1.a;;
Arith2.a;; (* Not in scope *)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A module itself can be parameterised by types, including other modules. These are known as functors and allow us to instantiate modules by specifying the interface that given module parameters must conform. In this example the parameter M is an abstract module that conforms to the signature S. There is an implementation of this signature by the given module A which can be passed as the parameter M in the instantiation of FA. Within the definition of F we project into M to retrieve the s and t parameters abstractly. Functors are effectively functions from modules to modules.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module type S = sig
  type t 
  val s : t 
end

module A : S = struct 
  type t = int
  let s = 0
end

(* Functor *)
module F (M : S) = struct
  type a = M.t
  let b = M.s
end

(* F applied to module A *)
module FA = F(A);;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Normally two invocations of the same functor will produce modules containing equal abstract types. However we can define a different class of functor known as a generative functor which is denoted by an extra parameter () which will produce an output module containing non-equal abstract types. In this example the output types of F and G are distinct. This is particularly important when dealing with references which require mutation and uniqueness.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot;&gt;module G (M : S) () = struct
  type a = M.t
  let b = M.s
end

module GA = G(A)();;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the essence of module systems. While many languages have simple ways of namespacing and encapsulating units of code, the ML family enriches this by making the modules themselves both first order objects and parametrisable. This is a very powerful technique that is rarely seen elsewhere and the notion of programming with functors is one that gives rise to a rich set of abstractions for code reuse. Module systems are essential part of functional programming and future languages should learn from and explore new branches in this rich design space.&lt;/p&gt;
&lt;p&gt;External References&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.cs.cornell.edu/courses/cs3110/2019sp/textbook/modules/modular_programming.html&quot;&gt;Cornell CS3110&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Practical-Foundations-Programming-Languages-Robert-ebook/dp/B00B4V6AB2&quot;&gt;Practical Foundations for Programming: Chapter 42&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
      <pubDate>Mon, 01 Mar 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/exotic_01/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/exotic_01/</guid>
    </item>
    
    <item>
      <title>Gamestop, Bitcoin and the Commoditization of Populist Rage</title>
      <description>&lt;h1 id=&quot;gamestop%2C-bitcoin-and-the-commoditization-of-populist-rage&quot; tabindex=&quot;-1&quot;&gt;Gamestop, Bitcoin and the Commoditization of Populist Rage&lt;/h1&gt;
&lt;p&gt;Many articles written about the GameStop story mark the event as a watershed moment in the markets and an epic tale of the establishment vs the little man. I won’t tell you these things. In fact I assert the GameStop story is relatively inconsequential. This story does however hold up a mirror to a more profound truth. A deeper story about the rage that’s boiling just beneath the surface of modern life and how a new class of people have learned to tap into and release that pressure for their own personal gain. It’s the story of how Gamestop and bitcoin are symptoms of same collective neurosis and how, through technology, nihilism and rage have been turned into a tradeable commodity.&lt;/p&gt;
&lt;p&gt;Let’s state the uncomfortable truth. The future for many Americans is bleak and their lives are going to be nasty, brutish and short. Everyday I see a world where many in my generation have simply given up all hope for opportunity of a family, a house, a stable career and forced to confront an uncertain future in a world that is slowly boiling itself to death.&lt;/p&gt;
&lt;p&gt;Most of the stories we tell ourselves concern how to just cope with life in the presence of escalating Malthusian struggle. And millennials are desperate for stories that give them a sense of hope for a future of prosperity and a return to the lives their parents once led. And truth be told, we don’t have those answers yet.&lt;/p&gt;
&lt;p&gt;There’s a simple inescapable truth that is not lost on people who have studied the economics of the situation. Millennials are suffering because they are poor and many will never recover from back to back crises of 2008 and the global pandemic. The last ten years have seen a massive redistribution of wealth from this age bracket into older demographics.&lt;/p&gt;
&lt;p&gt;It’s not surprising that when it comes to their investment strategies, that the “you only live once” philosophy and a preference for high-risk high-return investments is preferred. What has this generation got to lose?&lt;/p&gt;
&lt;p&gt;And so we see Gamestop story fall out of that. In a nutshell a bunch of retail investors (possibly with help from institutional insiders) decided to pump a small cap stock far beyond what the valuation of the company should be. The whole setup requires a significant number of people to buy and hold the asset at a price that makes no sense economically. And in doing so they have locked each other in a game of musical chairs in which they collectively have to pour more and more money into this unsustainable scheme in order to maintain the bubble.&lt;/p&gt;
&lt;p&gt;This is a horrible prisoners dilemma in which a bunch of retail investors, many of whom can’t afford to play this dangerous game, are sustaining this “protest bubble” at their own expense, under the illusion of one of two (or possibly both) narratives: a) they will make obscene wealth from it b) they are sending a giant middle finger to the establishment. Neither of which are true in any meaningful sense.&lt;/p&gt;
&lt;p&gt;Equities markets are efficient and the majority of people who joined this bubble late are all but guaranteed to lose money. But you know who is making a killing off all this fiasco? Goldman Sachs, Citadel, Andersen Horrowitz those guys are making transaction fees, getting on the other side of your trades and facilitating Robinhood’s public offering. And they don’t give a damn about timing this bubble because they make money in either direction. The temporary inflation of a failing microcap stock for a few days is completely inconsequential to the larger market.&lt;/p&gt;
&lt;p&gt;What’s the endgame in this situation? Sure these activists can topple over a single small fund who was maintaining some dodgy shorts. But anyone who knows anything about high finance works knows those guys are laughing this charade off and already building their next hedge fund. While a bunch of retail investors are at best left holding a bunch of garbage equities in their portfolio.&lt;/p&gt;
&lt;p&gt;I’m very sympathetic to the retail investors in this bubble because a lot of their anger is absolutely justified. But we have to ask what has this achieved? Yes, a tiny group of colluding insiders are richer; but a ton of investors are left holding bags and a bunch of institutions made a killing taking their money. This isn’t some David vs Goliath moment. This is Goliath drinking champagne while David dances for his amusement.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;A similar story has been spun around bitcoin investments for years in order to solicit billions in investment from the public. The prospectus is a similarly simplistic and easily-digestible story:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Investing in a cryptocurrency means you are putting your money toward a technology and a currency that somehow could one day replace the modern financial system&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And who wouldn’t want that? Yes, let’s replace the evil corrupt system that screwed us over! There are only two problems with this story: Its economic absurdity and technical absurdity.&lt;/p&gt;
&lt;p&gt;Bitcoin doesn’t work, it’s a naked speculative bubble based on a technology that doesn’t scale can’t and can’t replace any existing financial service. Sure you can gamble on it, you buy a bitcoin and then maybe you sell a bitcoin to a greater fool for a profit. That’s it. Economically this is a negative sum game, or a natural Ponzi scheme as the economist Robert Shiller describes in his book on the analysis of bubbles.&lt;/p&gt;
&lt;p&gt;The emperor is running naked through the streets and a cottage industry has emerged of people taking bets on the colour of his cloak. And whenever you criticize the emporer’s choice of attire, you often get accused of simply not understanding the heights of sophistication in nudist haute couture.&lt;/p&gt;
&lt;p&gt;Simple answers to complex problems are optimized for viral transmission. Build the wall, drain the swamp, buy bitcoin. These stories resonate because they fit the existing memetic frames of people’s hopes and fears. Bitcoin serves as an emotional dual to the lottery. While in the lottery people pour their hope and money into the chance of life-changing winnings based on impossible odds. The same class of people pour their rage and money into bitcoin out anger towards a financial system that isn’t working for them. Buying lottery tickets and trading manipulated crypto assets are both a tax on the poor and disenfranchised.&lt;/p&gt;
&lt;p&gt;The Gamestop story and bitcoin story are two sides of the same coin. The synthesis of faith in technolology, resentment, and populist rage has been exploited, financialized and turned into a digital speculative commodity. And in doing so anger at the financial system has itself been transmuted into a vehicle for wealth extraction from the gullible to the greedy for the same system. Bitcoin is overthrowing the financial system in the same way a Che Guevara t-shirt on sale for $5 at Primark is overthrowing capitalism .&lt;/p&gt;
&lt;p&gt;The cryptocurrency philosophy wraps itself around a warped view of economics stripped of rigour and calculation and is left to brew in an angry stew of conspiracy theories, revealing only the naked will to power. It is an intellectual contagion that feeds on the hopes of the hopeless, metastasizing in the dark corners of the internet and mutating resentment into greed. A death cult with a central faith in digital alchemy that purports to transmute entropy into gold by channelling its true believers worst instincts of resentment and greed into energy waste.&lt;/p&gt;
&lt;p&gt;I make no pretense that I don’t despise the bitcoin philosophy and believe it is one of the most insidious memes infecting our generation. And sadly it is one enabled by my own industry. At its core it is a philsophy that channels the mutual suffering of our generation, not into productive activity or reform, but into the very same worst excesses of financial speculation and climate change that created our generation’s problems.&lt;/p&gt;
</description>
      <pubDate>Wed, 03 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/gamestop/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/gamestop/</guid>
    </item>
    
    <item>
      <title>Notes on Trekonomics</title>
      <description>&lt;h1 id=&quot;notes-on-trekonomics&quot; tabindex=&quot;-1&quot;&gt;Notes on Trekonomics&lt;/h1&gt;
&lt;p&gt;My holiday reading this year has been Manu Saadia&#39;s &lt;a href=&quot;https://www.amazon.com/Trekonomics-Economics-Star-Manu-Saadia/dp/1941758754&quot;&gt;Trekonomics&lt;/a&gt;, which is a deeply fun and thought-provoking exploration of the economic philosophy of the Star Trek universe. Here&#39;s my review and reading notes for the book.&lt;/p&gt;
&lt;p&gt;The book&#39;s ultimate motivating premise is that Star Trek not merely as popular entertainment, but as a significant cultural icon carrying a potent utopian theme: the potential for human society to evolve beyond the constraints of capitalism. He argues that engaging with Star Trek&#39;s fictional economy—its &amp;quot;dream-work&amp;quot;—is valuable for understanding our own world and the transformations that might be necessary.&lt;/p&gt;
&lt;p&gt;This approach resonates strongly with Mark Fisher&#39;s concept of &lt;em&gt;capitalist realism&lt;/em&gt;—the pervasive sense that capitalism is not merely the dominant economic system but the &lt;em&gt;only conceivable one&lt;/em&gt;. Fisher argued that contemporary culture suffers from an inability to imagine coherent alternatives to capitalism, a failure of political and social imagination that Star Trek boldly challenges. By presenting a fully realized post-scarcity society, Star Trek performs the crucial function of breaking through what Fisher called the capitalist realist impasse, offering viewers a rare glimpse of a world organized around principles other than market exchange and profit maximization.&lt;/p&gt;
&lt;p&gt;Similarly, Fredric Jameson&#39;s Marxist analysis of science fiction (in his book &lt;em&gt;Archaeologies of the Future&lt;/em&gt;) also provides a framework for understanding Star Trek&#39;s significance. For Jameson, science fiction&#39;s value lies not in predicting technological futures but in its capacity to estrange us from our present, making visible the historical contingency of our economic and social arrangements. Star Trek functions as what Jameson would call a &amp;quot;cognitive mapping&amp;quot; of late capitalism&#39;s contradictions and limitations, particularly in episodes that contrast Federation values with overtly capitalist species like the Ferengi. The series performs the difficult work that Jameson identified as crucial yet increasingly rare in postmodern culture: representing a genuine alternative to capitalism that is neither nostalgic nor merely dystopian, but constructively utopian in its orientation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Introduction: Beyond Capitalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Introduction lays out Saadia&#39;s primary goal: to rigorously explore the economic theory underpinning Star Trek&#39;s 24th-century vision, treating its economic imagination as a coherent system worth analyzing. Saadia posits that Star Trek&#39;s central economic idea—that advanced automation can liberate humanity from drudgery—is not outlandish but a plausible extrapolation of historical trends, provided we manage the transition equitably and sustainably.&lt;/p&gt;
&lt;p&gt;He identifies Star Trek&#39;s utopia as a post-capitalist society, distinguishing between the more Heinlein-influenced &lt;em&gt;Original Series&lt;/em&gt; (what he calls Star Trek 1.0) and the Asimov-influenced, fully utopian 24th century of &lt;em&gt;The Next Generation&lt;/em&gt; and beyond (what he calls Star Trek 2.0), which is the book&#39;s main focus. While acknowledging TOS&#39;s foundational contributions (altruism, scientific optimism), Saadia frames Star Trek 2.0 within the literary tradition of More&#39;s &lt;em&gt;Utopia&lt;/em&gt;, noting its unusually optimistic stance on technology compared to much science fiction.&lt;/p&gt;
&lt;p&gt;The Introduction highlights that both science fiction and economics grapple with societal change and future possibilities. Star Trek uniquely hints at a future where automation displaces labor &lt;em&gt;and&lt;/em&gt; wealth is evenly distributed, though it remains vague on the specific policies. The core shift explored is the psychological and motivational change in a society where the compulsion to work for survival vanishes, replaced by the pursuit of self-actualization and contribution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 1: Money Went the Way of the Dinosaurs – Life Without Currency&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This chapter tackles the most striking feature of Federation economics: the apparent absence of money. Saadia explores how a complex society might function without a traditional pricing mechanism, arguing that once the fundamental &amp;quot;economic problem&amp;quot; (scarcity of necessities) is solved via technology, money loses much of its purpose.&lt;/p&gt;
&lt;p&gt;Key consequences are explored: status is decoupled from wealth, conspicuous consumption disappears, and luxury is redefined around unique experiences, relationships, and artful artifacts. Scarcity doesn&#39;t vanish entirely – non-replicable items like dilithium, starships, and unique individuals remain limited. However, even resource extraction like dilithium mining becomes easier with near-zero labor costs. Strategic goods are likely allocated through non-market means like reputation, expertise, or political processes. Importantly, Saadia clarifies that &lt;em&gt;capital&lt;/em&gt; (productive assets, tools, knowledge) persists even without &lt;em&gt;money&lt;/em&gt;. The Federation&#39;s system is presented as a form of utilitarianism where individuals choose how best to allocate their time and talents towards the common good, maximizing their personal fulfillment (utility) in the process.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 2: Why Is Everyone So Worried About Holograms...? – The Meaning of Work&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Chapter 2 delves into the nature of work in a post-scarcity utopia, addressing the seeming paradox that Starfleet officers are incredibly busy despite no economic need to work. Saadia examines how individuals find meaning when machines can perform tasks more efficiently. The Federation promises the freedom to pursue one&#39;s true calling, unburdened by economic necessity or tradition.&lt;/p&gt;
&lt;p&gt;Using examples like the &lt;em&gt;Voyager&lt;/em&gt; episode &amp;quot;Author, Author&amp;quot; (holographic rights) and Sisko&#39;s family restaurant (emotional connection to work), the chapter illustrates that work becomes a matter of passion, reputation, and self-selection. In this economy, businesses don&#39;t generate profit, and labor costs are nil; success is measured by reputation and quality. Work takes on a deeper meaning, akin to an apprenticeship driven by dedication. This emphasis on genuine achievement makes cheating (like genetic enhancement) a serious offense in the Federation&#39;s meritocracy. The chapter also acknowledges the system&#39;s imperfections, noting individuals who opt out or engage in clandestine activities (Section 31). Ultimately, value shifts away from utility or scarcity towards affect, sentiment, and the &amp;quot;human touch,&amp;quot; even in replicated designs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 3: Tea, Earl Grey, Hot – The Replicator Revolution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The replicator takes center stage in Chapter 3 as the technological cornerstone of Star Trek&#39;s post-scarcity. Saadia presents it both literally and metaphorically as the endpoint of the Industrial Revolution and the ultimate form of automation (specifically, additive manufacturing/3D printing). Crucially, he emphasizes that the replicator&#39;s societal impact hinges on the &lt;em&gt;political decision&lt;/em&gt; to make it universally available as a free public good.&lt;/p&gt;
&lt;p&gt;Connected to real-world advancements (Boeing, GE using 3D printing), the replicator is depicted as the &amp;quot;last machine,&amp;quot; automating the production of necessities and freeing human labor. This triggers a large-scale labor substitution, shifting human effort towards high-skill, creative, knowledge-based work. While replicators need inputs (matter, energy, data/designs), their designation as a public good (nonexcludable, nonrival) in the Federation instantly confers these properties onto the goods they produce. This contrasts sharply with visions like Neal Stephenson&#39;s &lt;em&gt;The Diamond Age&lt;/em&gt;, where similar technology remains proprietary, underscoring that post-scarcity is a political and social choice, not just a technological one.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 4: Only a Fool Would Stand in the Way of Progress – Overcoming Limits&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This chapter confronts the Malthusian dilemma: how can infinite social wealth coexist with finite natural resources? Saadia argues that &lt;em&gt;Trekonomics&lt;/em&gt; doesn&#39;t violate economic principles but relies heavily on technological substitution to overcome resource limitations. Referencing &lt;em&gt;The Original Series&lt;/em&gt; episode &amp;quot;The Mark of Gideon&amp;quot; and historical debates (Ehrlich&#39;s &lt;em&gt;Population Bomb&lt;/em&gt;, Club of Rome&#39;s &lt;em&gt;Limits to Growth&lt;/em&gt;), the chapter tackles the &amp;quot;peak everything&amp;quot; concept.&lt;/p&gt;
&lt;p&gt;Using examples like the Haber-Bosch process, Saadia illustrates how technology has historically expanded Earth&#39;s carrying capacity. The argument is that, over time, technological innovation tends to substitute scarce resources or make rival goods nonrival. Star Trek&#39;s 24th-century abundance is presented not as a sudden revolution but as the logical outcome of centuries of sustained, technology-driven growth fueled by human capital and innovation. It&#39;s an amplification of existing positive trends, rather than a complete break from economic reality.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 5: The Thought That Warp Engines Might Be Causing... – Managing the Commons&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Even in a post-scarcity utopia, negative externalities and the management of common resources remain relevant. Chapter 5 explores how the Federation handles these issues, contrasting its approach with less cooperative alien species. Using the &lt;em&gt;Next Generation&lt;/em&gt; episode &amp;quot;Force of Nature&amp;quot; (warp drive damaging spacetime) as a case study, Saadia shows the Federation unilaterally choosing restraint, setting an example.&lt;/p&gt;
&lt;p&gt;The chapter discusses Garrett Hardin&#39;s &amp;quot;Tragedy of the Commons&amp;quot; but leans towards Elinor Ostrom&#39;s findings that communities &lt;em&gt;can&lt;/em&gt; effectively self-govern common resources, suggesting the Federation embodies this principle on a massive scale. Its governance structure appears adept at identifying and implementing optimal collective actions, possibly facilitated by a culture that trusts experts and treats knowledge itself as a public good fostered through a reputation economy. This contrasts with real-world struggles (like climate change) often hampered by vested interests resisting change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 6: That Future, That Space Station... – Intellectual Ancestors of Trekonomics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Chapter 6 provides an intellectual history, tracing the lineage of Star Trek&#39;s economic ideas. Saadia notes the rarity of optimistic post-labor themes in sci-fi, positioning Isaac Asimov as a crucial precursor. &lt;em&gt;Trekonomics&lt;/em&gt; is framed as a reworking and deepening of Asimov&#39;s core concepts (positive view of robots, tackling work replacement, economics as a narrative force in &lt;em&gt;Foundation&lt;/em&gt;, knowledge as key).&lt;/p&gt;
&lt;p&gt;Star Trek&#39;s optimism is contrasted with Frank Herbert&#39;s &lt;em&gt;Dune&lt;/em&gt;which deliberately excludes intelligent machines. Saadia also highlights other potential influences: the Strugatsky brothers&#39; &lt;em&gt;Noon: 22nd Century&lt;/em&gt; (an early post-economic spacefaring vision) and Ursula Le Guin&#39;s &lt;em&gt;The Dispossessed&lt;/em&gt; (emphasizing post-scarcity as an organizational choice). These ideas shaped TNG and DS9&#39;s exploration of utopian societies interacting with others. The chapter charts the evolution from TOS (influenced by Heinlein, critiquing the Cold War) to TNG&#39;s embrace of utopia, moving beyond Cold War allegories and Roddenberry&#39;s initial pragmatic constraints. While deeply indebted to Asimov, Star Trek diverges by retaining work, albeit transformed into a pursuit of passion and craft.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 7: There Are Still Many Human Emotions... – The Psychology of Abundance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This chapter examines the profound psychological consequences of living in a world of abundance. Saadia suggests that the often-perceived &amp;quot;perfection&amp;quot; or emotional stability of Starfleet characters stems directly from the elimination of poverty and its associated stresses, which are known to impact mental health and cognitive function. The presence of counselors like Deanna Troi underscores this focus on mental well-being enabled by material security.&lt;/p&gt;
&lt;p&gt;Social status is based on merit and rank, not wealth. The chapter revisits the Prime Directive, analyzing it as the Federation&#39;s core ethical principle for interacting with other cultures. It&#39;s presented as a non-interventionist, utilitarian policy designed to prevent cultural contamination and entanglement, born partly from Cold War critiques (Vietnam). The Borg are introduced as a chilling counterpoint – a collective consciousness that achieves a form of post-scarcity stability but eradicates individuality, serving as a mirror to the Federation&#39;s model where individual will coexists with collective benefit. Ultimately, prosperity in Star Trek is redefined along Stoic lines: value lies in cultivating the mind and acting justly, not in accumulating material goods.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 8: The Ferengis, Interstellar Capitalists – The Necessary Foil&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Ferengi receive dedicated focus in Chapter 8, presented as the embodiment of galactic capitalism and a crucial foil to the Federation&#39;s utopia. Saadia argues their importance lies in illuminating the Federation&#39;s values through contrast, particularly within the context of &lt;em&gt;Deep Space Nine&lt;/em&gt;, where the Federation grapples with a more complex, imperfect galaxy.&lt;/p&gt;
&lt;p&gt;Quark and his bar serve as a narrative hub, and Quark&#39;s character arc demonstrates the potential for change even in a profit-obsessed culture through interaction with Federation ideals. The Ferengi culture, codified by the Rules of Acquisition and belief in the Great Material Continuum, acts as a satirical commentary on contemporary capitalism. Despite their greed, they possess an ethical core (no slavery/conquest). The chapter notes the Federation&#39;s ability to handle money when needed (using latinum) and its immense potential financial power due to its vast wealth. While touching on the &lt;em&gt;doux commerce&lt;/em&gt; idea (commerce civilizing), the text acknowledges the critique that this ignores historical exploitation. The Ferengi are ultimately positioned as representing &amp;quot;us,&amp;quot; and their evolution hints at Star Trek&#39;s aspirational goal: to inspire change in its audience.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Chapter 9: I Live in the Hope That You May One Day... – Is Trekonomics Possible?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The concluding chapter addresses the ultimate question: Could &lt;em&gt;Trekonomics&lt;/em&gt; become a reality? Saadia connects Star Trek&#39;s vision to John Maynard Keynes&#39;s prediction of a future where the &amp;quot;economic problem&amp;quot; is solved, leaving humanity to grapple with leisure. Star Trek depicts the completion of this shift, where knowledge work dominates.&lt;/p&gt;
&lt;p&gt;Saadia explains that &amp;quot;Tikkun olam&amp;quot; translates to &amp;quot;Heal the world, as we say in Hebrew&amp;quot;. This concept is presented as blending effortlessly with the kind of secular Judaism passed on to the author by his parents. The idea is that by living by the precepts of Star Trek, he associates this commitment with actions like learning as much as humanly possible, solving problems for others, fighting injustice wherever and whenever possible, and trying to be a &lt;em&gt;mensch&lt;/em&gt; (a Yiddish term for a good person).&lt;/p&gt;
&lt;p&gt;Building on Paul Romer&#39;s work, knowledge is identified as the nonrival &amp;quot;secret ingredient&amp;quot; driving growth, its spread limited primarily by policy choices, not inherent constraints. The book reaffirms that Star Trek&#39;s utopia is presented as a plausible, long-term extrapolation of current scientific and technological progress. The biggest hurdle isn&#39;t technology, but &lt;em&gt;distribution&lt;/em&gt; and political will. As real-world prosperity grows and &amp;quot;free&amp;quot; digital goods proliferate, we inch closer, but achieving a Star Trek-like future (especially space colonization) requires exponential wealth growth. The core lesson, Saadia suggests, is that &amp;quot;We are the Vulcans&amp;quot;—achieving this future requires adopting more rational, altruistic, and Stoic values, embracing pervasive automation that makes work elective, and fundamentally redefining prosperity around inner cultivation and contribution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Podcasts and Interviews&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The author was also interviewed on the &lt;a href=&quot;https://open.spotify.com/episode/5OmxyKubeLIHXLxOTzGYZP?si=a1a22ddd0fe84bc5&quot;&gt;Slate Money podcast&lt;/a&gt; and an interview with &lt;a href=&quot;https://www.ft.com/content/975341ff-97b3-3fb6-8338-d524366d0b34&quot;&gt;Financial Times&lt;/a&gt; with economist Paul Krugman.&lt;/p&gt;
&lt;p&gt;The discussion revolves around the significant complexities and potential &amp;quot;dark sides&amp;quot; to this utopian vision. Annalee Newitz points out that the Federation&#39;s comfortable post-scarcity lifestyle might be implicitly supported by other, less equitable economic systems on its periphery, hinting at exploitation, slavery (e.g., Data, holograms), or wage-slavery like conditions. Furthermore, Paul Krugman questions the true extent of post-scarcity, noting that replicators primarily address goods, not services, and that a society reliant on advanced AI for services could blur the lines between servitors and slaves.&lt;/p&gt;
&lt;p&gt;Paul Krugman references &amp;quot;Economic Possibilities for our Grandchildren&amp;quot; by John Maynard Keynes which predicted that if the world achieved a certain high level of wealth, money would effectively become obsolete, implying a resolution to the fundamental economic problem of scarcity. John K. Galbraith, in a similar vein, argued in &amp;quot;A New Industrial State&amp;quot; that the average American&#39;s standard of living would reach such heights that only &amp;quot;propaganda&amp;quot; would make them desire more.&lt;/p&gt;
&lt;p&gt;Trekonomics explores a society where, even with material abundance, human nature finds new arenas for competition and conflict. The meritocracy of Starfleet, while aspirational, is described by Manu Saadia as &amp;quot;harsh and cut-throat,&amp;quot; representing an elite &amp;quot;1%.&amp;quot; For the other 99%, life might be comfortable but not necessarily fulfilling, with pressures to achieve still present. The focus shifts from competition for resources to competition for status, reputation, and intellectual achievement, suggesting that while basic needs are met, human beings will continue to seek purpose and navigate social hierarchies, potentially leading to new forms of stress and inequality.&lt;/p&gt;
</description>
      <pubDate>Sat, 09 Jan 2021 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/notes_trekonomics/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/notes_trekonomics/</guid>
    </item>
    
    <item>
      <title>Listicle of Buddhist Lists</title>
      <description>&lt;h1 id=&quot;listicle-of-buddhist-lists&quot; tabindex=&quot;-1&quot;&gt;Listicle of Buddhist Lists&lt;/h1&gt;
&lt;p&gt;Buddhism has more lists than a Buzzfeed article. Inspired by the &lt;a href=&quot;https://www.leighb.com/listlist.htm&quot;&gt;List of Buddhist Lists&lt;/a&gt;, here are the Buddha&#39;s teachings as millennial listicles. Plus or minus some bad puns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mindfulness&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Noticing your brain is completely bonkers right now, and being surprisingly okay with it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Types of Truthbombs&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Conventional - Truths you should give a shit about.&lt;/li&gt;
&lt;li&gt;Ultimate - Truths that don&#39;t give a shit about you.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Four Shower Thoughts&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;😱 - Everything&#39;s gonna die and disintegrate. And ain&#39;t nothing you can do about it.&lt;/li&gt;
&lt;li&gt;🤷‍ - Chillax, because that&#39;s just how reality is and it literally couldn&#39;t be any other way.&lt;/li&gt;
&lt;li&gt;🤔 - And actually, wanting it to be any other way is crazeballs.&lt;/li&gt;
&lt;li&gt;😃 - But don&#39;t worry, because there&#39;s four weird truths that make it all better.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Buddha&#39;s Four Noble OG Truthbombs&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Life sucks.&lt;/li&gt;
&lt;li&gt;You want stuff.&lt;/li&gt;
&lt;li&gt;Stop wanting stuff.&lt;/li&gt;
&lt;li&gt;There&#39;s eight cool life hacks to stop wanting stuff.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Buddha&#39;s Eightfold Life Hacks&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;See shit clearly&lt;/li&gt;
&lt;li&gt;Think less stupid thoughts&lt;/li&gt;
&lt;li&gt;Don&#39;t be an asshole when you talk&lt;/li&gt;
&lt;li&gt;Do good stuff, not bad stuff&lt;/li&gt;
&lt;li&gt;Get a job that doesn&#39;t screw people over&lt;/li&gt;
&lt;li&gt;Try not to suck so much&lt;/li&gt;
&lt;li&gt;Pay attention to the now&lt;/li&gt;
&lt;li&gt;Chill out and focus&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Eight ... Oh Look, a Squirrel!&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Praise - Getting your ego stroked until it purrs, then wondering why it still feels empty&lt;/li&gt;
&lt;li&gt;Blame - Everyone&#39;s favorite game of hot potato where you&#39;re always &amp;quot;it&amp;quot;&lt;/li&gt;
&lt;li&gt;Loss - The universe&#39;s way of teaching detachment through involuntary item donation&lt;/li&gt;
&lt;li&gt;Gain - Acquiring stuff that will eventually become someone else&#39;s Loss&lt;/li&gt;
&lt;li&gt;Pleasure - Temporary happiness with an expiration date shorter than milk&lt;/li&gt;
&lt;li&gt;Pain - Life&#39;s persistent reminder that you have a body, whether you want one or not&lt;/li&gt;
&lt;li&gt;Fame - Being known by people who don&#39;t actually know you&lt;/li&gt;
&lt;li&gt;Ill-fame - When your reputation enters a room five minutes before you do&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Five Things You Think Is &amp;quot;You&amp;quot; But Isn&#39;t&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Form (Rupa) - The meat vehicle you&#39;re temporarily renting from the universe&lt;/li&gt;
&lt;li&gt;Feeling (Vedana) - That split-second &amp;quot;ooh&amp;quot; or &amp;quot;ew&amp;quot; before your brain overthinks everything&lt;/li&gt;
&lt;li&gt;Perception (Sañña) - Your mind&#39;s labeling system that&#39;s convinced a designer logo means happiness&lt;/li&gt;
&lt;li&gt;Mental Formations (Sankhara) - The mental Netflix of thoughts, emotions, and that embarrassing thing you said in sixth grade&lt;/li&gt;
&lt;li&gt;Consciousness (Viññana) - The awareness that&#39;s watching this whole cosmic sitcom unfold&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Five Things That Will F*ck You Up&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Sensual Desire - When your mantra becomes &amp;quot;om my god, look at that butt&amp;quot;&lt;/li&gt;
&lt;li&gt;Anger and Ill-will - Plotting revenge fantasies instead of counting breaths&lt;/li&gt;
&lt;li&gt;Sloth and Torpor - Meditation position: human puddle&lt;/li&gt;
&lt;li&gt;Restlessness and Remorse - Your mind doing parkour between yesterday&#39;s embarrassments and tomorrow&#39;s anxieties&lt;/li&gt;
&lt;li&gt;Skeptical Doubt - &amp;quot;Is this working? Am I enlightened yet? Hello? Is my sound on? Anyone?&amp;quot;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Buddha&#39;s Big No-No List&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;No Killing - Keeping all beings alive, even that spider that&#39;s definitely plotting against you&lt;/li&gt;
&lt;li&gt;No Stealing - If it&#39;s not yours, don&#39;t make it yours (yes, that includes your roommate&#39;s leftovers)&lt;/li&gt;
&lt;li&gt;No Sketchy Sex - Keep your bedroom drama-free and consent-based&lt;/li&gt;
&lt;li&gt;No Lying - Your pants aren&#39;t actually on fire, but your karma will be&lt;/li&gt;
&lt;li&gt;No Intoxicants - Skip the substances that make you think 3AM texts to your ex are a good idea&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Triple Troubles&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Greed - Wanting more stuff than your closet can hold, then buying a bigger closet&lt;/li&gt;
&lt;li&gt;Hatred - When your inner temperature runs hotter than your hot yoga class&lt;/li&gt;
&lt;li&gt;Delusion - Thinking Instagram is real life (spoiler: it&#39;s not)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Three Marks of eXistenZ&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Impermanence (anicca) - Everything changes.&lt;/li&gt;
&lt;li&gt;Suffering (dukkha) - Life sucks.&lt;/li&gt;
&lt;li&gt;Not-Self (anatta) - Mo&#39; self, mo&#39; problems.&lt;/li&gt;
&lt;/ol&gt;
&lt;!--
1. Not-Self (anatta) - You&#39;re nothin&#39;, and you&#39;re gonna suffer until you realize you&#39;re nothin&#39;.
--&gt;
&lt;p&gt;&lt;strong&gt;Meditation for Dummies&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You sit and think about how you aren&#39;t your thoughts.&lt;/li&gt;
&lt;li&gt;If you think really hard about your thoughts, you&#39;ll observe you (are) overthinking (you).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Gnarly Dependent Origination&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The universe is one big ocean, the waves come and go.&lt;/li&gt;
&lt;li&gt;Sometimes, you just have to go with the waves. Live to surf, surf to live.&lt;/li&gt;
&lt;li&gt;Waves don&#39;t start or end, they just be vibin&#39;.&lt;/li&gt;
&lt;li&gt;A surfer may be removed from the surf, but the wave cannot be removed from the surfer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Ten Easy-ish Vibe Checks&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Generosity - Give away your stuff until your friends think you&#39;ve joined a cult&lt;/li&gt;
&lt;li&gt;Morality - Not being a jerk, even when no one&#39;s watching&lt;/li&gt;
&lt;li&gt;Renunciation - Ghosting your possessions before they can ghost you&lt;/li&gt;
&lt;li&gt;Wisdom - Realizing you know nothing, and being weirdly cool with it&lt;/li&gt;
&lt;li&gt;Energy - Showing up for meditation at 5am while your roommates are still in drunk sleep&lt;/li&gt;
&lt;li&gt;Patience - Waiting for your Amazon package without refreshing the tracking page every 5 minutes&lt;/li&gt;
&lt;li&gt;Truthfulness - Telling your friend their new haircut looks &amp;quot;interesting&amp;quot; instead of &amp;quot;tragic&amp;quot;&lt;/li&gt;
&lt;li&gt;Determination - Still meditating even when your legs feel like they&#39;re being eaten by fire ants&lt;/li&gt;
&lt;li&gt;Unconditional Love - Caring about people who post political rants on Facebook&lt;/li&gt;
&lt;li&gt;Equanimity - Staying chill when life throws you curveballs, fastballs, or just straight-up hits you with the bat&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Nirvana&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fancy word for not giving a shit about self anymore and peacin&#39; out, cuz yo&#39; realize the u in &#39;you&#39; was never actually you at all, yo.&lt;/li&gt;
&lt;/ol&gt;
</description>
      <pubDate>Tue, 15 Jan 2019 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/buddhism_summary/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/buddhism_summary/</guid>
    </item>
    
    <item>
      <title>What are Elementary Particles?</title>
      <description>&lt;h1 id=&quot;what-are-elementary-particles%3F&quot; tabindex=&quot;-1&quot;&gt;What are Elementary Particles?&lt;/h1&gt;
&lt;p&gt;In primary school we&#39;re taught the naive intuition of particles as tiny, solid spheres propagating along well-defined trajectories collapses under the weight of quantum phenomena. Then in high-school we&#39;re taught that wave–particle duality shows that entities such as electrons and photons exhibit delocalized wave-like behavior described by a wavefunction &#92;(&#92;psi(x)&#92;) until a measurement forces a localized, particle-like manifestation. The indistinguishability of identical quanta—formalized by the need to symmetrize or antisymmetrize the multi-particle wavefunction in Hilbert space—renders them fundamentally indistinct, contrary to classical notions of separable objects. Then in university we&#39;re taught that Heisenberg’s uncertainty principle, precludes simultaneous precise knowledge of position and momentum, negating the picture of microscopic billiard balls. Then in graduate school we&#39;re taught that in quantum field theory particles are not primitive constituents but excitations of underlying fields. But there&#39;s another level of questioning beyond that.&lt;/p&gt;
&lt;p&gt;In the QFT ontology, fields defined at each spacetime point are the primary entities, each carrying operator-valued degrees of freedom. What we term a &amp;quot;particle&amp;quot; is a quantized excitation of its associated field, created or annihilated by ladder operators acting on the Fock space. The dynamics are encoded in a Lagrangian density &#92;(&#92;mathcal{L}(&#92;phi,&#92;partial_&#92;mu&#92;phi,&#92;psi,&#92;bar&#92;psi,&#92;dots)&#92;), and physical processes such as scattering or decay correspond to perturbative expansions in Feynman diagrams. Creation and annihilation operators &#92;(a^&#92;dagger_p&#92;) and &#92;(a_p&#92;) succinctly describe transitions between vacuum and excited states, replacing classical trajectories with changes in field occupancy.&lt;/p&gt;
&lt;p&gt;Within the Standard Model, each particle species is identified by its transformation properties under the Poincaré group and the internal gauge group &#92;(SU(3)_c&#92;times SU(2)_L&#92;times U(1)_Y&#92;). Wigner&#39;s classification associates elementary particles with irreducible representations of the Poincaré algebra, fixed by the Casimir invariants&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
C_1 = P_&#92;mu P^&#92;mu = m^2,&lt;br /&gt;
&#92;qquad&lt;br /&gt;
C_2 = W_&#92;mu W^&#92;mu = -m^2 s(s+1),&lt;br /&gt;
$$&lt;/p&gt;
&lt;p&gt;which uniquely determine mass &#92;(m&#92;) and spin &#92;(s&#92;). Gauge charges such as color, weak isospin, and hypercharge arise from irreducible representations of the internal symmetry group. Thus an electron field transforms as a spin-&#92;(&#92;tfrac12&#92;) Weyl representation of the Lorentz group, carries hypercharge &#92;(-&#92;tfrac12&#92;) under &#92;(U(1)_Y&#92;), and is a singlet under &#92;(SU(3)_c&#92;).&lt;/p&gt;
&lt;p&gt;Philosophically, &lt;a href=&quot;https://plato.stanford.edu/entries/structural-realism/&quot;&gt;structural realism&lt;/a&gt; offers a compelling ontology for this representation-theoretic view. Then there are two interpretations of structural realism: &lt;a href=&quot;https://plato.stanford.edu/entries/structural-realism/#EpisStruRealESR&quot;&gt;epistemic structural realism&lt;/a&gt; holds that scientific theories capture only relational structure, not the intrinsic nature of unobservables, while &lt;a href=&quot;https://plato.stanford.edu/entries/structural-realism/#OntiStruRealOSR&quot;&gt;ontic structural realism&lt;/a&gt; asserts that structure is all there is: relations and symmetries compose reality, and objects are mere nodes in a relational web. Under this perspective, a particle is not a substratum but the instantiation of a mathematical structure defined by group representations and field equations.&lt;/p&gt;
&lt;p&gt;The gauge symmetries of &#92;(SU(3)_c&#92;times SU(2)_L&#92;times U(1)_Y&#92;) further illustrate how abstract structure dictates physical phenomena. Color charge and confinement in quantum chromodynamics arise from the nonabelian structure of &#92;(SU(3)_c&#92;), while spontaneous symmetry breaking in the electroweak sector, achieved through the Higgs field acquiring a vacuum expectation value, endows particles with mass. These mechanisms underscore that what we call &amp;quot;particles&amp;quot; are emergent nodes in a tapestry woven from symmetry principles rather than independent substances.&lt;/p&gt;
&lt;p&gt;We engage with these structures through the formal tools of Lagrangian mechanics, field operators, and perturbation theory, where we translate abstract algebraic relations into precise numerical predictions. The extraordinary empirical success of quantum electrodynamics, quantum chromodynamics, and the electroweak theory—verified to unprecedented accuracy—attests to the ontological priority of mathematical structure over picturable matter.&lt;/p&gt;
&lt;p&gt;Particles are best understood as mathematical structures: irreducible representations of spacetime and internal symmetry groups that manifest as quantized field excitations. This structural ontology dissolves classical paradoxes of identity, localization, and determinacy, revealing a reality whose fundamental fabric is woven from abstract symmetries and relations. This view avoids speculating about an underlying &amp;quot;stuff&amp;quot; or &amp;quot;substratum&amp;quot; that has these properties. The properties, defined mathematically, constitute the particle ontologically.&lt;/p&gt;
&lt;p&gt;Of course this begs the perennial question: Does this mean reality is mathematical? Or is mathematics just our best tool for describing a reality whose ultimate nature remains elusive? This ties back to the broader realism/anti-realism debates. But ultimatly these are not physics questions anymore, so that&#39;s best left to another department.&lt;/p&gt;
</description>
      <pubDate>Sun, 11 Nov 2018 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/particles/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/particles/</guid>
    </item>
    
    <item>
      <title>Integral Zoo</title>
      <description>&lt;h1 id=&quot;integral-zoo&quot; tabindex=&quot;-1&quot;&gt;Integral Zoo&lt;/h1&gt;
&lt;p&gt;Similar to the &lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo&quot;&gt;derivative zoo&lt;/a&gt;, there are many different constructions we call &amp;quot;integrals&amp;quot;. Here are some of the most interesting ones:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#classical-integral&quot;&gt;Classical Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#line-integral&quot;&gt;Line Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#surface-integral&quot;&gt;Surface Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#contour-integral&quot;&gt;Contour Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#stieltjes-integral&quot;&gt;Stieltjes Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#lebesgue-integral&quot;&gt;Lebesgue Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#fractional-integral&quot;&gt;Fractional Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#path-integral&quot;&gt;Path Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#bochner-integral&quot;&gt;Bochner Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#ito-integral&quot;&gt;Ito Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#hadamard-integral&quot;&gt;Hadamard Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#darboux-integral&quot;&gt;Darboux Integral&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/integral_zoo/#wiener-integral&quot;&gt;Wiener Integral&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;classical-integral&quot; tabindex=&quot;-1&quot;&gt;Classical Integral&lt;/h2&gt;
&lt;p&gt;The indefinite integral, or antiderivative, of a function &#92;(f(x)&#92;) represents the family of functions &#92;(F(x)&#92;) whose derivative is &#92;(f(x)&#92;). It is denoted by &#92;(&#92;int f(x) dx = F(x) + C&#92;), where &#92;(F&#39;(x) = f(x)&#92;) and &#92;(C&#92;) is an arbitrary constant of integration, reflecting that the derivative of a constant is zero. This concept arises as the inverse operation to differentiation and is fundamentally linked to definite integrals via the Fundamental Theorem of Calculus.&lt;/p&gt;
&lt;h2 id=&quot;line-integral&quot; tabindex=&quot;-1&quot;&gt;Line Integral&lt;/h2&gt;
&lt;p&gt;A line integral generalizes the definite integral to integration along a curve &#92;(C&#92;) in 2D or 3D space. There are two main types. For a scalar field &#92;(f&#92;), the line integral with respect to arc length measures the &amp;quot;mass&amp;quot; or total value along the curve: &#92;(&#92;int_C f ds = &#92;int_a^b f(&#92;mathbf{r}(t)) |&#92;mathbf{r}&#39;(t)| dt&#92;). For a vector field &#92;(&#92;mathbf{F}&#92;), the line integral measures the work done by the field along the curve: &#92;(&#92;int_C &#92;mathbf{F} &#92;cdot d&#92;mathbf{r} = &#92;int_a^b &#92;mathbf{F}(&#92;mathbf{r}(t)) &#92;cdot &#92;mathbf{r}&#39;(t) dt&#92;). Both are calculated by parametrizing the curve &#92;(C&#92;) by &#92;(&#92;mathbf{r}(t)&#92;) for &#92;(t &#92;in [a, b]&#92;) and converting the integral into a standard definite integral in terms of the parameter &#92;(t&#92;).&lt;/p&gt;
&lt;h2 id=&quot;surface-integral&quot; tabindex=&quot;-1&quot;&gt;Surface Integral&lt;/h2&gt;
&lt;p&gt;A surface integral extends integration to surfaces &#92;(S&#92;) in 3D space, analogous to how line integrals extend definite integrals to curves. For a scalar field &#92;(g&#92;), the surface integral &#92;(&#92;iint_S g dS = &#92;iint_D g(&#92;mathbf{r}(u,v)) |&#92;mathbf{r}_u &#92;times &#92;mathbf{r}_v| dA&#92;) computes the total value or &amp;quot;mass&amp;quot; over the surface. For a vector field &#92;(&#92;mathbf{F}&#92;), the surface integral (often called flux) measures the net flow of the vector field through the surface: &#92;(&#92;iint_S &#92;mathbf{F} &#92;cdot d&#92;mathbf{S} = &#92;iint_S &#92;mathbf{F} &#92;cdot &#92;mathbf{n} dS = &#92;iint_D &#92;mathbf{F}(&#92;mathbf{r}(u,v)) &#92;cdot (&#92;mathbf{r}_u &#92;times &#92;mathbf{r}_v) dA&#92;). These are computed by parametrizing the surface &#92;(S&#92;) by &#92;(&#92;mathbf{r}(u,v)&#92;) over a domain &#92;(D&#92;) in the &#92;(uv&#92;)-plane and evaluating a double integral over &#92;(D&#92;).&lt;/p&gt;
&lt;h2 id=&quot;contour-integral&quot; tabindex=&quot;-1&quot;&gt;Contour Integral&lt;/h2&gt;
&lt;p&gt;A contour integral is essentially a line integral performed in the complex plane. For a complex function &#92;(f(z)&#92;) and a curve (contour) &#92;(C&#92;) parametrized by &#92;(&#92;gamma(t)&#92;) for &#92;(t &#92;in [a, b]&#92;), the integral is defined as &#92;(&#92;oint_C f(z) dz = &#92;int_a^b f(&#92;gamma(t)) &#92;gamma&#39;(t) dt&#92;). Note the complex multiplication &#92;(f(&#92;gamma(t))&#92;gamma&#39;(t)&#92;), which differs from the dot product used in real vector field line integrals. Contour integrals are fundamental in complex analysis, forming the basis for Cauchy&#39;s Integral Theorem, Cauchy&#39;s Integral Formula, and the Residue Theorem, which are powerful tools for evaluating integrals and analyzing complex functions.&lt;/p&gt;
&lt;h2 id=&quot;stieltjes-integral&quot; tabindex=&quot;-1&quot;&gt;Stieltjes Integral&lt;/h2&gt;
&lt;p&gt;The Riemann-Stieltjes integral, denoted &#92;(&#92;int_a^b f(x) d&#92;alpha(x)&#92;), generalizes the Riemann integral by integrating a function &#92;(f&#92;) with respect to another function &#92;(&#92;alpha&#92;) (called the integrator), rather than just with respect to &#92;(x&#92;) (which corresponds to &#92;(&#92;alpha(x) = x&#92;)). It is defined via limits of sums like &#92;(&#92;sum f(c_i) [&#92;alpha(x_i) - &#92;alpha(x_{i-1})]&#92;). This allows assigning different &amp;quot;weights&amp;quot; or &amp;quot;measures&amp;quot; to different parts of the interval, determined by the changes in &#92;(&#92;alpha&#92;). It is particularly useful in probability and statistics for calculating expected values with respect to cumulative distribution functions (if &#92;(&#92;alpha&#92;) is a CDF) and has applications in physics and functional analysis. The Lebesgue-Stieltjes integral is a more powerful measure-theoretic generalization.&lt;/p&gt;
&lt;h2 id=&quot;lebesgue-integral&quot; tabindex=&quot;-1&quot;&gt;Lebesgue Integral&lt;/h2&gt;
&lt;p&gt;The Lebesgue integral is a more general and powerful construction of the integral than the Riemann integral, based on measure theory. Instead of partitioning the domain &#92;([a, b]&#92;) (as in Riemann), the Lebesgue integral essentially partitions the &lt;em&gt;range&lt;/em&gt; of the function &#92;(f&#92;) and measures the size (using a measure &#92;(&#92;mu&#92;), typically Lebesgue measure) of the sets in the domain where &#92;(f&#92;) takes values in each part of the range partition. For a non-negative measurable function &#92;(f&#92;), it&#39;s defined as &#92;(&#92;int_E f d&#92;mu = &#92;sup { &#92;int_E s d&#92;mu &#92;mid 0 &#92;le s &#92;le f, s &#92;text{ is a simple function} }&#92;), and extended to general functions via &#92;(f = f^{+} - f^{-}&#92;). It can integrate a wider class of functions (including highly discontinuous ones) and has superior convergence theorems (like the Dominated Convergence Theorem and Monotone Convergence Theorem), making it standard in modern analysis and probability theory.&lt;/p&gt;
&lt;h2 id=&quot;fractional-integral&quot; tabindex=&quot;-1&quot;&gt;Fractional Integral&lt;/h2&gt;
&lt;p&gt;Fractional integrals generalize the notion of repeated integration to non-integer orders &#92;(&#92;alpha &amp;gt; 0&#92;). The most common form is the Riemann-Liouville fractional integral, defined as &#92;(_a I_t^&#92;alpha f(t) = &#92;frac{1}{&#92;Gamma(&#92;alpha)} &#92;int_a^t (t-&#92;tau)^{&#92;alpha-1} f(&#92;tau) d&#92;tau&#92;), where &#92;(&#92;Gamma&#92;) is the Gamma function. This formula can be motivated by generalizing Cauchy&#39;s formula for &#92;(n&#92;)-fold integration. Fractional integrals act as inverse operators (in a sense) to fractional derivatives and appear alongside them in the study of fractional calculus, used to model systems exhibiting memory or non-local behavior.&lt;/p&gt;
&lt;h2 id=&quot;path-integral&quot; tabindex=&quot;-1&quot;&gt;Path Integral&lt;/h2&gt;
&lt;p&gt;The path integral, particularly the Feynman path integral used in quantum mechanics and quantum field theory, is a concept for computing quantum amplitudes or statistical mechanical partition functions by summing (or &amp;quot;integrating&amp;quot;) over all possible trajectories (paths) a system can take between initial and final states. Schematically, it might look like &#92;(&#92;int &#92;mathcal{D}[x(t)] e^{i S[x(t)]/&#92;hbar}&#92;) or &#92;(&#92;int &#92;mathcal{D}[&#92;phi] e^{-S[&#92;phi]}&#92;), where &#92;(S&#92;) is the action functional and &#92;(&#92;mathcal{D}[x(t)]&#92;) represents a &amp;quot;measure&amp;quot; on the space of paths. It&#39;s not a standard mathematically rigorous integral in the sense of Riemann or Lebesgue; its definition typically relies on discretization (time-slicing) and taking limits, or other specialized formalisms.&lt;/p&gt;
&lt;h2 id=&quot;bochner-integral&quot; tabindex=&quot;-1&quot;&gt;Bochner Integral&lt;/h2&gt;
&lt;p&gt;The Bochner integral extends the Lebesgue integral to functions &#92;(f: X &#92;to B&#92;) that take values in a Banach space &#92;(B&#92;) (a complete normed vector space), defined over a measure space &#92;((X, &#92;Sigma, &#92;mu)&#92;). Similar to the Lebesgue construction, it is defined using limits of simple functions (which take finitely many values in &#92;(B&#92;)). The resulting integral &#92;( &#92;int_X f d&#92;mu &#92;) is an element of the Banach space &#92;(B&#92;). It is a crucial tool in functional analysis, vector measure theory, and the study of PDEs with values in Banach spaces.&lt;/p&gt;
&lt;h2 id=&quot;ito-integral&quot; tabindex=&quot;-1&quot;&gt;Ito Integral&lt;/h2&gt;
&lt;p&gt;The Itô integral is a fundamental concept in stochastic calculus, defining integration with respect to a stochastic process, typically Brownian motion (or a Wiener process) &#92;(W_t&#92;). For a suitable (non-anticipating or adapted) stochastic process &#92;(H_t&#92;), the Itô integral &#92;(&#92;int_0^T H_t dW_t&#92;) is defined as a limit of sums: &#92;(&#92;lim_{n&#92;to&#92;infty} &#92;sum_{i=0}^{n-1} H_{t_i} (W_{t_{i+1}} - W_{t_i})&#92;). A key feature is that the integrand &#92;(H_{t_i}&#92;) is evaluated at the &lt;em&gt;beginning&lt;/em&gt; of the time interval &#92;([t_i, t_{i+1}]&#92;). The result is a random variable, and Itô integrals do not follow classical calculus rules; for instance, the chain rule is replaced by Itô&#39;s lemma. It&#39;s essential for modeling systems driven by random noise, particularly in finance and physics.&lt;/p&gt;
&lt;h2 id=&quot;hadamard-integral&quot; tabindex=&quot;-1&quot;&gt;Hadamard Integral&lt;/h2&gt;
&lt;p&gt;The Hadamard finite part integral (or Hadamard regularization) is a method for assigning a finite value to certain types of improper integrals that diverge in the usual sense due to strong singularities. For example, for an integral &#92;(&#92;int_a^b &#92;frac{f(x)}{(x-x_0)^k} dx&#92;) where &#92;(f&#92;) is smooth and &#92;(x_0 &#92;in (a,b)&#92;), the Hadamard finite part involves subtracting the divergent terms derived from the Taylor expansion of &#92;(f&#92;) around &#92;(x_0&#92;). It&#39;s formally defined using limits, analytic continuation, or within the theory of distributions. It provides a way to regularize divergent integrals that appear in physics and applied mathematics, particularly in boundary element methods and quantum field theory.&lt;/p&gt;
&lt;h2 id=&quot;darboux-integral&quot; tabindex=&quot;-1&quot;&gt;Darboux Integral&lt;/h2&gt;
&lt;p&gt;The Darboux integral is a rigorous approach to defining the Riemann integral using upper and lower sums. For a bounded function &#92;(f&#92;) on an interval &#92;([a,b]&#92;), we partition &#92;([a,b]&#92;) into subintervals and define the upper sum &#92;(U(f,P) = &#92;sum_{i=1}^{n} M_i(x_i - x_{i-1})&#92;) and lower sum &#92;(L(f,P) = &#92;sum_{i=1}^{n} m_i(x_i - x_{i-1})&#92;), where &#92;(M_i&#92;) and &#92;(m_i&#92;) are the supremum and infimum of &#92;(f&#92;) on each subinterval. The upper Darboux integral is defined as &#92;(&#92;overline{&#92;int_a^b} f(x) dx = &#92;inf{U(f,P) : P &#92;text{ is a partition of } [a,b]}&#92;), and the lower Darboux integral as &#92;(&#92;underline{&#92;int_a^b} f(x) dx = &#92;sup{L(f,P) : P &#92;text{ is a partition of } [a,b]}&#92;). A function is Darboux integrable if these upper and lower integrals are equal, in which case this common value is the Darboux integral. This approach provides an equivalent alternative to the Riemann integral definition, with the advantage of clearly separating the approximation process from the limiting process, making certain theoretical properties more transparent.&lt;/p&gt;
&lt;h2 id=&quot;wiener-integral&quot; tabindex=&quot;-1&quot;&gt;Wiener Integral&lt;/h2&gt;
&lt;p&gt;The Wiener integral generally refers to integration with respect to the Wiener measure, which is a probability measure on the space of continuous paths (often starting at 0, denoted &#92;(C_0[0, T]&#92;)). This concept is central to the mathematical theory of Brownian motion. It can mean calculating the expectation of a functional &#92;(F&#92;) of a Wiener process &#92;(W&#92;), written as &#92;(E[F(W)] = &#92;int_{C_0[0,T]} F(&#92;omega) dP(&#92;omega)&#92;), where &#92;(P&#92;) is the Wiener measure. The term can sometimes also informally encompass stochastic integrals like the Itô or Stratonovich integrals, which integrate &lt;em&gt;with respect to&lt;/em&gt; sample paths of a Wiener process, or path integrals (in the Feynman sense) where the measure is formally related to Wiener measure in Euclidean time.&lt;/p&gt;
</description>
      <pubDate>Wed, 02 May 2018 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/integral_zoo/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/integral_zoo/</guid>
    </item>
    
    <item>
      <title>Derivative Zoo</title>
      <description>&lt;h1 id=&quot;derivative-zoo&quot; tabindex=&quot;-1&quot;&gt;Derivative Zoo&lt;/h1&gt;
&lt;p&gt;There are many types of operators that we call derivatives.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#classical-derivative&quot;&gt;Classical Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#one-sided-derivative&quot;&gt;One-Sided Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#higher-order-derivative&quot;&gt;Higher Order Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#implicit-derivative&quot;&gt;Implicit Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#complex-derivative&quot;&gt;Complex Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#partial-derivative&quot;&gt;Partial Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#directional-derivative&quot;&gt;Directional Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#covariant-derivative&quot;&gt;Covariant Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#lie-derivative&quot;&gt;Lie Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#exterior-derivative&quot;&gt;Exterior Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#material-derivative&quot;&gt;Material Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#weak-derivative&quot;&gt;Weak Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#frechet-derivative&quot;&gt;Fréchet Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#gateaux-derivative&quot;&gt;Gâteaux Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#variational-derivative&quot;&gt;Variational Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#fractional-derivative&quot;&gt;Fractional Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#radon-nikodym-derivative&quot;&gt;Radon-Nikodym Derivative&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stephendiehl.com/posts/derivative_zoo/#stochastic-derivative&quot;&gt;Stochastic Derivative&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;classical-derivative&quot; tabindex=&quot;-1&quot;&gt;Classical Derivative&lt;/h2&gt;
&lt;p&gt;The classical derivative of a function &#92;(f: &#92;mathbb{R} &#92;to &#92;mathbb{R}&#92;) at a point &#92;(x&#92;) is defined by the limit &#92;( f&#39;(x) = &#92;frac{df}{dx} = &#92;lim_{h &#92;to 0} &#92;frac{f(x+h) - f(x)}{h} &#92;). This value represents the instantaneous rate of change of the function &#92;(f&#92;) with respect to its variable &#92;(x&#92;), and geometrically corresponds to the slope of the tangent line to the graph of &#92;(f&#92;) at &#92;((x, f(x))&#92;). Its derivation stems from considering the average rate of change over a shrinking interval &#92;([x, x+h]&#92;), represented by the slope of the secant line &#92;( &#92;frac{f(x+h) - f(x)}{h} &#92;), and taking the limit as the interval width &#92;(h&#92;) approaches zero.&lt;/p&gt;
&lt;h2 id=&quot;one-sided-derivative&quot; tabindex=&quot;-1&quot;&gt;One-Sided Derivative&lt;/h2&gt;
&lt;p&gt;One-sided derivatives consider the rate of change as &#92;(x&#92;) is approached from only one direction. The right derivative is &#92;( f&#39;_+(x) = &#92;lim_{h &#92;to 0^+} &#92;frac{f(x+h) - f(x)}{h} &#92;) (where &#92;(h&#92;) approaches 0 from positive values), and the left derivative is &#92;( f&#39;_-(x) = &#92;lim_{h &#92;to 0^-} &#92;frac{f(x+h) - f(x)}{h} &#92;) (where &#92;(h&#92;) approaches 0 from negative values). These are derived using the same limit definition as the classical derivative, but restricting &#92;(h&#92;) to be either strictly positive or strictly negative. The classical derivative exists if and only if both the left and right derivatives exist and are equal.&lt;/p&gt;
&lt;h2 id=&quot;higher-order-derivative&quot; tabindex=&quot;-1&quot;&gt;Higher Order Derivative&lt;/h2&gt;
&lt;p&gt;Higher order derivatives are obtained by repeatedly applying the differentiation process. The &#92;(n&#92;)-th derivative, denoted &#92;(f^{(n)}(x)&#92;) or &#92;(&#92;frac{d^n f}{dx^n}&#92;), is defined recursively as the derivative of the &#92;((n-1)&#92;)-th derivative: &#92;( f^{(n)}(x) = &#92;frac{d}{dx} &#92;left( f^{(n-1)}(x) &#92;right) &#92;). For instance, the second derivative &#92;( f&#39;&#39;(x) = &#92;frac{d^2 f}{dx^2} = &#92;lim_{h &#92;to 0} &#92;frac{f&#39;(x+h) - f&#39;(x)}{h} &#92;) measures the rate of change of the first derivative (like acceleration) and relates to the function&#39;s concavity. Each higher order derivative measures the rate of change of the preceding one.&lt;/p&gt;
&lt;h2 id=&quot;implicit-derivative&quot; tabindex=&quot;-1&quot;&gt;Implicit Derivative&lt;/h2&gt;
&lt;p&gt;Implicit differentiation is a technique, rather than a specific formula, used when a relationship between &#92;(x&#92;) and &#92;(y&#92;) is given implicitly by an equation &#92;(F(x, y) = 0&#92;), defining &#92;(y&#92;) as a function of &#92;(x&#92;) locally. To find &#92;(&#92;frac{dy}{dx}&#92;), one differentiates both sides of the equation &#92;(F(x, y(x)) = 0&#92;) with respect to &#92;(x&#92;), treating &#92;(y&#92;) as &#92;(y(x)&#92;) and applying the chain rule where necessary (e.g., &#92;(&#92;frac{d}{dx}(y^2) = 2y &#92;frac{dy}{dx}&#92;)). The resulting equation is then algebraically solved for &#92;(&#92;frac{dy}{dx}&#92;). This allows finding the derivative without explicitly solving for &#92;(y&#92;).&lt;/p&gt;
&lt;h2 id=&quot;complex-derivative&quot; tabindex=&quot;-1&quot;&gt;Complex Derivative&lt;/h2&gt;
&lt;p&gt;The derivative of a complex function &#92;(f: &#92;mathbb{C} &#92;to &#92;mathbb{C}&#92;) at &#92;(z_0 &#92;in &#92;mathbb{C}&#92;) is defined similarly to the real case: &#92;( f&#39;(z_0) = &#92;lim_{h &#92;to 0} &#92;frac{f(z_0+h) - f(z_0)}{h} &#92;). However, here &#92;(h &#92;in &#92;mathbb{C}&#92;), and the limit must exist and yield the same value regardless of the path along which &#92;(h&#92;) approaches 0 in the complex plane. This path-independence requirement is very strong, distinguishing complex differentiability (holomorphicity) from real differentiability in &#92;(&#92;mathbb{R}^2&#92;) and implying the function satisfies the Cauchy-Riemann equations. It represents the complex rate of change.&lt;/p&gt;
&lt;h2 id=&quot;partial-derivative&quot; tabindex=&quot;-1&quot;&gt;Partial Derivative&lt;/h2&gt;
&lt;p&gt;For a multivariable function &#92;(f(x_1, x_2, ..., x_n)&#92;), the partial derivative with respect to one variable &#92;(x_i&#92;) at a point &#92;(&#92;mathbf{a}&#92;), denoted &#92;(&#92;frac{&#92;partial f}{&#92;partial x_i}(&#92;mathbf{a})&#92;), measures the function&#39;s rate of change along the &#92;(x_i&#92;) coordinate direction while holding all other variables constant. Its formula is &#92;( &#92;frac{&#92;partial f}{&#92;partial x_i}(&#92;mathbf{a}) = &#92;lim_{h &#92;to 0} &#92;frac{f(a_1, ..., a_i+h, ..., a_n) - f(a_1, ..., a_i, ..., a_n)}{h} &#92;). It is derived by applying the standard single-variable derivative definition with respect to &#92;(x_i&#92;), treating all other &#92;(x_j&#92;) (for &#92;(j &#92;neq i&#92;)) as fixed parameters.&lt;/p&gt;
&lt;h2 id=&quot;directional-derivative&quot; tabindex=&quot;-1&quot;&gt;Directional Derivative&lt;/h2&gt;
&lt;p&gt;The directional derivative generalizes the partial derivative, measuring the rate of change of a function &#92;(f: &#92;mathbb{R}^n &#92;to &#92;mathbb{R}&#92;) at a point &#92;(&#92;mathbf{a}&#92;) along an arbitrary direction specified by a unit vector &#92;(&#92;mathbf{u}&#92;). It is defined as &#92;( D_{&#92;mathbf{u}}f(&#92;mathbf{a}) = &#92;lim_{h &#92;to 0} &#92;frac{f(&#92;mathbf{a} + h&#92;mathbf{u}) - f(&#92;mathbf{a})}{h} &#92;). If &#92;(f&#92;) is differentiable at &#92;(&#92;mathbf{a}&#92;), this can be computed using the gradient &#92;(&#92;nabla f = (&#92;frac{&#92;partial f}{&#92;partial x_1}, ..., &#92;frac{&#92;partial f}{&#92;partial x_n})&#92;) via the dot product: &#92;( D_{&#92;mathbf{u}}f(&#92;mathbf{a}) = &#92;nabla f(&#92;mathbf{a}) &#92;cdot &#92;mathbf{u} &#92;). The definition applies the limit concept along the line &#92;(&#92;mathbf{a} + t&#92;mathbf{u}&#92;), while the gradient formula derives from the chain rule applied to &#92;(g(t) = f(&#92;mathbf{a} + t&#92;mathbf{u})&#92;).&lt;/p&gt;
&lt;h2 id=&quot;covariant-derivative&quot; tabindex=&quot;-1&quot;&gt;Covariant Derivative&lt;/h2&gt;
&lt;p&gt;The covariant derivative, &#92;(&#92;nabla_X T&#92;), generalizes the directional derivative to tensor fields &#92;(T&#92;) on curved spaces (manifolds) equipped with a connection &#92;(&#92;nabla&#92;). It measures the change of &#92;(T&#92;) along a vector field &#92;(X&#92;), accounting for the variation of the basis vectors or coordinate system. In local coordinates &#92;(x^i&#92;) with Christoffel symbols &#92;(&#92;Gamma^k_{ij}&#92;), the covariant derivative of a vector field &#92;(V = V^k &#92;frac{&#92;partial}{&#92;partial x^k}&#92;) along &#92;(X = X^j &#92;frac{&#92;partial}{&#92;partial x^j}&#92;) has components &#92;( (&#92;nabla_X V)^k = X^j (&#92;partial_j V^k + &#92;Gamma^k_{ij} V^i) &#92;), often written in component form as &#92;( &#92;nabla_j V^k = &#92;partial_j V^k + &#92;Gamma^k_{ij} V^i &#92;). It is derived axiomatically or by demanding it measure the intrinsic change of the tensor&#39;s components plus the change induced by the varying basis, yielding a tensor of the same type.&lt;/p&gt;
&lt;h2 id=&quot;lie-derivative&quot; tabindex=&quot;-1&quot;&gt;Lie Derivative&lt;/h2&gt;
&lt;p&gt;The Lie derivative, &#92;( &#92;mathcal{L}_X T &#92;), measures the rate of change of a tensor field &#92;(T&#92;) as it is &amp;quot;dragged&amp;quot; along the flow &#92;(&#92;phi_t&#92;) generated by a vector field &#92;(X&#92;). It is defined as &#92;( &#92;mathcal{L}_X T = &#92;lim_{t &#92;to 0} &#92;frac{&#92;phi_{-t}^* T - T}{t} &#92;), where &#92;(&#92;phi_{-t}^* T&#92;) denotes the pullback of &#92;(T&#92;) along the flow. Unlike the covariant derivative, it doesn&#39;t require a metric or connection. For a scalar function &#92;(f&#92;), it reduces to the directional derivative &#92;(&#92;mathcal{L}_X f = X(f)&#92;). For a vector field &#92;(Y&#92;), it equals the Lie bracket &#92;(&#92;mathcal{L}_X Y = [X, Y] = XY - YX&#92;). Its derivation involves comparing the tensor at a point &#92;(p&#92;) with the tensor at &#92;(&#92;phi_t(p)&#92;) appropriately transported back to &#92;(p&#92;).&lt;/p&gt;
&lt;h2 id=&quot;exterior-derivative&quot; tabindex=&quot;-1&quot;&gt;Exterior Derivative&lt;/h2&gt;
&lt;p&gt;The exterior derivative &#92;(d&#92;) is an operation mapping differential &#92;(k&#92;)-forms &#92;(&#92;omega&#92;) to &#92;((k+1)&#92;)-forms &#92;(d&#92;omega&#92;) on a manifold. For a 0-form (function) &#92;(f&#92;), &#92;(df = &#92;frac{&#92;partial f}{&#92;partial x^i} dx^i&#92;). For a &#92;(k&#92;)-form &#92;(&#92;omega = &#92;sum_{I} a_I dx^I&#92;), &#92;(d&#92;omega = &#92;sum_{I} da_I &#92;wedge dx^I&#92;). It uniquely satisfies properties like linearity, the graded Leibniz rule, &#92;(d(d&#92;omega) = 0&#92;) (&#92;(d^2=0&#92;)), and consistency with the gradient. It provides a coordinate-independent generalization of grad, curl, and div, and is fundamental to the generalized Stokes&#39; theorem: &#92;( &#92;int_M d&#92;omega = &#92;int_{&#92;partial M} &#92;omega &#92;). Its coordinate formula can be derived from these axioms.&lt;/p&gt;
&lt;h2 id=&quot;material-derivative&quot; tabindex=&quot;-1&quot;&gt;Material Derivative&lt;/h2&gt;
&lt;p&gt;The material derivative (or substantial derivative, total derivative), denoted &#92;(&#92;frac{D&#92;phi}{Dt}&#92;), measures the total rate of change of a field &#92;(&#92;phi(&#92;mathbf{x}, t)&#92;) (like temperature or density) experienced by a particle moving with a velocity field &#92;(&#92;mathbf{u}(&#92;mathbf{x}, t)&#92;). Its formula is &#92;( &#92;frac{D&#92;phi}{Dt} = &#92;frac{&#92;partial &#92;phi}{&#92;partial t} + (&#92;mathbf{u} &#92;cdot &#92;nabla) &#92;phi &#92;). It combines the local rate of change at a fixed point (&#92;(&#92;partial &#92;phi / &#92;partial t&#92;)) with the convective rate of change due to the particle&#39;s movement into regions with different &#92;(&#92;phi&#92;) values (&#92;(&#92;mathbf{u} &#92;cdot &#92;nabla &#92;phi&#92;)). It is derived by applying the multivariable chain rule to &#92;(&#92;phi(&#92;mathbf{x}(t), t)&#92;) where &#92;(&#92;frac{d&#92;mathbf{x}}{dt} = &#92;mathbf{u}&#92;).&lt;/p&gt;
&lt;h2 id=&quot;weak-derivative&quot; tabindex=&quot;-1&quot;&gt;Weak Derivative&lt;/h2&gt;
&lt;p&gt;The weak derivative extends differentiation to functions (like those in Sobolev spaces) which may not be smooth enough for classical differentiation. A function &#92;(v &#92;in L^1_{loc}(&#92;Omega)&#92;) is the &#92;(&#92;alpha&#92;)-th weak derivative of &#92;(u &#92;in L^1_{loc}(&#92;Omega)&#92;) (denoted &#92;(D^&#92;alpha u = v&#92;)) if &#92;( &#92;int_{&#92;Omega} u(x) (-1)^{|&#92;alpha|} D^&#92;alpha &#92;phi(x) dx = &#92;int_{&#92;Omega} v(x) &#92;phi(x) dx &#92;) holds for all smooth test functions &#92;(&#92;phi&#92;) with compact support in &#92;(&#92;Omega&#92;). This definition is motivated by the integration by parts formula, effectively transferring the derivative operation from the potentially non-smooth function &#92;(u&#92;) onto the infinitely differentiable test function &#92;(&#92;phi&#92;).&lt;/p&gt;
&lt;h2 id=&quot;fr%C3%A9chet-derivative&quot; tabindex=&quot;-1&quot;&gt;Fréchet Derivative&lt;/h2&gt;
&lt;p&gt;The Fréchet derivative generalizes the concept of the derivative to functions &#92;(f: U &#92;to W&#92;) between Banach spaces &#92;(V&#92;) and &#92;(W&#92;) (&#92;(U &#92;subseteq V&#92;) open). It is a bounded linear operator &#92;(A: V &#92;to W&#92;), denoted &#92;(Df(x)&#92;), that provides the best linear approximation of &#92;(f&#92;) near &#92;(x&#92;). This means &#92;( &#92;lim_{|h|_V &#92;to 0} &#92;frac{|f(x+h) - f(x) - A(h)|_W}{|h|_V} = 0 &#92;), or equivalently, &#92;(f(x+h) = f(x) + A(h) + o(|h|_V)&#92;). The definition formalizes the idea &#92;(f(x+h) &#92;approx f(x) + f&#39;(x)h&#92;) where &#92;(f&#39;(x)h&#92;) is replaced by the action of the linear map &#92;(A&#92;) on the displacement &#92;(h&#92;). If it exists, it&#39;s unique.&lt;/p&gt;
&lt;h2 id=&quot;g%C3%A2teaux-derivative&quot; tabindex=&quot;-1&quot;&gt;Gâteaux Derivative&lt;/h2&gt;
&lt;p&gt;The Gâteaux derivative is another generalization of the derivative for functions &#92;(f: U &#92;to W&#92;) between topological vector spaces, focusing on directional rates of change. The Gâteaux derivative of &#92;(f&#92;) at &#92;(x &#92;in U&#92;) in the direction &#92;(v &#92;in V&#92;) is &#92;( Df(x; v) = &#92;lim_{t &#92;to 0} &#92;frac{f(x+tv) - f(x)}{t} &#92;), provided the limit exists (where &#92;(t&#92;) is a scalar). It directly generalizes the concept of the directional derivative in &#92;(&#92;mathbb{R}^n&#92;). It is a weaker notion than the Fréchet derivative; Fréchet differentiability implies Gâteaux differentiability along all directions, but the converse is not always true.&lt;/p&gt;
&lt;h2 id=&quot;variational-derivative-(functional-derivative)&quot; tabindex=&quot;-1&quot;&gt;Variational Derivative (Functional Derivative)&lt;/h2&gt;
&lt;p&gt;The variational derivative, &#92;(&#92;frac{&#92;delta F}{&#92;delta f(x)}&#92;), measures how a functional &#92;(F[f]&#92;) (which maps functions to scalars) changes in response to an infinitesimal, localized variation &#92;(&#92;phi(x)&#92;) in its input function &#92;(f&#92;) at point &#92;(x&#92;). It is defined implicitly via the first variation: &#92;( &#92;delta F = &#92;int &#92;frac{&#92;delta F}{&#92;delta f(x)} &#92;phi(x) dx &#92;), where &#92;(&#92;delta F&#92;) is the linear part (in &#92;(&#92;phi&#92;)) of &#92;(F[f + &#92;epsilon &#92;phi] - F[f]&#92;) as &#92;(&#92;epsilon &#92;to 0&#92;). For functionals defined by an integral of a Lagrangian &#92;(L(x, f, f&#39;, ...)&#92;), it is often computed using the Euler-Lagrange expression: &#92;( &#92;frac{&#92;delta F}{&#92;delta f(x)} = &#92;frac{&#92;partial L}{&#92;partial f} - &#92;frac{d}{dx} &#92;frac{&#92;partial L}{&#92;partial f&#39;} + &#92;dots &#92;). It serves as the functional analogue of the gradient.&lt;/p&gt;
&lt;h2 id=&quot;fractional-derivative&quot; tabindex=&quot;-1&quot;&gt;Fractional Derivative&lt;/h2&gt;
&lt;p&gt;Fractional derivatives generalize the concept of differentiation to non-integer orders &#92;(&#92;alpha&#92;). There isn&#39;t a single universal definition, but common forms include the Riemann-Liouville derivative &#92;( _a D_t^&#92;alpha f(t) = &#92;frac{1}{&#92;Gamma(n-&#92;alpha)} &#92;frac{d^n}{dt^n} &#92;int_a^t (t-&#92;tau)^{n-&#92;alpha-1} f(&#92;tau) d&#92;tau &#92;) and the Caputo derivative &#92;( ^C_a D_t^&#92;alpha f(t) = &#92;frac{1}{&#92;Gamma(n-&#92;alpha)} &#92;int_a^t (t-&#92;tau)^{n-&#92;alpha-1} f^{(n)}(&#92;tau) d&#92;tau &#92;), where &#92;(n = &#92;lceil &#92;alpha &#92;rceil&#92;). These operators extend differentiation and integration to arbitrary real or complex orders. They are typically non-local (depending on the function&#39;s history) and find use in modeling phenomena with memory effects or fractal characteristics. Their derivation often involves generalizing properties of integer-order calculus, such as Cauchy&#39;s formula for repeated integration or Fourier transform properties.&lt;/p&gt;
&lt;h2 id=&quot;radon-nikodym-derivative&quot; tabindex=&quot;-1&quot;&gt;Radon-Nikodym Derivative&lt;/h2&gt;
&lt;p&gt;The Radon-Nikodym derivative &#92;(f = &#92;frac{d&#92;nu}{d&#92;mu}&#92;) relates two &#92;(&#92;sigma&#92;)-finite measures &#92;(&#92;mu&#92;) and &#92;(&#92;nu&#92;) on a measurable space &#92;((X, &#92;Sigma)&#92;). Provided that &#92;(&#92;nu&#92;) is absolutely continuous with respect to &#92;(&#92;mu&#92;) (&#92;(&#92;nu &#92;ll &#92;mu&#92;), meaning &#92;(&#92;mu(A)=0 &#92;implies &#92;nu(A)=0&#92;)), the Radon-Nikodym theorem guarantees the existence of a non-negative measurable function &#92;(f&#92;) such that &#92;( &#92;nu(A) = &#92;int_A f d&#92;mu &#92;) for all measurable sets &#92;(A&#92;). This function &#92;(f&#92;) acts as the density or rate of change of &#92;(&#92;nu&#92;) relative to &#92;(&#92;mu&#92;). If &#92;(&#92;mu&#92;) is Lebesgue measure, it generalizes concepts like probability density functions. Its existence is a deep result from measure theory, not derived from a simple formula but proven using functional analysis techniques.&lt;/p&gt;
&lt;h2 id=&quot;stochastic-derivative&quot; tabindex=&quot;-1&quot;&gt;Stochastic Derivative&lt;/h2&gt;
&lt;p&gt;The term &amp;quot;stochastic derivative&amp;quot; often refers to the Malliavin derivative, used in Malliavin calculus for functionals of stochastic processes like Brownian motion &#92;({W_t}&#92;). The Malliavin derivative &#92;(D_t F&#92;) of a random variable &#92;(F&#92;) measures its sensitivity to infinitesimal changes in the path of the driving noise at time &#92;(t&#92;). For a simple functional &#92;(F = f(W_{t_1}, ..., W_{t_n})&#92;), &#92;( D_t F = &#92;sum_{i=1}^n &#92;frac{&#92;partial f}{&#92;partial x_i}(W_{t_1}, ..., W_{t_n}) &#92;mathbf{1}_{[0, t_i]}(t) &#92;). It is formally defined via an integration-by-parts formula on Wiener space, making the derivative operator &#92;(D&#92;) the adjoint of the Skorokhod stochastic integral &#92;(&#92;delta&#92;). It plays a key role in stochastic analysis and quantitative finance.&lt;/p&gt;
</description>
      <pubDate>Tue, 01 May 2018 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/derivative_zoo/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/derivative_zoo/</guid>
    </item>
    
    <item>
      <title>Using Google OR-Tools to do Answer Set Programming</title>
      <description>&lt;h1 id=&quot;using-google-or-tools-to-do-answer-set-programming&quot; tabindex=&quot;-1&quot;&gt;Using Google OR-Tools to do Answer Set Programming&lt;/h1&gt;
&lt;p&gt;Ever tried manually figuring out the perfect schedule for your team or planning delivery routes? Enter Google OR-Tools, a free Swiss Army knife of a toolkit that tackles these headache-inducing optimization challenges. From constraint programming to linear programming and specialized routing solvers, it packs all the tools you need to solve complex puzzles in computer science. And since Google maintains and improves it, you can focus on solving your specific problems rather than reinventing optimization algorithms from scratch.&lt;/p&gt;
&lt;p&gt;Here&#39;s a simple example where we need to select people for a team while satisfying multiple constraints:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ortools&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;python &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cp_model

model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CpModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

persons &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Tony&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Ann&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Carl&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Amber&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Peter&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
person_vars &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NewBoolVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; persons&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Select exactly 2 people from group1&lt;/span&gt;
group1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Ann&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Carl&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Amber&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Peter&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person_vars&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; group1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Select exactly 1 person from group2&lt;/span&gt;
group2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Bob&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Amber&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person_vars&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; p &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; group2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Amber must be selected&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person_vars&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Amber&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Total team size must be 4&lt;/span&gt;
model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person_vars&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;values&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

solver &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CpSolver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
status &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; status &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;OPTIMAL &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; status &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FEASIBLE&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Solution found:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; person&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; var &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; person_vars&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;var&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;person&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;No solution found.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The other textbook example of this kind of problem is course scheduling:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ortools&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;python &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; cp_model

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_course_schedule&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    model &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CpModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Parameters&lt;/span&gt;
    num_time_slots &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Time slots per day&lt;/span&gt;
    num_rooms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
    courses &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Math&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Physics&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Chemistry&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Biology&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;English&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    teachers &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Smith&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Johnson&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;Williams&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Decision variables&lt;/span&gt;
    slots &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; course &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; courses&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; time &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_time_slots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; room &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_rooms&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                slots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;course&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; room&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NewBoolVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;course&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;time&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;room&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Each course must be scheduled exactly once&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; course &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; courses&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;course&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
                     &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; t &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_time_slots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                     &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; r &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_rooms&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Only one course per room per time slot&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; time &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_time_slots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; room &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_rooms&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; room&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; c &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; courses&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# Solve the model&lt;/span&gt;
    solver &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CpSolver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    status &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; status &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;OPTIMAL &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; status &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cp_model&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FEASIBLE&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&#92;nSchedule:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; time &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_time_slots&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;&#92;nTime Slot &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;time &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; room &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;num_rooms&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; course &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; courses&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; solver&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slots&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;course&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; room&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Room &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;room &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;course&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;No solution found.&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

create_course_schedule&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The distance matrix represents the distances between each pair of locations, where each row and column corresponds to a location (depot or customer). For example, distances[1][2] = 6 means it takes 6 units to travel from Customer 1 to Customer 2. The matrix is symmetric, meaning the distance from A to B equals the distance from B to A. The first row/column (index 0) represents the depot, while subsequent indices represent customers.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ortools&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;constraint_solver &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; routing_enums_pb2
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; ortools&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;constraint_solver &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; pywrapcp

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;create_delivery_route&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Distance matrix&lt;/span&gt;
    distances &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Depot&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Customer 1&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Customer 2&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Customer 3&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    manager &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pywrapcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RoutingIndexManager&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;distances&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    routing &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pywrapcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RoutingModel&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;manager&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;distance_callback&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;from_index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; to_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        from_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; manager&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IndexToNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;from_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        to_node &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; manager&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IndexToNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;to_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; distances&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;from_node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;to_node&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    transit_callback_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RegisterTransitCallback&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;distance_callback&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SetArcCostEvaluatorOfAllVehicles&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;transit_callback_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    search_parameters &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; pywrapcp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DefaultRoutingSearchParameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    search_parameters&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;first_solution_strategy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        routing_enums_pb2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FirstSolutionStrategy&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;PATH_CHEAPEST_ARC&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    solution &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SolveWithParameters&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;search_parameters&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Optimal delivery route:&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Start&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        route &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Depot&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IsEnd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Value&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NextVar&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; routing&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IsEnd&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                route&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&#39;Customer &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;manager&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;IndexToNode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        route&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;Depot&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39; -&gt; &#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;join&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;route&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

create_delivery_route&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 01 Feb 2018 00:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/or-tools/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/or-tools/</guid>
    </item>
    
    <item>
      <title>The Y-Combinator in Python</title>
      <description>&lt;h1 id=&quot;the-y-combinator-in-python&quot; tabindex=&quot;-1&quot;&gt;The Y-Combinator in Python&lt;/h1&gt;
&lt;p&gt;Lambda calculus holds a revered place as a foundational system in computation theory, influencing programming languages and paradigms. One fascinating application of lambda calculus is Church encoding, which allows representing data and operators using only functions. This blog post delves into how Church encoding can be implemented in Python to represent boolean values, basic arithmetic operations, and even complex functions like the Fibonacci sequence.&lt;/p&gt;
&lt;p&gt;Church encoding is a means of representing data and operators in lambda calculus, which uses functions instead of data types. Typical constructs such as numerals, boolean values, and basic arithmetic operations can be represented purely through functional abstractions and applications.&lt;/p&gt;
&lt;p&gt;Let&#39;s start by encoding boolean values and essential logical operations using lambda functions in Python.&lt;/p&gt;
&lt;p&gt;In Church encoding, TRUE and FALSE can be defined as functions taking two parameters, returning the first and the second parameter respectively:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;TRUE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x
FALSE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using these definitions, we can define logical operations. For instance, the AND operation can be represented as:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;And &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Arithmetic operations can also be represented in this system. For example, the successor of a number, addition, and multiplication are defined as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;Succ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Mul &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, for a more complex example, we use the Fibonacci sequence, implementing it through lambda calculus constructs in Python. We employ the Y combinator to handle recursion since lambda calculus inherently does not support recursion:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;Y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; v&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Fib &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pred&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sub&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;TWO&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; Gt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ONE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Implementing and running this encoded logic in Python serves as an excellent tool for understanding the deeper foundations of computation. Let’s see how it evaluates some Fibonacci positions:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Output: 1&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Output: 1&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Fib&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Output: 21&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The whole code provided for your twisted amusement is listed below:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;TRUE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x
FALSE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y
Product &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
And &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Not &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Boolean &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Product&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Succ &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Pred &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Nth &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Succ&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; Zero&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Dec &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Sub &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pred&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Mul &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Null &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Zero&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;TRUE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Eq &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Null&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Null&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Gt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Null&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Not&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Eq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Lt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Not&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Gt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Not&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Eq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
IfThenElse &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Range &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Nth&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Minimal&lt;/span&gt;
Fib &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Y&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; Add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Pred&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sub&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Two&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Boolean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Gt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;One&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Exapnded&lt;/span&gt;
Fib &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; w&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;w&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x
                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y
                                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                    &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                            i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y
                                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                            &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                    &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; j&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                            i&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; z
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                                                        &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; k
                                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; n
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; n
                    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 10 Aug 2017 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/church/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/church/</guid>
    </item>
    
    <item>
      <title>What Is Not Art?</title>
      <description>&lt;h1 id=&quot;what-is-not-art%3F&quot; tabindex=&quot;-1&quot;&gt;What Is Not Art?&lt;/h1&gt;
&lt;p&gt;The quest to define &amp;quot;art&amp;quot; is a notoriously fraught endeavor, one that has occupied philosophers, artists, and critics for centuries, often culminating in throwig up one&#39;s hands in frustration and an admission of its inherent elusiveness. Nevertheless, various philosophical schools have emerged, each proposing distinct frameworks for understanding what grants an object or experience the status of art. In order to ask the question of what is not art, we need to first understand what people positively say is art.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Essentialism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Essentialism posits that art possesses intrinsic, identifiable qualities that are fundamental to its nature as art. This stance suggests that there are core formal or physical properties an object must exhibit to be classified as an artwork. Historically, these properties might have been associated with beauty, mimesis (the imitation of reality), skilled craftsmanship, or the successful evocation of specific emotions. For an essentialist, the &amp;quot;artness&amp;quot; resides within the object itself, independent of external factors such as the artist&#39;s intention or the viewer&#39;s interpretation, though these might influence its appreciation. Critics of essentialism point to the vast diversity of art forms across cultures and throughout history, arguing that no single set of inherent properties can encompass everything that has been legitimately considered art, especially with the advent of conceptual art where the idea can take precedence over physical manifestation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Intentionalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The intentionalist view suggests that art is defined not solely by its physical characteristics but by the crucial interplay between these properties and a viewer&#39;s mental impression or experience. According to this perspective, an object becomes art through a combination of its tangible form and the cognitive or affective response it elicits. This definition places emphasis on the reception of the artwork, implying that the viewer&#39;s engagement and the mental state induced by the object are constitutive of its artistic status. It differs from traditional artist-centric intentionalism, which would prioritize the artist&#39;s original aims or meanings. This viewer-oriented intentionalism highlights the subjective yet crucial element of how an object is perceived and processed mentally.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Functionalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Functionalism asserts that an object or experience achieves the status of art by successfully fulfilling a specific purpose or function. The most commonly cited function is the provision of an aesthetic experience, which might involve feelings of pleasure, beauty, or profound engagement. However, art&#39;s functions can be broader, encompassing roles such as expressing emotion, conveying social or political commentary, inciting contemplation, fostering communal identity, or serving religious devotion. If an object is created with, and successfully achieves, one of these art-relevant functions, then it qualifies as art. A challenge for functionalism lies in defining what constitutes an &amp;quot;aesthetic experience&amp;quot; or an &amp;quot;art-relevant function&amp;quot; without circularity, and in accounting for art that may not seem to serve any obvious external purpose or even seeks to subvert typical artistic functions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Historicism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The historicist perspective proposes that an object&#39;s status as art is determined by its relationship to pre existing artworks or art traditions. Something is considered art if it consciously engages with, builds upon, reacts against, or is otherwise intelligibly linked to the history of art. This view emphasizes art&#39;s evolving narrative, where new works gain meaning and status through their dialogue with the past. For historicists, understanding an object as art requires recognizing its place within this ongoing historical conversation. A key question for historicism is how the very first artworks (perhaps cave paintings) acquired their status, as they would have had no prior art to relate to, suggesting that historicism might be a necessary but not always sufficient condition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Institutionalism&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Philosophers like Arthur Danto and George Dickie have argued that art is what designated members of the &amp;quot;artworld&amp;quot; deem it to be, that art is inherently a social construction. The artworld comprises a complex social network of artists, critics, curators, art historians, gallery owners, collectors, and other &amp;quot;influential insiders&amp;quot; or &amp;quot;connoisseurs.&amp;quot; According to Danto, to perceive something as art requires &amp;quot;an atmosphere of artistic theory, a knowledge of the history of art: an artworld.&amp;quot; This theory underscores the importance of context. The physical setting (like a gallery or museum, often referred to as a &amp;quot;white cube&amp;quot;), the artist&#39;s reputation, critical reviews, academic discourse, and market forces all contribute to an object&#39;s consecration as art. Marcel Duchamp&#39;s &lt;em&gt;Fountain&lt;/em&gt;, a urinal presented as sculpture, is a classic example: its art status is largely conferred by its acceptance and discussion within the artworld, not by any inherent aesthetic property. While powerful in explaining how certain objects come to be accepted as art, institutionalism has faced criticism for potential circularity (art is what the artworld says is art, and the artworld is made up of people concerned with art) and for being potentially elitist or exclusionary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The &amp;quot;Cluster Theory&amp;quot;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Recognizing the limitations of any single definition, the &amp;quot;cluster theory&amp;quot; of Berys Gaut suggests that art is defined by a collection of criteria, rather than a single necessary or sufficient condition. This perspective views &amp;quot;art&amp;quot; as a concept characterized by a &amp;quot;hodgepodge,&amp;quot; or more formally, a weighted set of properties, many of which are drawn from the theories mentioned above. These might include aesthetic qualities, expressive properties, intellectual challenge, formal complexity, originality, skilled execution, belonging to an established art form, or the intention to create art. An object possessing a significant number, or a particularly salient combination, of these properties is more likely to be classified as art. This approach offers flexibility and acknowledges the multifaceted nature of art, accommodating a wider range of artistic phenomena.&lt;/p&gt;
&lt;p&gt;Beyond these formal philosophical schools, there are other less formal perspectives. Some argue that an artist&#39;s declaration is paramount: &amp;quot;if an artist is calling something art, it’s art.&amp;quot; Of course this definition is circular, and punts on the question of who or what can be an artist and what &amp;quot;declaration&amp;quot; is. Can an algorithm be an artist? If an artist does not explicitly call something art, is it not art?&lt;/p&gt;
&lt;p&gt;And then there are neuroscientific approaches, such as that proposed by Johan Wagemans, who suggest art functions by &amp;quot;deliberately messing with&amp;quot; our brain&#39;s predictive processing. Artists create works that introduce incongruities and defy expectations, generating &amp;quot;prediction errors&amp;quot; that our brains find stimulating, thereby keeping our cognitive faculties nimble and potentially adjusting our perceptions of reality. This is an interesting perspective but it is largely non-predictive in helping classify art at least with our current understanding of consciousness and neuroscience.&lt;/p&gt;
&lt;p&gt;The ontological status of &amp;quot;not-art&amp;quot; becomes a fascinating thought experiment in itself when we consider the extreme flexibility of what &amp;quot;art&amp;quot; can be. If one subscribes to highly permissive definitions—where virtually anything can be designated or experienced as art through sufficient intention, contextual framing, or institutional acceptance—then the category of things that can &lt;em&gt;never&lt;/em&gt; be art shrinks to a near vanishing point, perhaps limited only by the boundaries of human perception or imagination. Certainly, countless objects and phenomena are &amp;quot;presently not art,&amp;quot; existing outside the current frameworks of artistic presentation, discourse, or reception. However, the deeper question of whether &amp;quot;not-art&amp;quot; exists as a stable, inherent ontological category, rather than merely a descriptor for the current &lt;em&gt;absence&lt;/em&gt; of art-conferring conditions or its exclusion from the stories we deem worth telling, remains profoundly contentious and central to the entire debate about art&#39;s definition.&lt;/p&gt;
&lt;p&gt;The very expansiveness of certain definitions of art, particularly those rooted in institutional validation or an artist&#39;s simple declaration, brings the falsifiability of the concept of &amp;quot;not-art&amp;quot; into sharp focus. If the category of &amp;quot;art&amp;quot; can theoretically accommodate any object or phenomenon provided it receives the appropriate contextual framing or an artist&#39;s imprimatur, then establishing a stable, testable category of &amp;quot;not-art&amp;quot; becomes profoundly challenging. For the term &amp;quot;art&amp;quot; to retain descriptive utility, its logical opposite, &amp;quot;not-art,&amp;quot; must represent a domain with discernible boundaries, however contested they may be. The principle of falsifiability suggests that a claim is empirically meaningful if one can conceive of an observation or argument that could refute it. If the assertion &amp;quot;this object is not art&amp;quot; can be perpetually overturned by a future shift in institutional consensus, as implied by institutionalism, or by a subsequent declaration from an individual identifying as an artist, then &amp;quot;not-art&amp;quot; as a definitive status is rendered highly provisional and difficult to falsify in any lasting sense. While one might observe that an object &lt;em&gt;currently&lt;/em&gt; lacks artworld recognition or an artist&#39;s claim, the stronger assertion that it &lt;em&gt;is inherently and permanently not art&lt;/em&gt; becomes almost impossible to defend if the criteria for art are entirely extrinsic and subject to change. Philosophical approaches like essentialism or functionalism, by contrast, offer more potential for falsifying a claim of &amp;quot;not-art&amp;quot;; if an object demonstrably lacks posited essential artistic properties or fails to fulfill an identified artistic function, one could argue for its &amp;quot;not-art&amp;quot; status, and this argument could itself be tested or refuted by demonstrating the presence of such properties or functions. Without such relatively stable, albeit debatable, criteria, if the state of &amp;quot;not-art&amp;quot; is not meaningfully falsifiable because almost anything &lt;em&gt;could&lt;/em&gt; become art, then the term &amp;quot;art&amp;quot; itself risks diluting its semantic content to the point of near vacuity, becoming a label of arbitrary application rather than a descriptor of a distinguishable category of human creation or experience.&lt;/p&gt;
&lt;p&gt;The challenge of establishing a falsifiable concept of &amp;quot;not-art&amp;quot; not only underscores the profound philosophical flexibility of defining &amp;quot;art&amp;quot; but also probes more deeply into fundamental aspects of human experience as social animals: specifically, what we collectively choose to value and the narratives we construct around those valuations. If &amp;quot;art&amp;quot; serves as a mechanism through which societies elevate certain objects, ideas, or experiences, imbuing them with significance and weaving them into a shared cultural tapestry, then &amp;quot;not-art&amp;quot; becomes less an intrinsic category and more a descriptor for those elements of existence currently residing outside this circle of deliberate attention and storytelling. It often encompasses the banal, the mundane, the aspects of daily life that have not yet been subjected to the transformative process of &amp;quot;making special,&amp;quot; to borrow Ellen Dissanayake&#39;s term, or selected for inclusion in the grand narratives curated by social traditions or institutions.&lt;/p&gt;
&lt;p&gt;This is however a fluid boundary reflecting our evolving priorities and the stories we deem worth telling. What is considered &amp;quot;not-art&amp;quot; today—the purely functional, the mass-produced, the everyday occurrence—can readily become the subject or medium of art tomorrow, precisely because our collective understanding of what merits deep contemplation or narrative exploration shifts. The philosophical slipperiness of defining art and not-art, therefore, is not merely an academic puzzle. It is a direct reflection of humanity&#39;s ongoing, dynamic process of sifting through the vastness of reality, selecting certain fragments, and through the multifaceted practices we call art, investing them with meaning, prompting reflection, and communicating values. The category of &amp;quot;not-art,&amp;quot; in this light, is less about an inherent lack of potential aesthetic quality and more about its current position relative to our active, social endeavor of creating and perpetuating cultural significance through the stories we choose to tell about ourselves and the world. The pursuit of defining art, then, is inextricably linked to the continuous human quest to articulate what matters.&lt;/p&gt;
</description>
      <pubDate>Thu, 15 Jun 2017 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/not_art/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/not_art/</guid>
    </item>
    
    <item>
      <title>Dive into GHC: Targeting Core</title>
      <description>&lt;h3 id=&quot;dive-into-ghc%3A-targeting-core&quot; tabindex=&quot;-1&quot;&gt;Dive into GHC: Targeting Core&lt;/h3&gt;
&lt;p&gt;In the &lt;a href=&quot;http://www.stephendiehl.com/posts/ghc_01.html&quot;&gt;last blog post&lt;/a&gt;, we&lt;br /&gt;
discussed the intermediate structures used in the GHC compiler. This time let&#39;s&lt;br /&gt;
discuss the Core language.&lt;/p&gt;
&lt;p style=&quot;text-align:center&quot;&gt;
**[Accompaying Source Code](https://github.com/sdiehl/dive-into-ghc/tree/master/03-core)**
&lt;/p&gt;
&lt;h4 id=&quot;core&quot; tabindex=&quot;-1&quot;&gt;Core&lt;/h4&gt;
&lt;p&gt;GHC&#39;s most central data types. Core is a very small,&lt;br /&gt;
explicitly-typed, variant of System FC; a typed lambda calculus that differs&lt;br /&gt;
from the simply typed lambda calculus by the introduction of a mechanism of&lt;br /&gt;
universal quantification over types denoted as capital lambda $&#92;Lambda$.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;-- System-F Notation&lt;/span&gt;
Λ &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; λ &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- Haskell Core&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&#92;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While the Haskell frontend is an implicitly-typed source language, Core is an&lt;br /&gt;
explicitly-typed language. Every binder has an explicit type, and terms include&lt;br /&gt;
explicit type abstractions and applications.&lt;/p&gt;
&lt;p&gt;To inspect the core from GHCi we can invoke it using the following flags and the&lt;br /&gt;
following shell alias. We have explicitly disable the printing of certain&lt;br /&gt;
metadata and longform names to make the representation easier to read.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token builtin class-name&quot;&gt;alias&lt;/span&gt; ghci-core&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ghci -ddump-simpl -dsuppress-idinfo &#92;
-dsuppress-coercions -dsuppress-type-applications &#92;
-dsuppress-uniques -dsuppress-module-prefixes&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At the interactive prompt we can then explore the core representation&lt;br /&gt;
interactively:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ ghci-core
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then for example we can type in normal expressions and see their translation&lt;br /&gt;
into Core.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;λ: &lt;span class=&quot;token builtin class-name&quot;&gt;let&lt;/span&gt; f x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x + &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; f :: Int -&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Int

&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; Simplified expression &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;
returnIO
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;: &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;((&lt;/span&gt;&#92; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Int&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; $fNumInt x &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;I# &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;))&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;cast&lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;))&lt;/span&gt;

λ: &lt;span class=&quot;token builtin class-name&quot;&gt;let&lt;/span&gt; f x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x, x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; Simplified expression &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt;
returnIO &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;: &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;((&lt;/span&gt;&#92; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;@ t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;))&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;cast&lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;.&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;core-syntax&quot; tabindex=&quot;-1&quot;&gt;Core Syntax&lt;/h4&gt;
&lt;p&gt;Core is defined in the&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/CoreSyn.html&quot;&gt;CoreSyn&lt;/a&gt;&lt;br /&gt;
module. The central datatype is &lt;code&gt;Expr&lt;/code&gt; which holds the 10 core datatypes that&lt;br /&gt;
all Haskell expressions can be condensed down into.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;	  &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Lit&lt;/span&gt;   &lt;span class=&quot;token constant&quot;&gt;Literal&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;App&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Arg&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Lam&lt;/span&gt;   &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Let&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Bind&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Case&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Alt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cast&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Coercion&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Tick&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Tickish&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt;  &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Coercion&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Coercion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The pattern match logic for Core is broken down into several datatypes which&lt;br /&gt;
discriminate on at most one branch of a constructor. The process of translating&lt;br /&gt;
frontend case statements into Core case statements involves the process of&lt;br /&gt;
expanding pattern matches out into their a &amp;quot;splitting tree&amp;quot; of cases. The case&lt;br /&gt;
for the wild card pattern match is &lt;code&gt;(DEFAULT, [], rhs)&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Arg&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Alt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;AltCon&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;AltCon&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataAlt&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataCon&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LitAlt&lt;/span&gt;  &lt;span class=&quot;token constant&quot;&gt;Literal&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DEFAULT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notably on all the types there is a parameter &lt;code&gt;b&lt;/code&gt; is the type of binders. The&lt;br /&gt;
binder type is a sum type containing a recursive binder and a non-recursive&lt;br /&gt;
binder. Bindings that are mutually recursive are encoded as a list of binders.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bind&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NonRec&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example the factorial function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;... is expanded out into it&#39;s core binders is enclosed in a recursive binding.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token constant&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;&#92;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ds&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;wild&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ds1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ds1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;__DEFAULT&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt;
        &lt;span class=&quot;token hvariable&quot;&gt;fac&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fNumInt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;wild&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fNumInt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;wild&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are several type synonyms provided by the &lt;code&gt;CoreSyn&lt;/code&gt; module that expand&lt;br /&gt;
out the binder parameter into a convenient synonym.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyVar&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreExpr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreBndr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreBind&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bind&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreBndr&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreProgram&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;CoreBind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As an aside the notation used in papers on the typing judgements for Haskell&lt;br /&gt;
syntax typically uses the following convention and has a one-to-one mapping to&lt;br /&gt;
each of the Haskell datatypes in &lt;code&gt;Expr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;$$&lt;br /&gt;
&#92;begin{aligned}&lt;br /&gt;
e ::= &#92; &amp;amp; n                                              &amp;amp; &#92;mathtt{Var}      &amp;amp;&#92;quad &#92;text{Variable} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;mathtt{lit}                                   &amp;amp; &#92;mathtt{Lam}      &amp;amp;&#92;quad &#92;text{Literal} &#92;&lt;br /&gt;
|&#92; &amp;amp; e_1 &#92; e_2                                      &amp;amp; &#92;mathtt{App}      &amp;amp;&#92;quad &#92;text{Application} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;lambda n . e                                  &amp;amp; &#92;mathtt{Lam}      &amp;amp;&#92;quad &#92;text{Abstraction} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;textbf{let}&#92; &#92;mathit{binding}&#92; &#92;textbf{in}&#92; e &amp;amp; &#92;mathtt{Let}      &amp;amp;&#92;quad &#92;text{Variable binding} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;textbf{case}&#92; e&#92; &#92;textbf{as}&#92; n&#92; &#92;textbf{return}&#92; &#92;tau&#92; &#92;textbf{of}&#92; &#92;overline{alt} &amp;amp; &#92;mathtt{Case}     &amp;amp;&#92;quad &#92;text{Pattern match} &#92;&lt;br /&gt;
|&#92; &amp;amp; e &#92;triangleright &#92;gamma                        &amp;amp; &#92;mathtt{Cast}     &amp;amp;&#92;quad &#92;text{Cast} &#92;&lt;br /&gt;
|&#92; &amp;amp; e_{&#92;lbrace &#92;textit{tick} &#92;rbrace }             &amp;amp; &#92;mathtt{Tick}     &amp;amp;&#92;quad &#92;text{Internal note} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;tau                                           &amp;amp; &#92;mathtt{Type}     &amp;amp;&#92;quad &#92;text{Type} &#92;&lt;br /&gt;
|&#92; &amp;amp; &#92;gamma                                         &amp;amp; &#92;mathtt{Coercion} &amp;amp;&#92;quad &#92;text{Coercion} &#92;&lt;br /&gt;
&#92;end{aligned}&lt;br /&gt;
$$&lt;/p&gt;
&lt;h4 id=&quot;var&quot; tabindex=&quot;-1&quot;&gt;Var&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;Var&lt;/code&gt; type is central to most of the core definitions, it is the primary&lt;br /&gt;
name type used in the later half of the compiler and contains is a synonym for&lt;br /&gt;
the &lt;code&gt;Id&lt;/code&gt; type but it may additionally potentially contain type variables,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;-- Type and kind variables&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyVar&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varName&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;realUnique&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FastInt&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varType&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Kind&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Internal type variables used by inference algorithm&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TcTyVar&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varName&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;realUnique&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FastInt&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varType&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Kind&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tc_tv_details&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TcTyVarDetails&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Value level identifiers&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varName&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;realUnique&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FastInt&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;varType&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;idScope&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdScope&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;id_details&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdDetails&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;id_info&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The fields for Var also contain and important type which indicates the&lt;br /&gt;
provenance of the identifier. A &lt;code&gt;LocalId&lt;/code&gt; is bound within an expression such&lt;br /&gt;
as a lambda, case, or let binding. A &lt;code&gt;GlobalId&lt;/code&gt; is either a top-level&lt;br /&gt;
expression or a imported data constructor, class, etc.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdScope&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GlobalId&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LocalId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ExportFlag&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to the scope there are two metadata records that give additional&lt;br /&gt;
information about the usage and type of the identifier. These are the &lt;code&gt;IdInfo&lt;/code&gt;&lt;br /&gt;
and &lt;code&gt;IdDetails&lt;/code&gt;. The &lt;code&gt;IdDetails&lt;/code&gt; primary contains information about why the&lt;br /&gt;
variable is introduced while &lt;code&gt;IdInfo&lt;/code&gt; contains metadata about optimizations&lt;br /&gt;
that may be applied during the core to core passes.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdDetails&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;VanillaId&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RecSelId&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;sel_tycon&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyCon&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;sel_naughty&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataConWorkId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataCon&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DataCon&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataConWrapId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DataCon&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;DataCon&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ClassOpId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Class&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Class&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;PrimOpId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;PrimOp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;PrimOp&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FCallId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ForeignCall&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ForeignCall&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TickBoxOpId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TickBoxOp&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DFunId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The structure of &lt;code&gt;IdInfo&lt;/code&gt; is a set flags.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;arityInfo&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ArityInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;specInfo&lt;/span&gt;        &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SpecInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unfoldingInfo&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Unfolding&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cafInfo&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CafInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;oneShotInfo&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;OneShotInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;inlinePragInfo&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;InlinePragma&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;occInfo&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;OccInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;strictnessInfo&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;StrictSig&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;demandInfo&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Demand&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;callArityInfo&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ArityInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the identifiers we&#39;ll construct we&#39;ll simply use the vanilla flavor of id as&lt;br /&gt;
they are simply from lambda expressions and toplevel functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;vanillaIdInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;vanillaIdInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cafInfo&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;vanillaCafInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;arityInfo&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unknownArity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;specInfo&lt;/span&gt;          &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptySpecInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unfoldingInfo&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noUnfolding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;oneShotInfo&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NoOneShotInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;inlinePragInfo&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;defaultInlinePragma&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;occInfo&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NoOccInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;demandInfo&lt;/span&gt;        &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;topDmd&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;strictnessInfo&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;nopSig&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;callArityInfo&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unknownArity&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A name combined with this metadata and a type uniquely constructs and Id/Var and&lt;br /&gt;
there are several helper functions that combine them together.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkTyVar&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Kind&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyVar&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;mkLocalVar&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdDetails&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;mkGlobalVar&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdDetails&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IdInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;example&quot; tabindex=&quot;-1&quot;&gt;Example&lt;/h4&gt;
&lt;p&gt;Consider the following incredibly simple module:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Example&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s recreate it using Core constructions, as the functions we mentioned above.&lt;br /&gt;
We&#39;ll simulate the unique supply in a contrived way using &lt;code&gt;mkUnique&lt;/code&gt;, in&lt;br /&gt;
practice one shouldn&#39;t actually do this and instead simply initialize the supply&lt;br /&gt;
inside of IO.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkName&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;mkName&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkInternalName&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkUnique&lt;/span&gt; &lt;span class=&quot;token char string&quot;&gt;&#39;n&#39;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkOccName&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;OccName&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;varName&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noSrcSpan&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;xn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;xn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkName&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;x&quot;&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkName&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkExternalName&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkUnique&lt;/span&gt; &lt;span class=&quot;token char string&quot;&gt;&#39;n&#39;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modl&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkOccName&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;OccName&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;varName&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;f&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noSrcSpan&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- a :: *&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyVar&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkTyVar&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;anyKind&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- x :: a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkLocalVar&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;VanillaId&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;xn&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TyVarTy&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;vanillaIdInfo&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- f :: a -&gt; a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkGlobalVar&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;VanillaId&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FunTy&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TyVarTy&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TyVarTy&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;vanillaIdInfo&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Syn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CoreBind&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Syn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NonRec&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fv&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Syn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Expr&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Syn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lam&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Syn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Var&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;modl&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;modl&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkModule&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mainPackageKey&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkModuleName&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So now we have a synthetic Core module that we can package up into a &lt;code&gt;ModGuts&lt;/code&gt;&lt;br /&gt;
and &lt;code&gt;ModSummary&lt;/code&gt; just like what we&#39;d get from the top half of the compiler&lt;br /&gt;
after Typechecking. For most of these fields we simply initialize them with&lt;br /&gt;
default dummy values.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModGuts&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModGuts&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt;          &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_exports&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Avail&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_deps&lt;/span&gt;            &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noDependencies&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_dir_imps&lt;/span&gt;        &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyModuleEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_used_names&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkNameSet&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_used_th&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_rdr_env&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyGlobalRdrEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_fix_env&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyFixityEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_tcs&lt;/span&gt;             &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_insts&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_fam_insts&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_patsyns&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_rules&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_binds&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;                  &lt;span class=&quot;token comment&quot;&gt;-- our binding&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_foreign&lt;/span&gt;         &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NoStubs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_warns&lt;/span&gt;           &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NoWarnings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_hpc_info&lt;/span&gt;        &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NoHpcInfo&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_modBreaks&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyModBreaks&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_vect_decls&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_vect_info&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noVectInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_boot&lt;/span&gt;            &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_anns&lt;/span&gt;            &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_inst_env&lt;/span&gt;        &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyInstEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_fam_inst_env&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptyFamInstEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_safe_haskell&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Sf_None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_trust_pkg&lt;/span&gt;       &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;mg_dependent_files&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;summ&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModSummary&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;summ&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModSummary&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_mod&lt;/span&gt;          &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modl&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_hsc_src&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HsSrcFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_location&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModLocation&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token hvariable&quot;&gt;ml_hs_file&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token hvariable&quot;&gt;ml_hi_file&lt;/span&gt;  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.hi&quot;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token hvariable&quot;&gt;ml_obj_file&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.o&quot;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_hs_date&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;UTCTime&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;toEnum&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_obj_date&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_iface_date&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_srcimps&lt;/span&gt;      &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_textual_imps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_hspp_file&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.hs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_hspp_opts&lt;/span&gt;    &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;ms_hspp_buf&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;code-generation&quot; tabindex=&quot;-1&quot;&gt;Code Generation&lt;/h4&gt;
&lt;p&gt;So now that we have our synthetic core module we can run the normal compiler&lt;br /&gt;
pipeline on it and compile it just like we would with source code. The&lt;br /&gt;
transformation of Core to native code goes through several processes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/CorePrep.html&quot;&gt;CorePrep&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/CoreToStg.html&quot;&gt;CoreToStg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/CoreLint.html&quot;&gt;Core Linting (Optional)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/SimplStg.html&quot;&gt;Stg2Stg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/AsmCodeGen.html&quot;&gt;Code Generation&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The main pass is called the Simplifier, which maps Core to Core. It&#39;s job it is&lt;br /&gt;
to perform a large collection of correctness-preserving transformations, with&lt;br /&gt;
the goal of producing a more efficient program.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Constant folding&lt;/li&gt;
&lt;li&gt;Applying the rewrite rules&lt;/li&gt;
&lt;li&gt;Inlining&lt;/li&gt;
&lt;li&gt;Case of case optimization&lt;/li&gt;
&lt;li&gt;Case of known constructor optimization&lt;/li&gt;
&lt;li&gt;Eta expansion and Eta reduction&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In addition to optimization GHC contains a pass called the Core Linter which&lt;br /&gt;
does a variety of internal consistency checks on a given Core program. Since&lt;br /&gt;
Core is explicitly typed, typechecking a given program is trivial as we simply&lt;br /&gt;
check that the type of identifiers matches with their bindings and that the&lt;br /&gt;
types are internally consistent. The Core linter is extremely useful if you&#39;re&lt;br /&gt;
writing optimization passes that transform Core or are targeting it from another&lt;br /&gt;
language (other than vanilla Haskell). The linter pass can prevent a whole slew&lt;br /&gt;
of bugs before they manifest as code generation errors.&lt;/p&gt;
&lt;p&gt;So now we&#39;ll run the rest of the compiler pipeline and generate assembly code&lt;br /&gt;
from our module. There&#39;s a bit of song and dance in converting between types but&lt;br /&gt;
the process is basically just connecting the inputs and outputs of the above&lt;br /&gt;
passes together.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscAsm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LinkBinary&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSession&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;setTargets&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Target&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;targetId&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TargetModule&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkModuleName&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;targetAllowObjCode&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;targetContents&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- repares for code generation.&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;prep&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;corePrepPgm&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;ms_location&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;summ&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_binds&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_tcs&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Transform Core into STG&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;stg&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;coreToStg&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;prep&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- STG Transformer&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;stg_binds2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cost_centre_info&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;stg2stg&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;stg&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Generated Cmm&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmms&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;codeGen&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_tcs&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cost_centre_info&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;stg_binds2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_hpc_info&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Initialize a name supply for the Cmm pipeline&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;us&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkSplitUniqSupply&lt;/span&gt; &lt;span class=&quot;token char string&quot;&gt;&#39;S&#39;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;initTopSRT&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;initUs_&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;us&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;emptySRT&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;run_pipeline&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmmPipeline&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Collect the Cmm code stream after running the pipeline.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmmstream&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
       &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;Stream&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mapAccumL&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;run_pipeline&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;initTopSRT&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmms&lt;/span&gt;
       &lt;span class=&quot;token hvariable&quot;&gt;Stream&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;yield&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;srtToData&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Prepare the Cmm for&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;genraw&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmmToRawCmm&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmmstream&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Initialize name supply for the native code generator and generate x86 to a&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;-- file from the prepared Cmm.&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;ncg_uniqs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkSplitUniqSupply&lt;/span&gt; &lt;span class=&quot;token char string&quot;&gt;&#39;n&#39;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;openFile&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.asm&quot;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;WriteMode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;nativeCodeGen&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guts&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modloc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fname&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ncg_uniqs&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;genraw&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;-- Dump the outputted Stg and  Cmm out&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;Stream&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;collect&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cmmstream&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;=== STG ===&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;stg_binds2&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;=== CMM ===&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;gen&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we get a &lt;code&gt;Example.asm&lt;/code&gt; file outputted from our synthetic core module.&lt;br /&gt;
A next step project would be to target a more complicated language to GHC Core&lt;br /&gt;
to take advantage of it&#39;s compiler optimizations and native code generation.&lt;/p&gt;
</description>
      <pubDate>Tue, 12 Jul 2016 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ghc_03/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ghc_03/</guid>
    </item>
    
    <item>
      <title>Dive into GHC: Intermediate Forms</title>
      <description>&lt;h3 id=&quot;dive-into-ghc%3A-intermediate-forms&quot; tabindex=&quot;-1&quot;&gt;Dive into GHC: Intermediate Forms&lt;/h3&gt;
&lt;p&gt;In the &lt;a href=&quot;http://www.stephendiehl.com/posts/ghc_01.html&quot;&gt;last blog post&lt;/a&gt;, we&lt;br /&gt;
discussed the high-level API used inside the GHC compiler. This time let&#39;s zoom&lt;br /&gt;
into the pieces that comprise the frontend of the compilation pipeline, namely:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Parsing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Renaming&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Typechecking&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Desugaring&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simplification&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Code Generation&lt;/li&gt;
&lt;/ol&gt;
&lt;p style=&quot;text-align:center&quot;&gt;
**[Accompaying Source Code](https://github.com/sdiehl/dive-into-ghc/tree/master/02-parser)**
&lt;/p&gt;
&lt;h4 id=&quot;passes&quot; tabindex=&quot;-1&quot;&gt;Passes&lt;/h4&gt;
&lt;p&gt;The following functions are the main passes are that are used inside of the&lt;br /&gt;
compiler driver functions in &lt;code&gt;HscMain&lt;/code&gt; .&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;-- Parse a module.&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;parseModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModSummary&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedModule&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- Typecheck and rename a parsed module.&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;typecheckModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TypecheckedModule&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- Desugar a typechecked module.&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;desugarModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TypecheckedModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DesugaredModule&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- Extract ModGuts&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;coreModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DesugaredMod&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModGuts&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;-- Generated STG bindings from Core module&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;coreToStg&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreProgram&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;StgBinding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each of the these functions manipulates an &lt;em&gt;intermediate form&lt;/em&gt; used in the&lt;br /&gt;
process of compiling a Haskell module into a form that can be compiled into&lt;br /&gt;
machine code or interpreted.&lt;/p&gt;
&lt;h4 id=&quot;parser&quot; tabindex=&quot;-1&quot;&gt;Parser&lt;/h4&gt;
&lt;p&gt;The frontend syntax for Haskell is fairly large and is partially defined in the&lt;br /&gt;
&lt;a href=&quot;https://www.haskell.org/onlinereport/haskell2010/haskellch10.html#x17-17500010&quot;&gt;Haskell 2010&lt;br /&gt;
specification&lt;/a&gt;&lt;br /&gt;
and extended by GHC. The parser is written using two custom tools:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Alex&lt;/em&gt;, for lexical analysis. See: &lt;a href=&quot;https://github.com/ghc/ghc/blob/master/compiler/parser/Lexer.x&quot;&gt;Lexer.x&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Happy&lt;/em&gt;, for the parser itself. See: &lt;a href=&quot;https://github.com/ghc/ghc/blob/master/compiler/parser/Parser.y&quot;&gt;Parser.y&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Happy is a parser generator system for Haskell, similar to the tool &lt;code&gt;yacc&#39; for C. It works as a preprocessor with it&#39;s own syntax that generates a parse table from two specifications, a lexer file and parser file. The Happy parser consists of a list of a production rules and a monad to running the parser in. Production rules consist of a set of options on the left and generating Haskell expressions on the right with indexed variables (&lt;/code&gt;$1&lt;code&gt;, &lt;/code&gt;$2&lt;code&gt;, ...) mapping to the ordered terms on the left (i.e. in the second term &lt;/code&gt;term&lt;code&gt;~&lt;/code&gt;$1&lt;code&gt;, &lt;/code&gt;term&lt;code&gt;~&lt;/code&gt;$2`).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-perl&quot;&gt;terms
    : term                   { [$1] }
    | term terms             { $1 : $2 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most complicated part of the lexer is with regards to whitespace-sensitive&lt;br /&gt;
layout. To handle whitespace the lexer will analyze the line position of certain&lt;br /&gt;
productions and will output &lt;em&gt;virtual tokens&lt;/em&gt; (vbrace, vsemi, etc) which are used&lt;br /&gt;
to canonicalize the syntax. For example the use of layout rule in do-notation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Haskell&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is converted into explicit virtual braces and semicolons at parse time.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hello&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Haskell&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The main entry points are defined in the&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/Parser.html&quot;&gt;Parser&lt;/a&gt;&lt;br /&gt;
and resolve into a monad &lt;code&gt;P&lt;/code&gt; which holds state about the source buffer, and&lt;br /&gt;
filename and location. The resulting AST is called HsSyn and contains position&lt;br /&gt;
information preserved from the tokens.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;parseType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LHsType&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RdrName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;parseExpression&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LHsExpr&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RdrName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;parseModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;P&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Located&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;HsModule&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RdrName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;located&quot; tabindex=&quot;-1&quot;&gt;Located&lt;/h4&gt;
&lt;p&gt;Frontend syntax in GHC carries position information along with it that can be&lt;br /&gt;
used in error reporting. Location information is defined by a &lt;code&gt;Located&lt;/code&gt; type&lt;br /&gt;
which has a single type paramater for the AST element to be &amp;quot;located&amp;quot;. Typically&lt;br /&gt;
type synonyms follow the convention that a &amp;quot;L&amp;quot; before the type indicates a&lt;br /&gt;
synonym for a located element. So a located &lt;code&gt;HsExpr&lt;/code&gt; is a &lt;code&gt;LHsExxpr&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GenLocated&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Located&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GenLocated&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SrcSpan&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;unLoc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GenLocated&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;unLoc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;getLoc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GenLocated&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;getLoc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;noLoc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Located&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;noLoc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;noSrcSpan&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Most notably there is a functor instance for &lt;code&gt;fmap&lt;/code&gt;ing over a located element,&lt;br /&gt;
which modifies the underlying type preserving the location.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;GenLocated&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;frontend-syntax&quot; tabindex=&quot;-1&quot;&gt;Frontend Syntax&lt;/h4&gt;
&lt;p&gt;The &lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/HsSyn.html&quot;&gt;frontend&lt;br /&gt;
syntax&lt;/a&gt;&lt;br /&gt;
(called HsSyn) is spread across several data types which contain all possible&lt;br /&gt;
syntax elements generated from the parser. These are parameterized over the name&lt;br /&gt;
type so that transformations (renamer, etc) can alter the structure of bindings&lt;br /&gt;
and identifiers while preserving the syntax tree structure.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;HsModule&lt;/strong&gt; - Top-level structure for a module. Contains module name,&lt;br /&gt;
exports, imports, and toplevel declarations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsDecl&lt;/strong&gt; - Type, class, value, and interface signature decls&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsBind&lt;/strong&gt; - Value bindings used for both top level and nested bindings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsGroup&lt;/strong&gt; - A HsGroup is a group of related HsDecls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsLit&lt;/strong&gt; - Literal value (Int, Char, etc)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsExpr&lt;/strong&gt; - A Haskell expression.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HsType&lt;/strong&gt; - Frontend type annotation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pat&lt;/strong&gt; - Pattern match in a case statement or toplevel binding.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There are many variants of&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/Var.html#t:Var&quot;&gt;identifiers&lt;/a&gt;&lt;br /&gt;
used in different phases of compilation. In order of generality:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;OccName&lt;/strong&gt; - Simplest name used, it holds the names as strings with the namespace that the name came from and position.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RdrName&lt;/strong&gt; - Is the type of names that come directly from the parser. Contains information whether the name is unqualified or qualified (with module name).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Name&lt;/strong&gt; - Is the type of names that have had their scoping and binding resolved. They have an OccName but also a Unique that disambiguates Names&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Var&lt;/strong&gt; - Is the type of names that not only have a Name but also a Type. Vars are either global or local.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Names are disambiguated from each other by an internal unique numerical key&lt;br /&gt;
called a&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/Unique.html#t:Unique&quot;&gt;Unique&lt;/a&gt;&lt;br /&gt;
, which are generated from a&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/UniqSupply.html&quot;&gt;UniqSupply&lt;/a&gt;&lt;br /&gt;
. These are often embedded within the monad transformer &lt;code&gt;MonadUnique&lt;/code&gt; layer of&lt;br /&gt;
the pass.&lt;/p&gt;
&lt;p&gt;After typechecking we deal with Var and Id names which have the property that&lt;br /&gt;
the Name and Type can always be recovered using the following functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;varName&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;varType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Kind&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition, many lookup functions use a sum type &lt;code&gt;TyThing&lt;/code&gt; which wraps&lt;br /&gt;
&amp;quot;things&amp;quot; in scope which have types, such as identifiers and type constructors.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyThing&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;AnId&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Id&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;AConLike&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ConLike&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ConLike&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ATyCon&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TyCon&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ACoAxiom&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;CoAxiom&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Branched&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;outputable&quot; tabindex=&quot;-1&quot;&gt;Outputable&lt;/h4&gt;
&lt;p&gt;Inside the GHC API is a pretty printer which can be used to dump out a pretty&lt;br /&gt;
printed summary of a large number of the internal types. The structure of the&lt;br /&gt;
class is similar to &lt;code&gt;Show&lt;/code&gt; typeclass.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Outputable&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;ppr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SDoc&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;pprPrec&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Rational&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SDoc&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;showPpr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Outputable&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To simplify our life a bit we can write a wrapper &lt;code&gt;showGhc&lt;/code&gt; which uses the&lt;br /&gt;
default dynflags to dump out an instance of &lt;code&gt;Outputable&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Outputable&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showPpr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unsafeGlobalDynFlags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;tracing-compilation&quot; tabindex=&quot;-1&quot;&gt;Tracing Compilation&lt;/h4&gt;
&lt;p&gt;Given the following example module we&#39;d like to trace the transformation of the&lt;br /&gt;
intermediate forms as it&#39;s run through the compiler.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Example&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Animal&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the machinery of the last post we import the module target, load it, and&lt;br /&gt;
then instead of adding it to an interactive context we explicitly call the&lt;br /&gt;
functions of the individual passes and dump our their structure to the screen.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSession&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscInterpreted&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guessTarget&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.hs&quot;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setTargets&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LoadAllTargets&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;modSum&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getModSummary&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mkModuleName&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example&quot;&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;pmod&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;parseModule&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modSum&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;-- ModuleSummary&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;typecheckModule&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pmod&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;-- TypecheckedSource&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dmod&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;desugarModule&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;-- DesugaredModule&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;core&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;coreModule&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dmod&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;-- ModGuts&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;stg&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;coreToStg&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mg_binds&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;core&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Parsed Source&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;parsedSource&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pmod&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Renamed Module&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_renamed_source&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Typechecked Module&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_typechecked_source&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Typed Toplevel Definitions&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modInfoTyThings&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;moduleInfo&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Typed Toplevel Exports&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;modInfoExports&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;moduleInfo&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tmod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Core Module&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_binds&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;core&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;banner&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;STG&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;showGhc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;stg&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;parsing&quot; tabindex=&quot;-1&quot;&gt;Parsing&lt;/h4&gt;
&lt;p&gt;From the module summary we can extract the underlying text stream of the source&lt;br /&gt;
file &lt;code&gt;Example.hs&lt;/code&gt;. Running the parser over this generates a &lt;em&gt;ParsedSource&lt;/em&gt;&lt;br /&gt;
datatype which contains the HsSyn AST with raw OccNames.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedModule&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pm_mod_summary&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModSummary&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pm_parsed_source&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedSource&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pm_extra_src_files&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pm_annotations&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ApiAnns&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;===============================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Parsed&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Source&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===============================&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Example&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Animal&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h4 id=&quot;renaming&quot; tabindex=&quot;-1&quot;&gt;Renaming&lt;/h4&gt;
&lt;p&gt;After parsing we run the renamer pass which is responsible for resolving scopes,&lt;br /&gt;
operator precedences, and unique&#39;ifying all names so that clashes cannot occur.&lt;br /&gt;
The renamer resolves &lt;code&gt;RdrName&lt;/code&gt;s into &lt;code&gt;Name&lt;/code&gt;s across all syntax elements.&lt;br /&gt;
In addition it annotates each name with whether they are qualified and global or&lt;br /&gt;
local to the current module. If a name is not in scope, compilation simply halts&lt;br /&gt;
here and reports the missing name.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;===============================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Renamed&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===============================&lt;/span&gt;
&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;

      &lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Animal&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;implicit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Prelude&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h4 id=&quot;typechecking&quot; tabindex=&quot;-1&quot;&gt;Typechecking&lt;/h4&gt;
&lt;p&gt;The renamed AST is then run through the typechecker which maps &lt;code&gt;Name&lt;/code&gt;s into&lt;br /&gt;
&lt;code&gt;Id&lt;/code&gt; and enriches them with inferred types using type inference.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TypecheckedModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TypecheckedModule&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_parsed_module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ParsedModule&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_renamed_source&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RenamedSource&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_typechecked_source&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;TypecheckedSource&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_checked_module_info&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;tm_internals_&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TcRnTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;TcGblEnv&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ModDetails&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting structure is a typechecked module which contains the toplevel&lt;br /&gt;
types of all bindings and all subexpressions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;=============================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Typechecked&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=============================&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;AbsBinds&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
   &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Exports&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Exported&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;types&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
                    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LclId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Str&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;DmdType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Binds&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting datatype has a field for &lt;code&gt;ModuleInfo&lt;/code&gt;, which contains various&lt;br /&gt;
metadata about the typing environment, the names in scope, and typeclasses&lt;br /&gt;
exported.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_type_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;TypeEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_exports&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;NameSet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NameSet&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_rdr_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;RdrName&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;GlobalRdrEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_instances&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ClsInst&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_iface&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModIface&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_safe&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SafeHaskellMode&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minf_modBreaks&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModBreaks&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using the &lt;code&gt;coreModule&lt;/code&gt; we can also construct the gruesomely named &lt;code&gt;ModGuts&lt;/code&gt;&lt;br /&gt;
datatype which contains an expanded form of all the various exports and types&lt;br /&gt;
with associated metadata.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModGuts&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModGuts&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_boot&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IsBootInterface&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_exports&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Avail&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;AvailInfo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_deps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Dependencies&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_dir_imps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ImportedMods&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_used_names&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;NameSet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;NameSet&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_used_th&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_rdr_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;RdrName&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;GlobalRdrEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_fix_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FixityEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_tcs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TyCon&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_insts&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ClsInst&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_fam_insts&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FamInst&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_patsyns&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PatSyn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;PatSyn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_rules&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;CoreSyn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CoreRule&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_binds&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;CoreSyn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CoreProgram&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_foreign&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ForeignStubs&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_warns&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Warnings&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_anns&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Annotations&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Annotation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_hpc_info&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;HpcInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_modBreaks&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ModBreaks&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_vect_decls&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;CoreSyn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CoreVect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_vect_info&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;VectInfo&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_inst_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;InstEnv&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;InstEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_fam_inst_env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FamInstEnv&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;FamInstEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_safe_haskell&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SafeHaskellMode&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_trust_pkg&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mg_dependent_files&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The ModuleInfo can be queried for any type of information about types or names&lt;br /&gt;
at this point. For instance if we were interesed in the types of a global name&lt;br /&gt;
we could look it up as a &lt;code&gt;TyThing&lt;/code&gt; inside the typing environment.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;modInfoTyThings&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;TyThing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;modInfoExports&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleInfo&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;=========================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Typed&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Toplevel&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Definitions&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=========================&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;constructor&lt;/span&gt; ‘&lt;span class=&quot;token constant&quot;&gt;Animal&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;constructor&lt;/span&gt; ‘&lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;token constant&quot;&gt;Data&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;constructor&lt;/span&gt; ‘&lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Identifier&lt;/span&gt; ‘&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Identifier&lt;/span&gt; ‘&lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;token constant&quot;&gt;Identifier&lt;/span&gt; ‘&lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;’&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;===========================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Typed&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Toplevel&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Exports&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;===========================&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h4 id=&quot;core&quot; tabindex=&quot;-1&quot;&gt;Core&lt;/h4&gt;
&lt;p&gt;The Core language is GHC&#39;s most central data types. Core is a very small,&lt;br /&gt;
explicitly-typed, variant of System FC. The output of the &lt;code&gt;typecheckModule&lt;/code&gt;&lt;br /&gt;
function is a datatype &lt;code&gt;CoreModule&lt;/code&gt; which contains the Core program as the&lt;br /&gt;
typing environment.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreModule&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreModule&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cm_module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cm_types&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;HscTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;TypeEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cm_binds&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CoreSyn&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CoreProgram&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;cm_safe&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SafeHaskellMode&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Looking at the core module we see that each name in the program is now annotated&lt;br /&gt;
with an explicit type and typeclass dictionaries have been inserted.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;================================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Core&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;================================&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
 &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LclIdX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Str&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;DmdType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
 &lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&#92;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fNumInt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Core then is translated into a intermediate form that is amenable to various&lt;br /&gt;
analysis passes that optimize the program for code generation. This form is an&lt;br /&gt;
abstract machine known a &lt;em&gt;Spineless Tagless G-Machine&lt;/em&gt;. In this syntax every&lt;br /&gt;
closure has an associated &lt;em&gt;Static Reference Table&lt;/em&gt; or SRT. More on this later.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;====================================&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;STG&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;====================================&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt;
 &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;LclIdX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Str&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;DmdType&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
     &lt;span class=&quot;token operator&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;srt&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;SRT&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;r6ry&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:-&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fNumInt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fNumInt&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;h4 id=&quot;summary-%26-next-steps&quot; tabindex=&quot;-1&quot;&gt;Summary &amp;amp; Next Steps&lt;/h4&gt;
&lt;p&gt;We mentioned Core and STG only briefly. The process by which frontend syntax is&lt;br /&gt;
transformed into Core will be our next topic. The design of Core defines a great&lt;br /&gt;
deal of aspects about how the language is structured and informs the type system&lt;br /&gt;
as well as code-generation.&lt;/p&gt;
</description>
      <pubDate>Mon, 11 Jul 2016 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ghc_02/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ghc_02/</guid>
    </item>
    
    <item>
      <title>Dive into GHC: Pipeline</title>
      <description>&lt;h3 id=&quot;dive-into-ghc%3A-pipeline&quot; tabindex=&quot;-1&quot;&gt;Dive into GHC: Pipeline&lt;/h3&gt;
&lt;p&gt;After reading &lt;a href=&quot;https://ghc.haskell.org/trac/ghc/blog/ContributingToGhc&quot;&gt;Simon&#39;s&lt;/a&gt;&lt;br /&gt;
call for more volunteer writing about GHC I thought it would be timely to share&lt;br /&gt;
some knowledge I&#39;ve accumulated over the years about working with the with GHC&lt;br /&gt;
internals.&lt;/p&gt;
&lt;p&gt;I&#39;m by no means an expert on GHC internals, but I have worked with them a fair&lt;br /&gt;
bit for several projects and the deep dive style of blog posts tends to be a&lt;br /&gt;
good format for helping ease into exploring the code for themselves. Often&lt;br /&gt;
times simply a high-level overview and a small bit of runnable example code is&lt;br /&gt;
enough to encourage further involvement with an open source project and this&lt;br /&gt;
what I aim to write.&lt;/p&gt;
&lt;p&gt;So begins a multipart writeup on the structure of GHC structured around several&lt;br /&gt;
examples that use the GHC API for some small project that shows off some&lt;br /&gt;
internal structure of the compiler.&lt;/p&gt;
&lt;p style=&quot;text-align:center&quot;&gt;
**[Accompaying Source Code](https://github.com/sdiehl/dive-into-ghc/tree/master/01-pipeline)**
&lt;/p&gt;
&lt;hr /&gt;
&lt;h4 id=&quot;official-commentary&quot; tabindex=&quot;-1&quot;&gt;Official Commentary&lt;/h4&gt;
&lt;p&gt;GHC core developers have actually spent a great deal of time over the sharing&lt;br /&gt;
knowledge about the design of the compiler. Some good places to start are the&lt;br /&gt;
following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://ghc.haskell.org/trac/ghc/wiki/Commentary&quot;&gt;GHC Commentary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=6COvD8oynmI&quot;&gt;OPLSS: Adventures in Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://ghc.haskell.org/trac/ghc/wiki/AboutVideos&quot;&gt;2006 GHC Hackathon Videos&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://davidterei.com/talks/2016-03-cs240h/ghc-compiler.html&quot;&gt;David Terei&#39;s Notes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://takenobu-hs.github.io/downloads/haskell_ghc_illustrated.pdf&quot;&gt;Takenobu Tani&#39;s GHC Illustrated&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://blog.ezyang.com/2011/04/tracing-the-compilation-of-hello-factorial/&quot;&gt;Edward Yang&#39;s Blog&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;On top of this there is a literature trail going back 25 years that shows how&lt;br /&gt;
the historical context and the research that led up to GHC today.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://ghc.haskell.org/trac/ghc/wiki/ReadingList&quot;&gt;GHC Reading List&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;toplevel&quot; tabindex=&quot;-1&quot;&gt;Toplevel&lt;/h4&gt;
&lt;p&gt;GHC is a quirky beast of a codebase, but as far compilers go it is a fairly&lt;br /&gt;
well-engineered and documented project if you know where to look. Yes, it uses a&lt;br /&gt;
somewhat idiosyncratic convention in places, but after all it is a 20-year old&lt;br /&gt;
codebase.&lt;/p&gt;
&lt;p&gt;To get the source for the compiler clone the official repo:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;git&lt;/span&gt; clone &lt;span class=&quot;token parameter variable&quot;&gt;--recursive&lt;/span&gt; git://git.haskell.org/ghc.git
$ &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; ghc/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are many utilities included with the compiler the encompass documentation&lt;br /&gt;
and the build system, but the important toplevel directories for the compiler&lt;br /&gt;
itself are primarily:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;├── rts          &lt;span class=&quot;token comment&quot;&gt;# The Haskell runtime systems&lt;/span&gt;
├── compiler     &lt;span class=&quot;token comment&quot;&gt;# The Haskell compiler logic&lt;/span&gt;
├── includes     &lt;span class=&quot;token comment&quot;&gt;# Header files for runtime and code generation&lt;/span&gt;
└── libraries    &lt;span class=&quot;token comment&quot;&gt;# The base libraries and Prelude source&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For this post we&#39;ll concern ourselves with the &lt;code&gt;compiler&lt;/code&gt; folder.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;├── basicTypes   &lt;span class=&quot;token comment&quot;&gt;# Types used across all modules&lt;/span&gt;
├── cbits        &lt;span class=&quot;token comment&quot;&gt;# Misc C utilities&lt;/span&gt;
├── cmm          &lt;span class=&quot;token comment&quot;&gt;# Cmm langauge definitions&lt;/span&gt;
├── codeGen      &lt;span class=&quot;token comment&quot;&gt;# Cmm Compilers&lt;/span&gt;
├── coreSyn      &lt;span class=&quot;token comment&quot;&gt;# Core language definitions&lt;/span&gt;
├── deSugar      &lt;span class=&quot;token comment&quot;&gt;# Desugarer&lt;/span&gt;
├── ghci         &lt;span class=&quot;token comment&quot;&gt;# Interactive shell&lt;/span&gt;
├── hsSyn        &lt;span class=&quot;token comment&quot;&gt;# Frontend syntax&lt;/span&gt;
├── iface        &lt;span class=&quot;token comment&quot;&gt;# Interface files&lt;/span&gt;
├── llvmGen      &lt;span class=&quot;token comment&quot;&gt;# LLVM Code generator&lt;/span&gt;
├── main         &lt;span class=&quot;token comment&quot;&gt;# Compiler driver logic and options&lt;/span&gt;
├── nativeGen    &lt;span class=&quot;token comment&quot;&gt;# Assemblers for x86 / SPARC / PPC&lt;/span&gt;
├── parser       &lt;span class=&quot;token comment&quot;&gt;# Frontend Parser for HsSyn&lt;/span&gt;
├── prelude      &lt;span class=&quot;token comment&quot;&gt;# Wired-In Types /  Primops and Builtins&lt;/span&gt;
├── profiling    &lt;span class=&quot;token comment&quot;&gt;# Runtime profiing tools&lt;/span&gt;
├── &lt;span class=&quot;token function&quot;&gt;rename&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Frontend renamer&lt;/span&gt;
├── simplCore    &lt;span class=&quot;token comment&quot;&gt;# Core-To-Core simplifier&lt;/span&gt;
├── simplStg     &lt;span class=&quot;token comment&quot;&gt;# Stg-To-Stg simplifier&lt;/span&gt;
├── specialise   &lt;span class=&quot;token comment&quot;&gt;# Specialisation pass ( Eliminates Overloading )&lt;/span&gt;
├── stgSyn       &lt;span class=&quot;token comment&quot;&gt;# Stg Core Language&lt;/span&gt;
├── stranal      &lt;span class=&quot;token comment&quot;&gt;# Strictness Analyzer&lt;/span&gt;
├── typecheck    &lt;span class=&quot;token comment&quot;&gt;# Typechecker&lt;/span&gt;
├── types        &lt;span class=&quot;token comment&quot;&gt;# Type language, data constructors, and type families&lt;/span&gt;
├── utils        &lt;span class=&quot;token comment&quot;&gt;# Misc functions and core data structures&lt;/span&gt;
└── vectorise    &lt;span class=&quot;token comment&quot;&gt;# Vectorisation optimiations&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;ghc-api&quot; tabindex=&quot;-1&quot;&gt;GHC API&lt;/h4&gt;
&lt;p&gt;Since GHC is itself written in Haskell, GHC is effectively a large library the&lt;br /&gt;
encompasses the &lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/index.html&quot;&gt;GHC&lt;br /&gt;
API&lt;/a&gt;.&lt;br /&gt;
The toplevel module is simply called&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/GHC.html&quot;&gt;GHC&lt;/a&gt;&lt;br /&gt;
and contains a namespace dump of many of the core types that drive the&lt;br /&gt;
&lt;em&gt;compilation pipeline&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Beneath this is the main API for compiling plain Haskell source code called&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/HscMain.html&quot;&gt;HscMain&lt;/a&gt;&lt;br /&gt;
which contains the various drivers for different passes within the compilation.&lt;br /&gt;
The six core passes make up the &lt;a href=&quot;http://www.aosabook.org/images/ghc/hscpipe2.png&quot;&gt;compilation&lt;br /&gt;
pipeline&lt;/a&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Parsing&lt;/li&gt;
&lt;li&gt;Renaming&lt;/li&gt;
&lt;li&gt;Typechecking&lt;/li&gt;
&lt;li&gt;Desugaring&lt;/li&gt;
&lt;li&gt;Simplification&lt;/li&gt;
&lt;li&gt;Code Generation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The result of this compilation is several artificats which are &lt;em&gt;object files&lt;/em&gt;&lt;br /&gt;
(.o), &lt;em&gt;interface files&lt;/em&gt; (.hi) and &lt;em&gt;executables&lt;/em&gt;.&lt;/p&gt;
&lt;h4 id=&quot;ghc-monad&quot; tabindex=&quot;-1&quot;&gt;GHC Monad&lt;/h4&gt;
&lt;p&gt;The heart of the compilation process is stored within the GHC Monad, a state&lt;br /&gt;
monad that handles the internal session state of the compilation pipeline, error&lt;br /&gt;
handling and sequencing of multi-module compilation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;unGhc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Session&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The abstract class &lt;code&gt;GhcMonad&lt;/code&gt; provides a lifted version of the GHC monad&lt;br /&gt;
functions to get at the internal session objects from within the various&lt;br /&gt;
submonads used throughout compilation (renamer, typechecker, etc).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;MonadIO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ExceptionMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HasDynFlags&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;getSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The evaluation function takes in a path to the &lt;code&gt;libdir&lt;/code&gt; and returns the result&lt;br /&gt;
inside of &lt;code&gt;IO&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The filepaths are installation specific paths indicating the local installation&lt;br /&gt;
and paths to the GHC compiler. These are provided by the &lt;a href=&quot;https://hackage.haskell.org/package/ghc-paths&quot;&gt;ghc-paths&lt;/a&gt;&lt;br /&gt;
package.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; GHC&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Paths&lt;/span&gt;

&lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;docdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghc_pkg&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At the heart of the session object is a very important structure called&lt;br /&gt;
&lt;code&gt;HscEnv&lt;/code&gt; which holds the internal state of compilation.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hsc_dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hsc_targets&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Target&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hsc_mod_graph&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleGraph&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hsc_IC&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;InteractiveContext&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hsc_HPT&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HomePackageTable&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;-- Many more ... (truncated for brevity)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;hsc_dflags&lt;/code&gt; holds the settings objects (more on this next). The&lt;br /&gt;
&lt;code&gt;hsc_targets&lt;/code&gt; holds the roots of the Module graph which are traversed&lt;br /&gt;
bottom-up to build up the entire set of modules needed for compilation of the&lt;br /&gt;
current package. The entire set of modules involved in this (roots and&lt;br /&gt;
non-roots) is stored in &lt;code&gt;hsc_mod_graph&lt;/code&gt; which holds the whole ModuleGraph,&lt;br /&gt;
which is not necessarily in topological order. The &lt;code&gt;hsc_IC&lt;/code&gt; field contains the&lt;br /&gt;
interactive context which is used for the interactive shell and for when the end&lt;br /&gt;
targets are linked in memory. Specific commands in GHCi such as adding modules&lt;br /&gt;
to the top-level scope modifying this structure state fully.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;hsc_HPT&lt;/code&gt; holds the home package table which describes already-compiled&lt;br /&gt;
home-package modules, When a module done being compiled, and is loaded with&lt;br /&gt;
&lt;code&gt;loadModule&lt;/code&gt; it is internally added to this mapping.&lt;/p&gt;
&lt;h4 id=&quot;dynflags&quot; tabindex=&quot;-1&quot;&gt;DynFlags&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.2/docs/html/libraries/ghc-7.10.2/DynFlags.html#t:DynFlags&quot;&gt;DynFlags&lt;/a&gt;&lt;br /&gt;
contains a collection of flags relating to the compilation of a single file or&lt;br /&gt;
GHC session. This is the core datatype that informs how compilation occurs and&lt;br /&gt;
is passed to most of the various pass functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcMode&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMode&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcLink&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscTarget&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;settings&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Settings&lt;/span&gt;

  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;DynFlag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;extensionFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ExtensionFlag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pkgState&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;PackageState&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pkgDatabase&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PackageConfig&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;packageEnv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;packageFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PackageFlag&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;extraPkgConfs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PkgConfRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PkgConfRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;-- Many more flags... (truncated for brevity)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;GhcMode&lt;/code&gt; informs whether we&#39;re doing multi-module compilation or one-shot&lt;br /&gt;
single-file compilation. In the case of multi-module the ModuleGraph is built up&lt;br /&gt;
via the&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/Finder.html&quot;&gt;Finder&lt;/a&gt;&lt;br /&gt;
function which searches the home package for the dependent modules.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GhcMode&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;CompManager &lt;code&gt;--make&lt;/code&gt;&lt;br /&gt;
OneShot &lt;code&gt;ghc -c Foo.hs&lt;/code&gt;&lt;br /&gt;
MkDepend &lt;code&gt;ghc -M&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;HscTarget&lt;/code&gt; datatype defines the target code type of the compilation. By&lt;br /&gt;
default this is &lt;code&gt;HscAsm&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;HscTarget&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;HscC Generate C code.&lt;br /&gt;
HscAsm Generate assembly using the native code generator.&lt;br /&gt;
HscLlvm Generate assembly using the llvm code generator.&lt;br /&gt;
HscInterpreted Generate bytecode.&lt;br /&gt;
HscNothing Don&#39;t generate any code. See notes above.&lt;/p&gt;
&lt;p&gt;After compilation is done (for multi-module) GHC then begins the linker phase&lt;br /&gt;
and the &lt;code&gt;GhcLink&lt;/code&gt; setting determines what to do with the resulting object&lt;br /&gt;
files.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GhcLink&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;NoLink Don&#39;t link at all&lt;br /&gt;
LinkBinary Link object code into a binary&lt;br /&gt;
LinkInMemory Use the in-memory dynamic linker (works for both bytecode and object code).&lt;br /&gt;
LinkDynLib Link objects into a dynamic lib (DLL on Windows, DSO on ELF platforms)&lt;br /&gt;
LinkStaticLib Link objects into a static lib&lt;/p&gt;
&lt;p&gt;The simplest initializer of a GHC session simply uses the defaults and sets up a&lt;br /&gt;
interpreted session that links any modules it is given in memory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscInterpreted&lt;/span&gt;
                              &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LinkInMemory&lt;/span&gt;
                              &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;GHC exposes many compiler flags on the commandline and these are themselves&lt;br /&gt;
reflected in various subfields of the &lt;code&gt;DynFlags&lt;/code&gt; struct. The three major&lt;br /&gt;
classes of flags are &lt;code&gt;DumpFlag&lt;/code&gt; (example: &lt;code&gt;-ddump-simpl&lt;/code&gt;), GeneralFlag&lt;br /&gt;
(example: &lt;code&gt;-fspec-constr&lt;/code&gt;) and ExtensionFlag (example: &lt;code&gt;-XTypeInType&lt;/code&gt;).&lt;br /&gt;
There are various helper functions that modifying the DynFlags to twiddle these&lt;br /&gt;
flags on or off.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;dopt_set&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DumpFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;gopt_set&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GeneralFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;xopt_set&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ExtensionFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Through the compilation GHC will query the state of these flags to dispatch to&lt;br /&gt;
different codepaths based on whether a language extension is set or other flag&lt;br /&gt;
behavior. This is done through querying the &lt;code&gt;GhcMonad&lt;/code&gt; instance to get the&lt;br /&gt;
dynflags and using one of the various flag specific functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;xopt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ExtensionFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;gopt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GeneralFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;dopt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DumpFlag&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;DynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To enable various flags we use modify the current &lt;code&gt;dflags&lt;/code&gt; object using the&lt;br /&gt;
flag set functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscInterpreted&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LinkInMemory&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
               &lt;span class=&quot;token operator&quot;&gt;`dopt_set`&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Opt_D_dump_BCOs&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;-- Set Dump Flag&lt;/span&gt;
               &lt;span class=&quot;token operator&quot;&gt;`xopt_set`&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Opt_OverloadedStrings&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;-- Set Language Extension Flag&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;compilation&quot; tabindex=&quot;-1&quot;&gt;Compilation&lt;/h4&gt;
&lt;p&gt;To start compilation we first add a target to the state. This modifies the&lt;br /&gt;
&lt;code&gt;hsc_targets&lt;/code&gt; field of the environment. To two types of targets are either&lt;br /&gt;
module names or filenames. The &lt;code&gt;guessTarget&lt;/code&gt; will discriminate on the given&lt;br /&gt;
string&#39;s extension it to determine which target object to create.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;addTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Target&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;guessTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Phase&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Targets specify the source files or modules at the top of the dependency tree.&lt;br /&gt;
For a executable program there is just a single target &lt;code&gt;Main.hs&lt;/code&gt;, for a&lt;br /&gt;
library the targets are visible module in the library.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Target&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;TargetModule A module name: search for the file&lt;br /&gt;
TargetFile FilePath A filename: preprocess and parse it to find the module name.&lt;/p&gt;
&lt;p&gt;If with the modules added to the state we can then perform dependency analsysis&lt;br /&gt;
to determine the module graph to proceed with multi-module compilation.&lt;br /&gt;
Dependency analysis entails parsing the import directives of the module and&lt;br /&gt;
resolving the &lt;code&gt;ModuleGraph&lt;/code&gt; which is a type alias for a list of&lt;br /&gt;
&lt;code&gt;ModuleSummary&lt;/code&gt; which contains the targets. This is performed by the&lt;br /&gt;
&lt;code&gt;depanal&lt;/code&gt; function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;depanal&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;ModuleName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;ModuleGraph&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After a target is created the compiler is then run on the module yielding the&lt;br /&gt;
resulting artifacts and it is loaded into the home package table. This is&lt;br /&gt;
accomplished via the &lt;code&gt;load&lt;/code&gt; command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LoadHowMuch&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SuccessFlag&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;LoadHowMuch&lt;/strong&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;LoadAllTargets Load all targets and its dependencies.&lt;br /&gt;
LoadUpTo Load only the given module and its dependencies.&lt;br /&gt;
LoadDependenciesOf Load only the dependencies of the given module, but not the module itself.&lt;/p&gt;
&lt;p&gt;A full example of this would be the compilation of a module &lt;code&gt;Example.hs&lt;/code&gt; in&lt;br /&gt;
the current working directory that is interpreted and linked in memory.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscInterpreted&lt;/span&gt;
                              &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt;   &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LinkInMemory&lt;/span&gt;
                              &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token hvariable&quot;&gt;target&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;guessTarget&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Example.hs&quot;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;addTarget&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;target&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LoadAllTargets&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;interactive-context&quot; tabindex=&quot;-1&quot;&gt;Interactive Context&lt;/h4&gt;
&lt;p&gt;On top of simply generating compiler artifacts. GHC can compile and link code&lt;br /&gt;
into memory to be evaluated interactively. The state of the interpreter backing&lt;br /&gt;
this is held in the&lt;br /&gt;
&lt;a href=&quot;https://downloads.haskell.org/~ghc/7.10.3/docs/html/libraries/ghc-7.10.3/HscTypes.html#t:InteractiveContext&quot;&gt;InteractiveContext&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The set of modules in the interactive scope can be modified by the&lt;br /&gt;
&lt;code&gt;setContext&lt;/code&gt; function.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;getContext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;InteractiveImport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;setContext&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;InteractiveImport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When a module is interpreted and loaded as an interactive import it has its full&lt;br /&gt;
top-level scope available. We can manipulate, query and extend this scope using&lt;br /&gt;
various function.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;parseName&lt;/code&gt; can be used to resolve a name (or names) from a given string to a&lt;br /&gt;
set of symbols in the interactive context. This returns a &lt;code&gt;Name&lt;/code&gt; object (more&lt;br /&gt;
on this later) which is GHC&#39;s internal name type that holds position and a&lt;br /&gt;
unique identifier.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;parseName&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To resolve the type of an given expression the &lt;code&gt;exprType&lt;/code&gt; can be used to&lt;br /&gt;
extract the type information within the current context.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;exprType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And within the entire interactive context we can query the set of all names that&lt;br /&gt;
have been brought into scope by imports. This is used for the interactive&lt;br /&gt;
&lt;code&gt;:browse&lt;/code&gt; command.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;getNamesInScope&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the most important function is evaluation of arbitrary expressions with in&lt;br /&gt;
the interactive context. Which is accomplished via &lt;code&gt;dynCompileExpr &lt;/code&gt;. This&lt;br /&gt;
returns a &lt;code&gt;Dynamic&lt;/code&gt; which can be safely cast using &lt;code&gt;fromDynamic&lt;/code&gt; for any&lt;br /&gt;
instance of &lt;code&gt;Typeable&lt;/code&gt;. This is used to dynamically evaluate a string&lt;br /&gt;
expression within the interactive context.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;dynCompileExpr&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dynamic&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;fromDynamic&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Typeable&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Dynamic&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;package-database&quot; tabindex=&quot;-1&quot;&gt;Package Database&lt;/h4&gt;
&lt;p&gt;In it&#39;s default state GHC is aware of two package databases: the global package&lt;br /&gt;
database in &lt;code&gt;/usr/lib/ghc-x.x.x/&lt;/code&gt; and the user database in &lt;code&gt;~/.ghc/lib&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This however can be extended via the “GHC_PACKAGE_PATH” environment variable&lt;br /&gt;
which reads the path variable and applies the &lt;code&gt;extraPkgConfs&lt;/code&gt; function to add&lt;br /&gt;
it to the package database. This is used in the various modern sandboxing&lt;br /&gt;
techniques used in tools like &lt;code&gt;cabal&lt;/code&gt; and &lt;code&gt;stack&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;extraPkgConfs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PkgConfRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;PkgConfRef&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To modify the given dynflags with a filepath, the following function can be&lt;br /&gt;
used to extend the state.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;addPkgDbs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;GhcMonad&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;FilePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;addPkgDbs&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fps&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dfs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;pkgs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;PkgConfFile&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fps&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dfs&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dfs&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;extraPkgConfs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;pkgs&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;extraPkgConfs&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dfs&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dfs&#39;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;initPackages&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dfs&#39;&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Stack sets this when launching the shell with &lt;code&gt;stack repl&lt;/code&gt;. More on modifying&lt;br /&gt;
this will be discussed later.&lt;/p&gt;
&lt;h4 id=&quot;mini-ghci&quot; tabindex=&quot;-1&quot;&gt;Mini GHCi&lt;/h4&gt;
&lt;p&gt;Ok, so let&#39;s a build a very small interactive shell for GHC. If you&#39;re not&lt;br /&gt;
familiar with &lt;a href=&quot;http://dev.stephendiehl.com/hask/#haskeline&quot;&gt;Haskeline&lt;/a&gt; (the&lt;br /&gt;
platform-agnostic &lt;a href=&quot;https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html&quot;&gt;readline&lt;/a&gt; abstraction) then read up on that first.&lt;/p&gt;
&lt;p&gt;The Haskeline interface is exposed as a monad transformer &lt;code&gt;InputT&lt;/code&gt; which&lt;br /&gt;
inside of IO gives us our interactive repl monad.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;InputT&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To set up the initial session set get the default dynflags, set the target to be&lt;br /&gt;
interpreted and memory-linked and twiddle the &lt;code&gt;-XExtendedDefaultRules&lt;/code&gt; flag.&lt;br /&gt;
We set the interactive shell to import the &lt;code&gt;Prelude&lt;/code&gt; and then monadically&lt;br /&gt;
return the resulting session so that we can progressively add to it on each&lt;br /&gt;
shell commnad.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;initSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;initSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Setting up HscEnv&quot;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSessionDynFlags&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;hscTarget&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscInterpreted&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcLink&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;LinkInMemory&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;token operator&quot;&gt;`xopt_set`&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Opt_ExtendedDefaultRules&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSessionDynFlags&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dflags&#39;&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setContext&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IIDecl&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;simpleImportDecl&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkModuleName&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Prelude&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSession&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each our interactive shell commands is then wrapped in a helper function&lt;br /&gt;
&lt;code&gt;session&lt;/code&gt; which spins up a new Ghc monad but restores the session from the&lt;br /&gt;
last compilation. The monadic action is then evaluated and the resulting session&lt;br /&gt;
afterwards is returned as a value to be reused.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;runGhc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;libdir&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setSession&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getSession&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The evaluator function tries two different compilation steps. First it tries to&lt;br /&gt;
compile the expression as is to see if it evaluates to a &lt;code&gt;IO a&lt;/code&gt; action. If it&lt;br /&gt;
does it is then evaluated directly within the monad. If it does not then the&lt;br /&gt;
&lt;code&gt;fromDynamic&lt;/code&gt; cast will simply yield a Nothing and we&#39;ll try to wrap the&lt;br /&gt;
expression in a print statement. The resulting compiled expression is guaranteed&lt;br /&gt;
to be an &lt;code&gt;IO a&lt;/code&gt; so we unsafely coerce the compiled code pointer that GHC gives&lt;br /&gt;
us into IO and run it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;inp&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;dyn&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fromDynamic&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;$&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dynCompileExpr&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;inp&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;dyn&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;compileExpr&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Prelude.print (&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;inp&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token comment&quot;&gt;-- &#39;print&#39; is constrained to &#39;IO ()&#39; so unsafeCoerce is &quot;safe&quot;&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;unsafeCoerce&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;act&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;act&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;act&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To add an import we simply cons the import as a module name to the context and&lt;br /&gt;
then yield the new state.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;addImport&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Ghc&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;addImport&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getContext&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;setContext&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;IIDecl&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;simpleImportDecl&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;mkModuleName&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ctx&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we do the naughty thing of catching all exceptions that are thrown and just&lt;br /&gt;
printing them out. This is fairly justified in the case that if expression&lt;br /&gt;
compilation fails we have to just trap and report the failure in the embedded&lt;br /&gt;
interpreter logic.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;ghcCatch&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;MonadIO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;ghcCatch&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;mres&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;m&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;mres&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;SomeException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;liftIO&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;err&lt;/span&gt;
      &lt;span class=&quot;token builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Right&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The REPL then just reads the user&#39;s input and dispatch based on whether the line&lt;br /&gt;
starts with the keyword &lt;code&gt;import&lt;/code&gt;. Depending on the expression line it then&lt;br /&gt;
spins up a GHC session with the currently held HscEnv from the last line and&lt;br /&gt;
trys to compile it. If succesfully it then calls &lt;code&gt;repl&lt;/code&gt; with the new &lt;code&gt;env&lt;/code&gt;&lt;br /&gt;
state ad-infinitum. Unix signals for aborting are handled by Haskeline monad.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;HscEnv&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Repl&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;minput&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;getInputLine&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&gt;&gt;&gt; &quot;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;minput&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;outputStrLn&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Goodbye.&quot;&lt;/span&gt;

    &lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;import&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;`isPrefixOf`&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;concat&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;tail&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;input&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;env&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcCatch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;addImport&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token builtin&quot;&gt;maybe&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&#39;&lt;/span&gt;

    &lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;token hvariable&quot;&gt;env&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;ghcCatch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;session&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token builtin&quot;&gt;maybe&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then putting it all together.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;initSession&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;runInputT&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;defaultSettings&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;repl&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can then run our little shell.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;$ stack build dive
$ stack &lt;span class=&quot;token builtin class-name&quot;&gt;exec&lt;/span&gt; dive

Setting up HscEnv
&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; fmap &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;+1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2,3&lt;/span&gt;,4,5,6,7,8,9,10,11&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;import&lt;/span&gt; Data.Text
&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
Goodbye.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So that&#39;s our custom Mini GHCi. In practice &lt;a href=&quot;https://github.com/ghc/ghc/tree/master/libraries/ghci&quot;&gt;real&lt;br /&gt;
GHCi&lt;/a&gt; does things a&lt;br /&gt;
little differently, but some underlying machinery remains the same. Other&lt;br /&gt;
features like name lookup and introspection are left as an exercise to the&lt;br /&gt;
reader. A fun next project would be to create tiny shell with an introspection&lt;br /&gt;
tool querying the original source code of any definition in scope.&lt;/p&gt;
&lt;h4 id=&quot;summary-%26-next-steps&quot; tabindex=&quot;-1&quot;&gt;Summary &amp;amp; Next Steps&lt;/h4&gt;
&lt;p&gt;This is the &amp;quot;Very High Level&amp;quot; API we can use to interact with GHC. Next we&#39;ll&lt;br /&gt;
concern ourselves with the guts of the internal artifacts used and how to&lt;br /&gt;
introspect and build them programatically.&lt;/p&gt;
</description>
      <pubDate>Tue, 21 Jun 2016 01:00:00 +0000</pubDate>
      <link>https://www.stephendiehl.com/posts/ghc_01/</link>
      <guid isPermaLink="true">https://www.stephendiehl.com/posts/ghc_01/</guid>
    </item>
    
    <item>
      <title>Building a Better Custom Haskell Prelude</title>
      <description>&lt;h3 id=&quot;building-a-better-custom-haskell-prelude&quot; tabindex=&quot;-1&quot;&gt;Building a Better Custom Haskell Prelude&lt;/h3&gt;
&lt;p&gt;The Haskell Prelude is the default import into all Haskell modules, it provides&lt;br /&gt;
an endless number of ways to shoot ourselves in the foot and historical cruft&lt;br /&gt;
that can&#39;t be removed. While it is difficult to fix upstream, we can however&lt;br /&gt;
remove the Prelude entirely on a project-level and replace it with a more&lt;br /&gt;
sensible set of defaults using the &lt;code&gt;-XNoImplicitPrelude&lt;/code&gt; language extension.&lt;/p&gt;
&lt;p&gt;There are two philosophies on building new Preludes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Big Vehicle Prelude&lt;/em&gt; - Fix most of the deficiencies in the Prelude by&lt;br /&gt;
introducing new abstractions that replace large portions of the basic types&lt;br /&gt;
and class, and generally redefine the way we write Haskell. See&lt;br /&gt;
&lt;a href=&quot;https://hackage.haskell.org/package/numeric-prelude&quot;&gt;numeric-prelude&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Small Vehicle Prelude&lt;/em&gt; - Fix the broken parts of the Prelude by building on&lt;br /&gt;
existing types and classes and masking broken bits. See&lt;br /&gt;
&lt;a href=&quot;https://hackage.haskell.org/package/numeric-prelude&quot;&gt;basic-prelude&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I don&#39;t prescribe to the large vehicle approach, a lot of the default Prelude is&lt;br /&gt;
not ideal but good enough to get the job done. And interoperability with the&lt;br /&gt;
rest of the ecosystem, which typically uses default Base and Prelude, is&lt;br /&gt;
incredibly important.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&quot;http://www.stephendiehl.com/posts/production.html&quot;&gt;previous posts&lt;/a&gt; I&#39;ve&lt;br /&gt;
written about how rolling our own small-vehicle Prelude is a good idea for large&lt;br /&gt;
teams working in industry but I left it somewhat ambiguous about what to&lt;br /&gt;
include. Obviously everyone&#39;s company is different so I thought I&#39;d expand on&lt;br /&gt;
what I consider a sensible set of defaults that one could use to then build a&lt;br /&gt;
custom Prelude. We&#39;ll call this a &lt;em&gt;Protolude&lt;/em&gt;.&lt;/p&gt;
&lt;p style=&quot;text-align:center&quot;&gt;
**[Git Project](https://github.com/sdiehl/protolude)**
&lt;/p&gt;
&lt;h4 id=&quot;module-structure&quot; tabindex=&quot;-1&quot;&gt;Module Structure&lt;/h4&gt;
&lt;p&gt;The basic module structure is consists of a main module reexport which we&#39;ll&lt;br /&gt;
dump everything in called &lt;code&gt;X&lt;/code&gt;. And various miscellaneous functions which&lt;br /&gt;
we&#39;ll reexport from the root module. Off of this we&#39;ll have several utilities&lt;br /&gt;
modules which have domain specific functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;{-# LANGUAGE NoImplicitPrelude #-}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Protolude&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;identity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;uncons&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token hvariable&quot;&gt;applyN&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

  &lt;span class=&quot;token constant&quot;&gt;LText&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token constant&quot;&gt;LByteString&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Prelude &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; P&lt;/span&gt;

&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; List &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Show &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Bool &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Debug &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Monad &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;qualified&lt;/span&gt; Applicative &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;base-types&quot; tabindex=&quot;-1&quot;&gt;Base Types&lt;/h4&gt;
&lt;p&gt;The Basic types (&lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Integer&lt;/code&gt;, &lt;code&gt;Float&lt;/code&gt;, &lt;code&gt;Char&lt;/code&gt;) we reexport wholesale&lt;br /&gt;
along with their associated functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;-- Base types&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Int &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Bits &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Word &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Bool &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X &lt;span class=&quot;token keyword&quot;&gt;hiding&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Char &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Maybe &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X &lt;span class=&quot;token keyword&quot;&gt;hiding&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;fromJust&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Either &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;
&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Complex &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt;

&lt;span class=&quot;token import-statement&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Function &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; X&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;const&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;flip&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;fix&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;on&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define the flipped application operator for GHC &amp;lt; 7.10 which does not provide&lt;br /&gt;
this be default.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We rename the &lt;code&gt;id&lt;/code&gt; function to identity since it&#39;s far more likely we&#39;ll have&lt;br /&gt;
variable names called &lt;code&gt;id&lt;/code&gt; over the identity function. This is a contentious&lt;br /&gt;
point and your mileage may vary with this change.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;identity&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;identity&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The bool function (the combinator analogue of &lt;code&gt;maybe&lt;/code&gt; and &lt;code&gt;either&lt;/code&gt;) is&lt;br /&gt;
provided by GHC 7.10, but we reexport it for earlier versions.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;uncons&lt;/code&gt; function is not provided by default, but it unpacks a list into a&lt;br /&gt;
Maybe tuple containing the head in the first tuple parameter if it exists.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;uncons&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;uncons&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Nothing&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;uncons&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;applyN&lt;/code&gt; function takes a function and a count and applies it &lt;code&gt;n&lt;/code&gt; number&lt;br /&gt;
of times.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-haskell&quot;&gt;&lt;span class=&quot;token hvariable&quot;&gt;applyN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;token hvariable&quot;&gt;applyN&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;token hvariable&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;token punctua